managed to send data over MQTT

This commit is contained in:
simonox 2023-02-20 16:03:00 +01:00
parent e52764751c
commit ce07ae0466
2 changed files with 94 additions and 11 deletions

View file

@ -1,26 +1,109 @@
/* /*
SCT-013 Sensor - Power meassurement, based on Thomas Edlinger's code for "www.edistechlab.com"
Required libraries (Tools -> manage libraries) Required libraries (Tools -> manage libraries)
- EmonLib libary V1.1.0 by OpenEnergyMonitor - EmonLib@1.1.0
Based on EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3 - PubSubClient@2.8.0
- Wifi
*/ */
#include "EmonLib.h" #include "EmonLib.h"
#include <WiFi.h>
#include <PubSubClient.h>
#include "environment.h" // put your credentials and configuration in, here
// sensor
EnergyMonitor emon1; EnergyMonitor emon1;
const byte current1Pin = A1; // ADC-PIN const byte current1Pin = A1; // ADC-PIN
const byte voltage = 230; // Power voltage in Europe = 230 V const byte voltage = 230; // Power voltage in Europe = 230 V
// refrences from environment.h
const char* ssid = secrect_ssid;
const char* password = secret_password;
const char* mqttServer = mqtt_server;
const int mqttPort = mqtt_port;
// wifi and MQTT
WiFiClient wifiClient;
PubSubClient client(wifiClient);
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial.setTimeout(500);
setup_energy_sensor();
setup_wifi();
client.setServer(mqttServer, mqttPort);
reconnect();
}
void setup_energy_sensor() {
analogReadResolution(ADC_BITS); // activate 12 Bit resolution for our ESP32 analogReadResolution(ADC_BITS); // activate 12 Bit resolution for our ESP32
emon1.current(current1Pin, 8); // Pin and Calibration emon1.current(current1Pin, 8); // Pin and Calibration
} }
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
//Once connected, publish an announcement...
client.publish("/iot-platform/engergy-montitor/test-device/status", "online");
// ... and resubscribe
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() { void loop() {
double Irms = emon1.calcIrms(1480); double irms = emon1.calcIrms(1480);
Serial.print(Irms*voltage); double power = irms * voltage;
// debug, print out on serial
Serial.print(power);
Serial.print(" Watt - "); Serial.print(" Watt - ");
Serial.print(Irms); Serial.print(irms);
Serial.println(" Ampere"); Serial.println(" Ampere");
// convert double to char array for MQTT
char powerArray[10];
snprintf(powerArray, 10, "%f", power);
char irmsArray[10];
snprintf(irmsArray, 10, "%f", irms);
printf("%s", irmsArray);
// publish values
client.publish("/iot-platform/engergy-montitor/test-device/watt", powerArray);
client.publish("/iot-platform/engergy-montitor/test-device/ampere", irmsArray);
// wait a second
delay(1000); delay(1000);
} }

View file

@ -1,5 +1,5 @@
// Replace with your network credentials // Replace with your network credentials
#define secrect_ssid "your_ssid" #define secrect_ssid "Guest"
#define secret_password "your_password" #define secret_password "guestguest"
#define mqttServer = "m16.cloudmqtt.com" #define mqtt_server "192.168.2.103"
#define mqttPort = 12595 #define mqtt_port 1883