From 4e1e32dbd006a39365b635fceb48616a47e7f9b8 Mon Sep 17 00:00:00 2001 From: Florian Knodt Date: Sat, 27 Jul 2019 14:16:55 +0200 Subject: [PATCH] Fixed examples and README --- README.md | 8 + examples/bz40i_live_page/bz40i_live_page.ino | 163 ++-- examples/bz40i_live_page/index_page.h | 804 +++++++++++++++++-- examples/bz40i_simple/bz40i_simple.ino | 114 ++- 4 files changed, 923 insertions(+), 166 deletions(-) diff --git a/README.md b/README.md index 296bd90..fb4b8e6 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ NOTE for Hardware Serial mode: to force the Hardware Serial mode,
user must edit the corresponding entry in [BZ40i_Config_User.h](https://github.com/adlerweb/BZ40i_Energy_Meter/blob/master/BZ40i_Config_User.h#L15) file.
adding #define USE_HARDWARESERIAL to the main ino file is not enough.
+Keep in mind you need to set your BZ40i to RTU-mode with the same baudrate as defined here, for example "BAU r4.8k". + --- ### Initializing: ### @@ -171,6 +173,12 @@ bz40i.clearErrCount(); --- +### Notes: ### + +I wasn't able to get a proper timing with ESP8266 and SoftwareSerial 5.x. If you run into similar problems try downgrading to Version 4.0. Also keep in mind a lower baudrate is much more likely to work. + +--- + ### Credits: ### :+1: SDM_Energy_Meter library by Reaper7 (https://github.com/reaper7/SDM_Energy_Meter)
diff --git a/examples/bz40i_live_page/bz40i_live_page.ino b/examples/bz40i_live_page/bz40i_live_page.ino index a0d0fa6..38e8a21 100644 --- a/examples/bz40i_live_page/bz40i_live_page.ino +++ b/examples/bz40i_live_page/bz40i_live_page.ino @@ -1,25 +1,17 @@ -//sdm live page example by reaper7 - -#define READSDMEVERY 2000 //read sdm every 2000ms -#define NBREG 5 //number of sdm registers to read +/** + * BZ40i HTTP Example + * + * Live HTTP status page for BZ40i + * + * Requires: + * https://github.com/plerup/espsoftwareserial#4.0.0 + * https://github.com/me-no-dev/ESPAsyncTCP + * https://github.com/me-no-dev/ESPAsyncWebServer + */ + +#define READSDMEVERY 5000 //read BZ40i every 5s //#define USE_STATIC_IP -/* WEMOS D1 Mini - ______________________________ - | L T L T L T L T L T L T | - | | - RST| 1|TX HSer - A0| 3|RX HSer - D0|16 5|D1 - D5|14 4|D2 - D6|12 10kPUP_0|D3 -RX SSer/HSer swap D7|13 LED_10kPUP_2|D4 -TX SSer/HSer swap D8|15 |GND - 3V3|__ |5V - | | - |___________________________| -*/ - #include #include #include @@ -29,16 +21,20 @@ TX SSer/HSer swap D8|15 |GND #include //https://github.com/me-no-dev/ESPAsyncWebServer #include //import SoftwareSerial library (if used) -#include //https://github.com/reaper7/SDM_Energy_Meter +#include //https://github.com/adlerweb/BZ40i_Energy_Meter/ #include "index_page.h" + +#define NBREG 37 +#define DEBUG_SERIAL + //------------------------------------------------------------------------------ AsyncWebServer server(80); -SoftwareSerial swSerSDM(13, 15); //config SoftwareSerial (rx->pin13 / tx->pin15) (if used) +SoftwareSerial swSerBZ40i(D1, D2); //config SoftwareSerial (rx->D1 / tx->D2) -SDM sdm(swSerSDM, 9600, NOT_A_PIN); //SOFTWARE SERIAL -//SDM sdm(Serial, 9600, NOT_A_PIN, SERIAL_8N1, false); //HARDWARE SERIAL +BZ40i bz40i(swSerBZ40i, 4800, NOT_A_PIN); //SOFTWARE SERIAL +//BZ40i bz40i(Serial, 9600, NOT_A_PIN, SERIAL_8N1, false); //HARDWARE SERIAL //------------------------------------------------------------------------------ String devicename = "PWRMETER"; @@ -56,34 +52,66 @@ String lastresetreason = ""; unsigned long readtime; //------------------------------------------------------------------------------ -typedef volatile struct { - volatile float regvalarr; +typedef struct { + String dname; + float regvalarr; const uint16_t regarr; -} sdm_struct; - -volatile sdm_struct sdmarr[NBREG] = { - {0.00, SDM220T_VOLTAGE}, //V - {0.00, SDM220T_CURRENT}, //A - {0.00, SDM220T_POWER}, //W - {0.00, SDM220T_POWER_FACTOR}, //PF - {0.00, SDM220T_FREQUENCY}, //Hz + byte isSigned; +} bz40i_struct; + +bz40i_struct bz40iarr[NBREG] = { + {"BZ40i_U_SYSTEM", 0.00, BZ40i_U_SYSTEM, 0}, + {"BZ40i_U_LN_P1", 0.00, BZ40i_U_LN_P1, 0}, + {"BZ40i_U_LN_P2", 0.00, BZ40i_U_LN_P2, 0}, + {"BZ40i_U_LN_P3", 0.00, BZ40i_U_LN_P3, 0}, + {"BZ40i_U_LL_P12", 0.00, BZ40i_U_LL_P12, 0}, + {"BZ40i_U_LL_P23", 0.00, BZ40i_U_LL_P23, 0}, + {"BZ40i_U_LL_P31", 0.00, BZ40i_U_LL_P31, 0}, + {"BZ40i_I_SYSTEM", 0.00, BZ40i_I_SYSTEM, 1}, + {"BZ40i_I_P1", 0.00, BZ40i_I_P1, 1}, + {"BZ40i_I_P2", 0.00, BZ40i_I_P2, 1}, + {"BZ40i_I_P3", 0.00, BZ40i_I_P3, 1}, + {"BZ40i_I_N", 0.00, BZ40i_I_N, 0}, + {"BZ40i_PF_SYSTEM", 0.00, BZ40i_PF_SYSTEM, 1}, + {"BZ40i_PF_P1", 0.00, BZ40i_PF_P1, 1}, + {"BZ40i_PF_P2", 0.00, BZ40i_PF_P2, 1}, + {"BZ40i_PF_P3", 0.00, BZ40i_PF_P3, 1}, + {"BZ40i_S_SYSTEM", 0.00, BZ40i_S_SYSTEM, 1}, + {"BZ40i_S_P1", 0.00, BZ40i_S_P1, 1}, + {"BZ40i_S_P2", 0.00, BZ40i_S_P2, 1}, + {"BZ40i_S_P3", 0.00, BZ40i_S_P3, 1}, + {"BZ40i_P_SYSTEM", 0.00, BZ40i_P_SYSTEM, 1}, + {"BZ40i_P_P1", 0.00, BZ40i_P_P1, 1}, + {"BZ40i_P_P2", 0.00, BZ40i_P_P2, 1}, + {"BZ40i_P_P3", 0.00, BZ40i_P_P3, 1}, + {"BZ40i_Q_SYSTEM", 0.00, BZ40i_Q_SYSTEM, 1}, + {"BZ40i_Q_P1", 0.00, BZ40i_Q_P1, 1}, + {"BZ40i_Q_P2", 0.00, BZ40i_Q_P2, 1}, + {"BZ40i_Q_P3", 0.00, BZ40i_Q_P3, 1}, + {"BZ40i_F", 0.00, BZ40i_F, 2}, + {"BZ40i_IMPORT_P", 0.00, BZ40i_IMPORT_P, 0}, + {"BZ40i_IMPORT_Q_LAG", 0.00, BZ40i_IMPORT_Q_LAG, 0}, + {"BZ40i_IMPORT_Q_LEAD", 0.00, BZ40i_IMPORT_Q_LEAD, 0}, + {"BZ40i_IMPORT_S", 0.00, BZ40i_IMPORT_S, 0}, + {"BZ40i_EXPORT_P", 0.00, BZ40i_EXPORT_P, 0}, + {"BZ40i_EXPORT_Q_LAG", 0.00, BZ40i_EXPORT_Q_LAG, 0}, + {"BZ40i_EXPORT_Q_LEAD", 0.00, BZ40i_EXPORT_Q_LEAD, 0}, + {"BZ40i_EXPORT_S", 0.00, BZ40i_EXPORT_S, 0} }; //------------------------------------------------------------------------------ -void xmlrequest(AsyncWebServerRequest *request) { - String XML = F(""); +void jsonrequest(AsyncWebServerRequest *request) { + String json = F("{\n"); for (int i = 0; i < NBREG; i++) { - XML += ""; - XML += String(sdmarr[i].regvalarr,2); - XML += ""; + json += " \""; + json += bz40iarr[i].dname; + json += "\": " + String(bz40iarr[i].regvalarr,2) + ",\n"; } - XML += F(""); - XML += String(ESP.getFreeHeap()); - XML += F(""); - XML += F(""); - XML += lastresetreason; - XML += F(""); - XML += F(""); - request->send(200, "text/xml", XML); + json += F(" \"heap\": "); + json += String(ESP.getFreeHeap()); + json += F(",\n \"reset\": \""); + json += lastresetreason; + json += F("\"\n}"); + request->send(200, "application/json", json); } //------------------------------------------------------------------------------ void indexrequest(AsyncWebServerRequest *request) { @@ -118,15 +146,21 @@ void otaInit() { ledOff(); }); ArduinoOTA.begin(); + #ifdef DEBUG_SERIAL + Serial.println("OTA ready"); + #endif } //------------------------------------------------------------------------------ void serverInit() { server.on("/", HTTP_GET, indexrequest); - server.on("/xml", HTTP_PUT, xmlrequest); + server.on("/json", HTTP_GET, jsonrequest); server.onNotFound([](AsyncWebServerRequest *request){ request->send(404); }); server.begin(); + #ifdef DEBUG_SERIAL + Serial.println("HTTP ready"); + #endif } //------------------------------------------------------------------------------ static void wifiInit() { @@ -141,24 +175,39 @@ static void wifiInit() { ledSwap(); delay(100); } + + #ifdef DEBUG_SERIAL + Serial.print("Connected to "); + Serial.print(wifi_ssid); + Serial.print(" using legacy IP "); + Serial.println(WiFi.localIP()); + #endif } //------------------------------------------------------------------------------ -void sdmRead() { - float tmpval = NAN; +void bz40iRead() { + float temp = NAN; for (uint8_t i = 0; i < NBREG; i++) { - tmpval = sdm.readVal(sdmarr[i].regarr); + temp = bz40i.readVal(bz40iarr[i].regarr, bz40iarr[i].isSigned); - if (isnan(tmpval)) - sdmarr[i].regvalarr = 0.00; - else - sdmarr[i].regvalarr = tmpval; + if(!isnan(temp)) { + bz40iarr[i].regvalarr = temp; + #ifdef DEBUG_SERIAL + Serial.print(bz40iarr[i].dname); + Serial.print(" -> "); + Serial.println(bz40iarr[i].regvalarr); + #endif + } yield(); } } //------------------------------------------------------------------------------ void setup() { + #ifdef DEBUG_SERIAL + Serial.begin(115200); + Serial.println("\nBZ40i HTTP example"); + #endif pinMode(LED_BUILTIN, OUTPUT); ledOn(); @@ -167,7 +216,7 @@ void setup() { wifiInit(); otaInit(); serverInit(); - sdm.begin(); + bz40i.begin(); readtime = millis(); @@ -178,7 +227,7 @@ void loop() { ArduinoOTA.handle(); if (millis() - readtime >= READSDMEVERY) { - sdmRead(); + bz40iRead(); readtime = millis(); } diff --git a/examples/bz40i_live_page/index_page.h b/examples/bz40i_live_page/index_page.h index 60acd7a..1aa17ee 100644 --- a/examples/bz40i_live_page/index_page.h +++ b/examples/bz40i_live_page/index_page.h @@ -1,70 +1,738 @@ const char index_page[] PROGMEM = R"=====( - - - - - SDM live POWER table - - - - -
-

SDM live POWER table

- - - - - - - - -
VOLTAGEV
CURRENTA
POWERW
POWER FACTORPF
FREQUENCYHz
FREE HEAPbytes
LAST RESET REASON
-
- - + + + + POWER METER + + + + + + + +

POWER METER

+
+
+

Netzspannung L-N

+ + + +
??.?? V
+
+ + + +
??.?? V
+
+ + + +
??.?? V
+
+ +
+
+

Netzspannung L-L

+ + + +
??.?? V
+
+ + + +
??.?? V
+
+ + + +
??.?? V
+
+ +
+
+

Strom

+ + + +
??.?? A
+
+ + + +
??.?? A
+
+ + + +
??.?? A
+
+ + + +
??.?? A
+
+ +
+
+

Wirkleistung

+ + + +
??.?? W
+
+ + + +
??.?? W
+
+ + + +
??.?? W
+
+ + + +
??.?? W
+
+ +
+
+

Scheinleistung

+ + + +
??.?? VA
+
+ + + +
??.?? VA
+
+ + + +
??.?? VA
+
+ + + +
??.?? VA
+
+ +
+
+

Blindleistung

+ + + +
??.?? VAr
+
+ + + +
??.?? VAr
+
+ + + +
??.?? VAr
+
+ + + +
??.?? VAr
+
+ +
+
+

Leistungsfaktor

+ + + +
??.??
+
+ + + +
??.??
+
+ + + +
??.??
+
+ + + +
??.??
+
+ +
+
+

Netzfrequenz

+ + +
??.?? Hz (123)
+
+ +
+
+

Stromimport

+ +
??.?? Wh
+ +
??.?? VArh
+ +
??.?? VArh
+ +
??.?? VAh
+
+
+

Stromexport

+ +
??.?? Wh
+ +
??.?? VArh
+ +
??.?? VArh
+ +
??.?? VAh
+
+
+

Konfiguration

+
    +
  • + Netzspannung L-N +
      +
    • L1
    • +
    • L2
    • +
    • L3
    • +
    • Graph
    • +
    +
  • +
  • + Netzspannung L-L +
      +
    • L1-L2
    • +
    • L2-L3
    • +
    • L3-L1
    • +
    • Graph
    • +
    +
  • +
  • + Strom +
      +
    • L1
    • +
    • L2
    • +
    • L3
    • +
    • N
    • +
    • Graph
    • +
    +
  • +
  • + Wirkleistung +
      +
    • L1
    • +
    • L2
    • +
    • L3
    • +
    • +
    • Graph
    • +
    +
  • +
  • + Scheinleistung +
      +
    • L1
    • +
    • L2
    • +
    • L3
    • +
    • +
    • Graph
    • +
    +
  • +
  • + Blindleistung +
      +
    • L1
    • +
    • L2
    • +
    • L3
    • +
    • +
    • Graph
    • +
    +
  • +
  • + Leistungsfaktor +
      +
    • L1
    • +
    • L2
    • +
    • L3
    • +
    • Ø
    • +
    • Graph
    • +
    +
  • +
  • + Netzfrequenz +
      +
    • Graph
    • +
    +
  • +
  • + Stromimport +
      +
    • Preis pro kWh:
    • +
    +
  • +
  • + Stromexport +
      +
    • Preis pro kWh:
    • +
    +
  • +
  • + System +
      +
    • Version:
    • +
    • Automatische Updates alle: Sekunden
    • +
    • Diagrammhistorie: Datenpunkte
    • +
    +
  • +
+
+
+ + Update: Unknown + + + + )====="; diff --git a/examples/bz40i_simple/bz40i_simple.ino b/examples/bz40i_simple/bz40i_simple.ino index e6f0974..07d3240 100644 --- a/examples/bz40i_simple/bz40i_simple.ino +++ b/examples/bz40i_simple/bz40i_simple.ino @@ -1,57 +1,89 @@ -/* WEMOS D1 Mini - ______________________________ - | L T L T L T L T L T L T | - | | - RST| 1|TX HSer - A0| 3|RX HSer - D0|16 5|D1 - D5|14 4|D2 - D6|12 10kPUP_0|D3 -RX SSer/HSer swap D7|13 LED_10kPUP_2|D4 -TX SSer/HSer swap D8|15 |GND - 3V3|__ |5V - | | - |___________________________| -*/ - +/** + * Simple BZ40i Example + * + * Dump measurements to serial console + * + * Requires: + * https://github.com/plerup/espsoftwareserial#4.0.0 + */ #include //import SoftwareSerial library -#include //import SDM library +#include //import BZ40i library + +SoftwareSerial swSerBZ40i(D1, D2); //config SoftwareSerial (rx->D1 / tx->D2) + +BZ40i bz40i(swSerBZ40i, 4800, NOT_A_PIN); //config BZ40i + +#define NBREG 37 -SoftwareSerial swSerSDM(13, 15); //config SoftwareSerial (rx->pin13 / tx->pin15) +typedef struct { + String dname; + const uint16_t regarr; + byte isSigned; +} bz40i_struct; -SDM sdm(swSerSDM, 9600, NOT_A_PIN); //config SDM +bz40i_struct bz40iarr[NBREG] = { + {"BZ40i_U_SYSTEM", BZ40i_U_SYSTEM, 0}, + {"BZ40i_U_LN_P1", BZ40i_U_LN_P1, 0}, + {"BZ40i_U_LN_P2", BZ40i_U_LN_P2, 0}, + {"BZ40i_U_LN_P3", BZ40i_U_LN_P3, 0}, + {"BZ40i_U_LL_P12", BZ40i_U_LL_P12, 0}, + {"BZ40i_U_LL_P23", BZ40i_U_LL_P23, 0}, + {"BZ40i_U_LL_P31", BZ40i_U_LL_P31, 0}, + {"BZ40i_I_SYSTEM", BZ40i_I_SYSTEM, 1}, + {"BZ40i_I_P1", BZ40i_I_P1, 1}, + {"BZ40i_I_P2", BZ40i_I_P2, 1}, + {"BZ40i_I_P3", BZ40i_I_P3, 1}, + {"BZ40i_I_N", BZ40i_I_N, 0}, + {"BZ40i_PF_SYSTEM", BZ40i_PF_SYSTEM, 1}, + {"BZ40i_PF_P1", BZ40i_PF_P1, 1}, + {"BZ40i_PF_P2", BZ40i_PF_P2, 1}, + {"BZ40i_PF_P3", BZ40i_PF_P3, 1}, + {"BZ40i_S_SYSTEM", BZ40i_S_SYSTEM, 1}, + {"BZ40i_S_P1", BZ40i_S_P1, 1}, + {"BZ40i_S_P2", BZ40i_S_P2, 1}, + {"BZ40i_S_P3", BZ40i_S_P3, 1}, + {"BZ40i_P_SYSTEM", BZ40i_P_SYSTEM, 1}, + {"BZ40i_P_P1", BZ40i_P_P1, 1}, + {"BZ40i_P_P2", BZ40i_P_P2, 1}, + {"BZ40i_P_P3", BZ40i_P_P3, 1}, + {"BZ40i_Q_SYSTEM", BZ40i_Q_SYSTEM, 1}, + {"BZ40i_Q_P1", BZ40i_Q_P1, 1}, + {"BZ40i_Q_P2", BZ40i_Q_P2, 1}, + {"BZ40i_Q_P3", BZ40i_Q_P3, 1}, + {"BZ40i_F", BZ40i_F, 2}, + {"BZ40i_IMPORT_P", BZ40i_IMPORT_P, 0}, + {"BZ40i_IMPORT_Q_LAG", BZ40i_IMPORT_Q_LAG, 0}, + {"BZ40i_IMPORT_Q_LEAD", BZ40i_IMPORT_Q_LEAD, 0}, + {"BZ40i_IMPORT_S", BZ40i_IMPORT_S, 0}, + {"BZ40i_EXPORT_P", BZ40i_EXPORT_P, 0}, + {"BZ40i_EXPORT_Q_LAG", BZ40i_EXPORT_Q_LAG, 0}, + {"BZ40i_EXPORT_Q_LEAD", BZ40i_EXPORT_Q_LEAD, 0}, + {"BZ40i_EXPORT_S", BZ40i_EXPORT_S, 0} +}; void setup() { Serial.begin(115200); //initialize serial - sdm.begin(); //initialize SDM communication + bz40i.begin(); //initialize BZ40i communication } void loop() { - char bufout[10]; - sprintf(bufout, "%c[1;0H", 27); - Serial.print(bufout); - - Serial.print("Voltage: "); - Serial.print(sdm.readVal(SDM220T_VOLTAGE), 2); //display voltage - Serial.println("V"); - - delay(50); - - Serial.print("Current: "); - Serial.print(sdm.readVal(SDM220T_CURRENT), 2); //display current - Serial.println("A"); + float temp = NAN; + Serial.println("Reading..."); - delay(50); + for (uint8_t i = 0; i < NBREG; i++) { + Serial.print(" ADDR: 0x"); + Serial.print(bz40iarr[i].regarr, 16); + temp = bz40i.readVal(bz40iarr[i].regarr, bz40iarr[i].isSigned); - Serial.print("Power: "); - Serial.print(sdm.readVal(SDM220T_POWER), 2); //display power - Serial.println("W"); + Serial.print(" = "); + Serial.print(temp); - delay(50); + Serial.print(" ("); + Serial.print(bz40iarr[i].dname); + Serial.println(")"); - Serial.print("Frequency: "); - Serial.print(sdm.readVal(SDM220T_FREQUENCY), 2); //display frequency - Serial.println("Hz"); + yield(); + } delay(1000); //wait a while before next loop }