In the Netherlands most people receive the value of the current electricity price for power_delivered, however since 1 July many have to pay about 11 cents for every kWh delivered back to the net.
Delivered kWh to the net used to bring in 27-33 cents per kWh and that is now reduced to 16-22 cents per kWh.
The device I constructed is a little box with an ESP8266 and a green and a red led.
green - power_delivery > 2000 Watt
green and red - power_delivery between 10 and 2000 Watt
red - power consumed
The code for the ESP8266 is
Code: Select all
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
* https://randomnerdtutorials.com/esp32-mqtt-publish-subscribe-arduino-ide/
*********/
// Load Wi-Fi library
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <PubSubClient.h>
#include <Arduino.h>
// Replace the next variables with your SSID/Password combination
const char* ssid = "your-SSID";
const char* password = "your-password";
// Add your MQTT Broker IP address, example:
const char* mqtt_server = "192.168.1.144";
WiFiClient myClient;
PubSubClient client(myClient);
long lastMsg = 0;
char msg[50];
int value = 0;
// LED Pin for testing
// const int ledPin = 2; // D4 is D4 PLUS the onboard LED
// onboard LOW is ON D4 low is OFF
const int ledRood = 13; // D7
const int ledGroen = 12; // D6
void setup() {
Serial.begin(115200);
// default settings
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(ledRood, OUTPUT);
pinMode(ledGroen, OUTPUT);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic myoutput , you check if the message is either "on" or "off".
// Changes the output state according to the message
if (String(topic) == "my/output") {
Serial.print("1 Output receveid output : ");
if(messageTemp == "roodon"){
Serial.println("4 ledRood ON");
digitalWrite(ledRood, HIGH);
}
else if(messageTemp == "roodoff"){
Serial.println("5 ledRood OFF");
digitalWrite(ledRood, LOW);
}
if(messageTemp == "groenon"){
Serial.println("6 ledgGroen ON");
digitalWrite(ledGroen, HIGH);
}
else if(messageTemp == "groenoff"){
Serial.println("7 ledGroen OFF");
digitalWrite(ledGroen, LOW);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Subscribe
client.subscribe("my/output");
} 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() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 5000) {
lastMsg = now;
/*
// Temperature in Celsius
temperature = bme.readTemperature();
// Uncomment the next line to set temperature in Fahrenheit
// (and comment the previous temperature line)
//temperature = 1.8 * bme.readTemperature() + 32; // Temperature in Fahrenheit
// Convert the value to a char array
char tempString[8];
dtostrf(temperature, 1, 2, tempString);
Serial.print("Temperature: ");
Serial.println(tempString);
client.publish("esp32/temperature", tempString);
humidity = bme.readHumidity();
// Convert the value to a char array
char humString[8];
dtostrf(humidity, 1, 2, humString);
Serial.print("Humidity: ");
Serial.println(humString);
client.publish("esp32/humidity", humString); */
}
}
Code: Select all
-- PV panels P1 power_delivery indicated by LED lights
-- assumptions:
local P1 = 'Power' -- P1 reading
--local LOGGING = true
return {
active = true,
on =
{
timer =
{
'every minute'-- between 20 minutes before sunrise and 20 minutes aftter sunset'
},
},
logging = {
level = domoticz.LOG_ERROR, -- domoticz.LOG_DEBUG for debug, to domoticz.LOG_ERROR when all OK
marker = "----- P1 power_delivery ---------"
},
-- between 30 minutes after sunrise and 20 minutes before sunset'
--LOG level: This is the log level you want for this script. Can be domoticz.LOG_INFO, domoticz.LOG_MODULE_EXEC_INFO, domoticz.LOG_DEBUG or domoticz.LOG_ERROR
--marker: A string that is prefixed before each log message. That way you can easily create a filter in the Domoticz log to see just these messages.
execute = function(dz)
-- domoticz.utils.dumpTable(item.json) -- dumpTable laat alle waarden zien, mooi voor debugging
-- collect all input data
local power_usage = dz.devices(P1).usage -- usage
local power_delivery = dz.devices(P1).usageDelivered
-- info only on log level = LOG_DEBUG
dz.log('Gebruik : ' .. power_usage, dz.LOG_DEBUG)
dz.log('Teruglevering : ' .. power_delivery, dz.LOG_DEBUG)
if (power_delivery >= 2000) then
-- groene led aan
dz.executeShellCommand('mosquitto_pub -d -t my/output -h 192.168.1.144 -m "roodoff"')
dz.executeShellCommand('mosquitto_pub -d -t my/output -h 192.168.1.144 -m "groenon"')
elseif (power_delivery < 2000) and power_delivery > 10 then
-- groene led en rode led aan
dz.executeShellCommand('mosquitto_pub -d -t my/output -h 192.168.1.144 -m "roodon"')
dz.executeShellCommand('mosquitto_pub -d -t my/output -h 192.168.1.144 -m "groenon"')
elseif (power_delivery <= 10) then
-- rode led aan
dz.executeShellCommand('mosquitto_pub -d -t my/output -h 192.168.1.144 -m "roodon"')
dz.executeShellCommand('mosquitto_pub -d -t my/output -h 192.168.1.144 -m "groenoff"')
end
end
}
-- end power_delivery level indicator
PS The code shows the simplicity of using MQTT in Domoticz.