diff --git a/software/firmware/energy-montior/02-energy-monitor-mqtt/02-energy-monitor-mqtt.ino b/software/firmware/energy-montior/02-energy-monitor-mqtt/02-energy-monitor-mqtt.ino index 1ed43f2..1ba10cd 100644 --- a/software/firmware/energy-montior/02-energy-monitor-mqtt/02-energy-monitor-mqtt.ino +++ b/software/firmware/energy-montior/02-energy-monitor-mqtt/02-energy-monitor-mqtt.ino @@ -1,26 +1,109 @@ /* -SCT-013 Sensor - Power meassurement, based on Thomas Edlinger's code for "www.edistechlab.com" Required libraries (Tools -> manage libraries) - - EmonLib libary V1.1.0 by OpenEnergyMonitor -Based on EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3 + - EmonLib@1.1.0 + - PubSubClient@2.8.0 + - Wifi */ - #include "EmonLib.h" +#include +#include +#include "environment.h" // put your credentials and configuration in, here + +// sensor EnergyMonitor emon1; const byte current1Pin = A1; // ADC-PIN 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() { 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 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() { - double Irms = emon1.calcIrms(1480); - Serial.print(Irms*voltage); + double irms = emon1.calcIrms(1480); + double power = irms * voltage; + + + + // debug, print out on serial + Serial.print(power); Serial.print(" Watt - "); - Serial.print(Irms); + Serial.print(irms); 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); } \ No newline at end of file diff --git a/software/firmware/energy-montior/02-energy-monitor-mqtt/environment.h b/software/firmware/energy-montior/02-energy-monitor-mqtt/environment.h index 51ad9ce..5a12318 100644 --- a/software/firmware/energy-montior/02-energy-monitor-mqtt/environment.h +++ b/software/firmware/energy-montior/02-energy-monitor-mqtt/environment.h @@ -1,5 +1,5 @@ // Replace with your network credentials -#define secrect_ssid "your_ssid" -#define secret_password "your_password" -#define mqttServer = "m16.cloudmqtt.com" -#define mqttPort = 12595 +#define secrect_ssid "Guest" +#define secret_password "guestguest" +#define mqtt_server "192.168.2.103" +#define mqtt_port 1883