iot-backend/software/firmware/energy-montior/02-energy-monitor-mqtt/02-energy-monitor-mqtt.ino

116 lines
2.8 KiB
Arduino
Raw Normal View History

2023-02-18 17:20:13 +01:00
/*
Required libraries (Tools -> manage libraries)
2023-02-20 16:03:00 +01:00
- EmonLib@1.1.0
- PubSubClient@2.8.0
- Wifi
2023-02-18 17:20:13 +01:00
*/
2023-02-20 16:15:49 +01:00
#include <stdlib.h>
#include <string.h>
2023-02-18 17:20:13 +01:00
#include "EmonLib.h"
2023-02-20 16:03:00 +01:00
#include <WiFi.h>
#include <PubSubClient.h>
#include "environment.h" // put your credentials and configuration in, here
// sensor
2023-02-18 17:20:13 +01:00
EnergyMonitor emon1;
const byte current1Pin = A1; // ADC-PIN
2023-02-18 17:20:13 +01:00
const byte voltage = 230; // Power voltage in Europe = 230 V
2023-02-20 16:03:00 +01:00
// refrences from environment.h
const char* ssid = secrect_ssid;
const char* password = secret_password;
const char* mqttServer = mqtt_server;
const int mqttPort = mqtt_port;
2023-02-20 16:15:49 +01:00
const char* mqttPrefix = mqtt_prefix;
2023-02-20 16:03:00 +01:00
// wifi and MQTT
WiFiClient wifiClient;
PubSubClient client(wifiClient);
2023-02-18 17:20:13 +01:00
void setup() {
Serial.begin(115200);
2023-02-20 16:03:00 +01:00
Serial.setTimeout(500);
setup_energy_sensor();
setup_wifi();
client.setServer(mqttServer, mqttPort);
reconnect();
}
void setup_energy_sensor() {
2023-02-18 17:20:13 +01:00
analogReadResolution(ADC_BITS); // activate 12 Bit resolution for our ESP32
emon1.current(current1Pin, 8); // Pin and Calibration
2023-02-18 17:20:13 +01:00
}
2023-02-20 16:03:00 +01:00
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...
2023-02-20 16:15:49 +01:00
client.publish(concat(mqttPrefix, "/status"), "online");
2023-02-20 16:03:00 +01:00
// ... 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);
}
}
}
2023-02-20 16:15:49 +01:00
char* concat(const char* str1, const char* str2) {
char* result;
asprintf(&result, "%s%s", str1, str2);
return result;
}
2023-02-18 17:20:13 +01:00
void loop() {
2023-02-20 16:15:49 +01:00
// get values
2023-02-20 16:03:00 +01:00
double irms = emon1.calcIrms(1480);
double power = irms * voltage;
// debug, print out on serial
Serial.print(power);
2023-02-18 17:20:13 +01:00
Serial.print(" Watt - ");
2023-02-20 16:03:00 +01:00
Serial.print(irms);
2023-02-18 17:20:13 +01:00
Serial.println(" Ampere");
2023-02-20 16:03:00 +01:00
// convert double to char array for MQTT
char powerArray[10];
snprintf(powerArray, 10, "%f", power);
char irmsArray[10];
snprintf(irmsArray, 10, "%f", irms);
2023-02-20 16:15:49 +01:00
2023-02-20 16:03:00 +01:00
// publish values
2023-02-20 16:15:49 +01:00
client.publish(concat(mqttPrefix, "/watt"), powerArray);
client.publish(concat(mqttPrefix, "/ampere"), irmsArray);
2023-02-20 16:03:00 +01:00
// wait a second
2023-02-18 17:20:13 +01:00
delay(1000);
}