PZEM 04 Wemos D1 to DOMOTICZ With Oled
by spike318 in Circuits > Arduino
777 Views, 2 Favorites, 0 Comments
PZEM 04 Wemos D1 to DOMOTICZ With Oled
New project started : PZEM Multi modules with Oled and all data sent to Domoticz.
Programmed in IDE ARDUINO !! NO TASMOTA or ESP EASY !!!
3 Modules to monitor 3 circuits
And also a reset button on the device to reset the total kWh counter
Preparing PZEM Module
First thing to do !! You need to modify your PZEM module to work with Wemos D1
Pzem add resistor from 1K between RX en 5v. Important PZEM must be connected to 3.3 V
Set the Adress From Each Module
In the code you need only to change this value, eg 0X01 , 0X02, 0X03 ! These are the codes i used for my project
So before you can use this, you must program each module seperately !! Dont't connect more modules at the same time if you want to program them !
The Code to Programm the PZEM Adress
// Use this to set the PZEM adress, only one module may be connected to the Wemos D1
#include <Arduino.h>
#include <PZEM004Tv30.h>
PZEM004Tv30 pzem(D2, D1); // (RX,TX)connect to TX,RX of PZEM
// Change settings below to the adress you want to sent to the module
#define SET_ADDRESS 0x04
void setup()
{
Serial.begin(115200);
}
void loop() {
static uint8_t addr = SET_ADDRESS;
// Set the custom address
Serial.print("Setting address to: 0x");
Serial.println(addr, HEX);
if(!pzem.setAddress(addr))
{
// Setting custom address failed. Probably no PZEM connected
Serial.println("Error setting address.");
} else {
// Print out the new custom address
Serial.print("Current address: 0x");
Serial.println(pzem.readAddress(), HEX);
Serial.println();
}
delay(5000);
}
Now Setup Your Hardware Like Shown Below !
The Code for the Whole Project
#include <Arduino.h>
#include<Wire.h>
#include <PZEM004Tv30.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <U8g2lib.h>
//oled display setup
U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// WiFi settings van je eigen netwerk
#define wifi_ssid "+++++++++"
#define wifi_password "jcdsdfsdf2"
// HTTP Domoticz settings
const char* host = "192.168.1.19";
const int port = 8080;
HTTPClient http;
// PZEM setup bij meerdere moet het adress ingevuld zijn ! 0x..
PZEM004Tv30 pzem(D5, D6,0x01); // (RX,TX)connect to TX,RX of PZEM
PZEM004Tv30 pzem1(D5, D6,0x02); // (RX,TX)connect to TX,RX of PZEM
PZEM004Tv30 pzem2(D5, D6,0x03); // (RX,TX)connect to TX,RX of PZEM
void setup()
{
Serial.begin(115200);
// setup_wifi();
// pzem.resetEnergy();
Serial.println("startup");
// display setup
u8g2.begin();
u8g2.enableUTF8Print();
// wifi i verbinding oproepen
setup_wifi();
}
void setvar()
{
//pzem1 waardes voor naar domoticz te zenden
// allen van PZEM1 sturen we volt frequentie en pf door voor de andere alleen power en energy
//pzem1 0x01
int idxvar1 = (294); // domoticz IDX for VAR1 power
int VAR1 = pzem.power();
int idxvar2 = (295);
float VAR2 = pzem.voltage();
int idxvar3 = (296);
float VAR3 = pzem.energy();
int idxvar4 = (296);
float VAR4 = pzem.frequency();
int idxvar5 = (296);
float VAR5 = pzem.pf();
//pzem 0x02
int idxvar6 = (294); // domoticz IDX for VAR1 power
int VAR6 = pzem1.power();
int idxvar7 = (296);
float VAR7 = pzem1.energy();
//pzem 0x03
int idxvar8 = (294); // domoticz IDX for VAR1 power
int VAR8 = pzem2.power();
int idxvar9 = (296);
float VAR9 = pzem2.energy();
Serial.println(VAR1);
Serial.println(VAR2);
Serial.println(VAR3);
Serial.println(VAR4);
Serial.println(VAR5);
Serial.println(VAR6);
Serial.println(VAR7);
Serial.println(VAR8);
Serial.println(VAR9);
// Here is the Json format that Domoticz expects
// /json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP
// VAR1 versturen
String URLVAR1 = "/json.htm?type=command¶m=udevice&idx=";
URLVAR1 += String(idxvar1); // IDX van je Device in domoticz
URLVAR1 += "&nvalue=0&svalue=";
URLVAR1 += String(VAR1); // dit is de waarde die naar de IDX gestuurd word in domoticz
// VAR 2 versturen
String URLVAR2 = "/json.htm?type=command¶m=udevice&idx=";
URLVAR2 += String(idxvar2); // IDX van je Device in domoticz
URLVAR2 += "&nvalue=0&svalue=";
URLVAR2 += String(VAR2); // dit is de waarde die naar de IDX gestuurd word in domoticz
// VAR 3 versturen
String URLVAR3 = "/json.htm?type=command¶m=udevice&idx=";
URLVAR3 += String(idxvar3); // IDX van je Device in domoticz
URLVAR3 += "&nvalue=0&svalue=";
URLVAR3 += String(VAR3); // dit is de waarde die naar de IDX gestuurd word in domoticz
sendToDomoticz(URLVAR1);
sendToDomoticz(URLVAR2);
sendToDomoticz(URLVAR3);
}
void loop()
{
setvar(); // variable zenden naar domotics
// wait for WiFi connection
//main energy meter
Serial.println();
Serial.print("meter 1: ");
float voltage = pzem.voltage();
float current = pzem.current();
int power = pzem.power(); // int omdat we geen comma getallen willen bij Watt
float energy = pzem.energy();
float frequency = pzem.frequency();
float pf = pzem.pf();
Serial.print(voltage);
Serial.print(" volt ");
Serial.print(current);
Serial.print(" Stroom ");
Serial.print(power);
Serial.print(" Watt ");
Serial.print(energy);
Serial.print(" Energy total ");
Serial.print(frequency);
Serial.print(" HZ ");
Serial.print(pf);
Serial.print(" pf ");
Serial.println();
Serial.print("meter 2: ");
float voltage1 = pzem1.voltage();
float current1 = pzem1.current();
int power1 = pzem1.power(); // int omdat we geen comma getallen willen bij Watt
float energy1 = pzem1.energy();
float frequency1 = pzem1.frequency();
float pf1 = pzem1.pf();
Serial.print(voltage1);
Serial.print(" volt ");
Serial.print(current1);
Serial.print(" Stroom ");
Serial.print(power1);
Serial.print(" Watt ");
Serial.print(energy1);
Serial.print(" Energy total ");
Serial.print(frequency1);
Serial.print(" HZ ");
Serial.print(pf1);
Serial.print(" pf ");
Serial.println();
Serial.print("meter 3: ");
float voltage2 = pzem2.voltage();
float current2 = pzem2.current();
int power2 = pzem2.power(); // int omdat we geen comma getallen willen bij Watt
float energy2 = pzem2.energy();
float frequency2 = pzem2.frequency();
float pf2 = pzem1.pf();
Serial.print(voltage2);
Serial.print(" volt ");
Serial.print(current2);
Serial.print(" Stroom ");
Serial.print(power2);
Serial.print(" Watt ");
Serial.print(energy2);
Serial.print(" Energy total ");
Serial.print(frequency2);
Serial.print(" HZ ");
Serial.print(pf2);
Serial.print(" pf ");
// u8g2.clearDisplay();
u8g2.setDisplayRotation(U8G2_R1);
u8g2.setFlipMode(0);
u8g2.firstPage();
do {
// u8g2.setFont(u8g2_font_cube_mel_tr);
//lokaal ip nummer weergeven zo zien we als we verbinding hebben met ons netwerk
u8g2.setFont(u8g2_font_5x7_tf);
// u8g2.drawStr(4,8,"Inj");
u8g2.setCursor(0, 6);
u8g2.println(WiFi.localIP());
/*
Description: Draw a line between two points. This procedure will use the current color (setDrawColor).
Arguments:
u8g2 : Pointer to the u8g2 structure (C interface only).
x0: X-position of the first point.
y0: Y-position of the first point.
x1: X-position of the second point.
y1: Y-position of the second point.
*/
u8g2.drawLine(0, 7, 118, 7);
// u8g2.setFont(u8g2_font_7x13_tr);
//u8g2.setFont(u8g2_font_luBS18_tf);
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.setCursor(0, 20);
u8g2.print(power);
u8g2.print(" W");
// u8g2.setFont(u8g2_font_6x10_tr);
u8g2.setCursor(0, 30);
u8g2.print(pzem.energy());
u8g2.print(" kWh");
//u8g2.setFont(u8g2_font_7x13_tr);
u8g2.setCursor(0, 42);
u8g2.print(power);
u8g2.print(" W");
// u8g2.setFont(u8g2_font_6x10_tr);
u8g2.setCursor(0, 52);
u8g2.print(pzem.energy());
u8g2.print(" kWh");
//u8g2.setFont(u8g2_font_7x13_tr);
u8g2.setCursor(0, 64);
u8g2.print(power);
u8g2.print(" W");
//u8g2.setFont(u8g2_font_6x10_tr);
u8g2.setCursor(0, 74);
u8g2.print(pzem.energy());
u8g2.print(" kWh");
u8g2.setCursor(0, 104);
u8g2.print(voltage);
u8g2.print(" V");
u8g2.setCursor(0, 114);
u8g2.print(frequency);
u8g2.print(" Hz");
u8g2.setCursor(0, 124);
u8g2.print(pf);
u8g2.print(" pf");
// u8g2.setCursor(0, 100);
//u8g2.print(pzem.voltage());
//u8g2.print(" V");
//u8g2.setFont(u8g2_font_luBS18_tf);
/*u8g2.setFont(u8g2_font_emoticons21_tr);
u8g2.drawStr(0,22,"3");//smile face happy
u8g2.setFont(u8g2_font_emoticons21_tr);
u8g2.drawStr(0,56,"&");//smile face angry
*/
} while ( u8g2.nextPage() );
//delay(1000);
delay(5000);
}
//Connect to wifi
void setup_wifi()
{
delay(10);
u8g2.firstPage();
do {
//u8g2.setDisplayRotation(U8G2_R1);
// u8g2.setFont(u8g2_font_8x13_tr);
//u8g2.setFont(u8g2_font_luBS18_tf);
// u8g2.setFont(u8g2_font_6x13_tr);
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.setCursor(0, 20);
u8g2.print(wifi_ssid);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connection Established");
Serial.print("IP Adress : ");
Serial.print(WiFi.localIP());
u8g2.setFont(u8g2_font_6x10_tr);
//u8g2.setFont(u8g2_font_6x13_tr);
u8g2.setCursor(0, 35);
u8g2.println("IP Adress : ");
u8g2.setCursor(0, 50);
u8g2.println(WiFi.localIP());;
} while ( u8g2.nextPage() );
}
void sendToDomoticz(String URLVAR1)
{
Serial.print("Connecting to ");
Serial.println(host);
Serial.print("Requesting URLVAR1: ");
Serial.println(URLVAR1);
http.begin(host,port,URLVAR1);
int httpCode = http.GET();
if (httpCode)
{
if (httpCode == 200)
{
String payload = http.getString();
Serial.println("Domoticz response ");
Serial.println(payload);
}
}
Serial.println("closing connection");
http.end();
}
Youtube to See How to Config Domoticz
Some Files of the Whole Project
www.spiketech.be for al my projects