From e52764751c31436192f35948d0db10713dbd1fc3 Mon Sep 17 00:00:00 2001 From: simonox Date: Mon, 20 Feb 2023 14:51:10 +0100 Subject: [PATCH] restructured code to prepare MQTT --- .../01-energy-monitor-serial-out.ino} | 0 .../02-energy-monitor-mqtt.ino | 26 +++++++++++++++++++ .../02-energy-monitor-mqtt/environment.h | 5 ++++ software/firmware/energy-montior/README.md | 12 +++++++-- 4 files changed, 41 insertions(+), 2 deletions(-) rename software/firmware/energy-montior/{energy-monitor/energy-monitor.ino => 01-energy-monitor-serial-out/01-energy-monitor-serial-out.ino} (100%) create mode 100644 software/firmware/energy-montior/02-energy-monitor-mqtt/02-energy-monitor-mqtt.ino create mode 100644 software/firmware/energy-montior/02-energy-monitor-mqtt/environment.h diff --git a/software/firmware/energy-montior/energy-monitor/energy-monitor.ino b/software/firmware/energy-montior/01-energy-monitor-serial-out/01-energy-monitor-serial-out.ino similarity index 100% rename from software/firmware/energy-montior/energy-monitor/energy-monitor.ino rename to software/firmware/energy-montior/01-energy-monitor-serial-out/01-energy-monitor-serial-out.ino 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 new file mode 100644 index 0000000..1ed43f2 --- /dev/null +++ b/software/firmware/energy-montior/02-energy-monitor-mqtt/02-energy-monitor-mqtt.ino @@ -0,0 +1,26 @@ +/* +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 +*/ + +#include "EmonLib.h" +EnergyMonitor emon1; +const byte current1Pin = A1; // ADC-PIN +const byte voltage = 230; // Power voltage in Europe = 230 V + +void setup() { + Serial.begin(115200); + analogReadResolution(ADC_BITS); // activate 12 Bit resolution for our ESP32 + emon1.current(current1Pin, 8); // Pin and Calibration +} + +void loop() { + double Irms = emon1.calcIrms(1480); + Serial.print(Irms*voltage); + Serial.print(" Watt - "); + Serial.print(Irms); + Serial.println(" Ampere"); + 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 new file mode 100644 index 0000000..51ad9ce --- /dev/null +++ b/software/firmware/energy-montior/02-energy-monitor-mqtt/environment.h @@ -0,0 +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 diff --git a/software/firmware/energy-montior/README.md b/software/firmware/energy-montior/README.md index f05ec45..92f2cfa 100644 --- a/software/firmware/energy-montior/README.md +++ b/software/firmware/energy-montior/README.md @@ -68,7 +68,7 @@ See the Fritzing file for [details](./energy-monitor/energy-monitor.fzz). Start with a simple code that just prints the values. The code is quite simple, as we can use the existing *[EmonLib libary V1.1.0 by OpenEnergyMonitor](https://docs.openenergymonitor.org/electricity-monitoring/ct-sensors/)*. -[Check out the small amount of code to print the values to serial out.](https://code.curious.bio/curious.bio/iot-platform/src/commit/ecf0b3ee63c9d3d49bcf32a7da9bde64a6a62c28/software/firmware/energy-montior/energy-monitor/energy-monitor.ino) This piece of code is based on on Thomas Edlinger's code for [Edi's Tech Lab](https://www.edistechlab.com). +[Check out the small amount of code to print the values to serial out.](./01-energy-monitor-serial-out/) This piece of code is based on on Thomas Edlinger's code for [Edi's Tech Lab](https://www.edistechlab.com). The only interesting part is this line: @@ -90,8 +90,16 @@ The code just prints the current power consumption to serial out: ``` #### Post to MQTT -// TODO: Continue here +##### Credentials +To connect to your wifi and access your MQTT server you have to add this to an `environment` header file: + +```C +#define secrect_ssid "your_ssid" +#define secret_password "your_password" +#define mqttServer = "m16.cloudmqtt.com" +#define mqttPort = 12595 +``` ## Links