Compare commits

...

13 Commits

Author SHA1 Message Date
Gunnar Skjold
4b32af69a7 Fixed typo 2020-02-18 07:37:42 +01:00
Gunnar Skjold
fc42fb22d8 Merge pull request #33 from gskjold/dev-v1.0.1
Dev v1.0.1
2020-02-16 14:49:36 +01:00
Gunnar Skjold
4e24e2949a Updated featheresp32 config to use Serial2 for Mbus 2020-02-15 18:01:10 +01:00
Gunnar Skjold
b2174dd521 If debug mode and HW_ROARFRED, use software serial to set upp RX for HAN with 2400 and TX for debugging with 115200 2020-02-14 20:14:30 +01:00
Gunnar Skjold
fe79a96827 Added change to allow auto modem sleep 2020-02-14 19:52:15 +01:00
Gunnar Skjold
554cc7c023 Added date and time from meter as rtc in json 2020-02-14 16:03:52 +01:00
Gunnar Skjold
d2f93c07bd Not sure if this does anything, but increased json and mqtt limits to see if Kamstrup list 3 is received 2020-02-13 20:50:13 +01:00
Gunnar Skjold
b6efae656f Expose export power in JSON 2020-02-12 16:40:51 +01:00
Gunnar Skjold
fa0f93cefe Switched to reactive import for Q 2020-02-12 13:25:36 +01:00
Gunnar Skjold
058dc658c1 Switched to reactive import for Q 2020-02-12 13:16:49 +01:00
Gunnar Skjold
4b9cb27e58 Disable web security if AP mode is triggered 2020-02-11 17:32:18 +01:00
Gunnar Skjold
5f14517258 Minor changes after testing MQTT reconnect 2020-02-11 17:25:52 +01:00
Gunnar Skjold
ad820fff01 Allow other services to get resources while trying to connect to mqtt 2020-02-11 11:04:37 +01:00
6 changed files with 105 additions and 106 deletions

View File

@@ -27,7 +27,7 @@ You can also use a ESP based development board and combine this with a M-Bus mod
- Jump GPIO4 to GND to force AP mode during boot - Jump GPIO4 to GND to force AP mode during boot
[Adafruit HUZZAH32](https://www.adafruit.com/product/3405) [Adafruit HUZZAH32](https://www.adafruit.com/product/3405)
- M-Bus connected to RX - M-Bus connected to GPIO16
Combine one of above board with an M-Bus module. Connect 3.3v and GND together between the boards and connect the TX pin from the M-Bus board to the dedicated M-Bus pin on the ESP board. Combine one of above board with an M-Bus module. Connect 3.3v and GND together between the boards and connect the TX pin from the M-Bus board to the dedicated M-Bus pin on the ESP board.

View File

@@ -10,9 +10,11 @@ static void hanToJsonKaifa3phase(int listSize, JsonObject& data, HanReader& hanR
{ {
data["lv"] = hanReader.getString( (int)Kaifa_List3Phase::ListVersionIdentifier); data["lv"] = hanReader.getString( (int)Kaifa_List3Phase::ListVersionIdentifier);
data["id"] = hanReader.getString( (int)Kaifa_List3Phase::MeterID); data["id"] = hanReader.getString( (int)Kaifa_List3Phase::MeterID);
data["type"] = hanReader.getString( (int)Kaifa_List3Phase::MeterType); data["type"] = hanReader.getString( (int)Kaifa_List3Phase::MeterType);
data["P"] = hanReader.getInt( (int)Kaifa_List3Phase::ActiveImportPower); data["P"] = hanReader.getInt( (int)Kaifa_List3Phase::ActiveImportPower);
data["Q"] = hanReader.getInt( (int)Kaifa_List3Phase::ReactiveImportPower); data["Q"] = hanReader.getInt( (int)Kaifa_List3Phase::ReactiveImportPower);
data["PO"] = hanReader.getInt( (int)Kaifa_List3Phase::ActiveExportPower);
data["QO"] = hanReader.getInt( (int)Kaifa_List3Phase::ReactiveExportPower);
data["I1"] = ((double) hanReader.getInt((int)Kaifa_List3Phase::CurrentL1)) / 1000; data["I1"] = ((double) hanReader.getInt((int)Kaifa_List3Phase::CurrentL1)) / 1000;
data["I2"] = ((double) hanReader.getInt((int)Kaifa_List3Phase::CurrentL2)) / 1000; data["I2"] = ((double) hanReader.getInt((int)Kaifa_List3Phase::CurrentL2)) / 1000;
data["I3"] = ((double) hanReader.getInt((int)Kaifa_List3Phase::CurrentL3)) / 1000; data["I3"] = ((double) hanReader.getInt((int)Kaifa_List3Phase::CurrentL3)) / 1000;
@@ -23,6 +25,7 @@ static void hanToJsonKaifa3phase(int listSize, JsonObject& data, HanReader& hanR
if (listSize >= (int)Kaifa::List3PhaseLong) if (listSize >= (int)Kaifa::List3PhaseLong)
{ {
data["rtc"] = hanReader.getTime( (int)Kaifa_List3Phase::MeterClock);
data["tPI"] = hanReader.getInt( (int)Kaifa_List3Phase::CumulativeActiveImportEnergy); data["tPI"] = hanReader.getInt( (int)Kaifa_List3Phase::CumulativeActiveImportEnergy);
data["tPO"] = hanReader.getInt( (int)Kaifa_List3Phase::CumulativeActiveExportEnergy); data["tPO"] = hanReader.getInt( (int)Kaifa_List3Phase::CumulativeActiveExportEnergy);
data["tQI"] = hanReader.getInt( (int)Kaifa_List3Phase::CumulativeReactiveImportEnergy); data["tQI"] = hanReader.getInt( (int)Kaifa_List3Phase::CumulativeReactiveImportEnergy);
@@ -39,12 +42,15 @@ static void hanToJsonKaifa1phase(int listSize, JsonObject& data, HanReader& hanR
data["type"] = hanReader.getString( (int)Kaifa_List1Phase::MeterType); data["type"] = hanReader.getString( (int)Kaifa_List1Phase::MeterType);
data["P"] = hanReader.getInt( (int)Kaifa_List1Phase::ActiveImportPower); data["P"] = hanReader.getInt( (int)Kaifa_List1Phase::ActiveImportPower);
data["Q"] = hanReader.getInt( (int)Kaifa_List1Phase::ReactiveImportPower); data["Q"] = hanReader.getInt( (int)Kaifa_List1Phase::ReactiveImportPower);
data["PO"] = hanReader.getInt( (int)Kaifa_List1Phase::ActiveExportPower);
data["QO"] = hanReader.getInt( (int)Kaifa_List1Phase::ReactiveExportPower);
data["I1"] = ((double) hanReader.getInt((int)Kaifa_List1Phase::CurrentL1)) / 1000; data["I1"] = ((double) hanReader.getInt((int)Kaifa_List1Phase::CurrentL1)) / 1000;
data["U1"] = ((double) hanReader.getInt((int)Kaifa_List1Phase::VoltageL1)) / 10; data["U1"] = ((double) hanReader.getInt((int)Kaifa_List1Phase::VoltageL1)) / 10;
} }
if (listSize >= (int)Kaifa::List1PhaseLong) if (listSize >= (int)Kaifa::List1PhaseLong)
{ {
data["rtc"] = hanReader.getTime( (int)Kaifa_List1Phase::MeterClock);
data["tPI"] = hanReader.getInt( (int)Kaifa_List1Phase::CumulativeActiveImportEnergy); data["tPI"] = hanReader.getInt( (int)Kaifa_List1Phase::CumulativeActiveImportEnergy);
data["tPO"] = hanReader.getInt( (int)Kaifa_List1Phase::CumulativeActiveExportEnergy); data["tPO"] = hanReader.getInt( (int)Kaifa_List1Phase::CumulativeActiveExportEnergy);
data["tQI"] = hanReader.getInt( (int)Kaifa_List1Phase::CumulativeReactiveImportEnergy); data["tQI"] = hanReader.getInt( (int)Kaifa_List1Phase::CumulativeReactiveImportEnergy);
@@ -77,15 +83,15 @@ static void hanToJsonKaifa(JsonObject& data, HanReader& hanReader, Stream *debug
} }
static void hanToJsonAidon3phase(int listSize, JsonObject& data, HanReader& hanReader, Stream *debugger) static void hanToJsonAidon3phase(int listSize, JsonObject& data, HanReader& hanReader, Stream *debugger) {
{ if (listSize >= (int)Aidon::List3PhaseShort) {
if (listSize >= (int)Aidon::List3PhaseShort)
{
data["lv"] = hanReader.getString( (int)Aidon_List3Phase::ListVersionIdentifier); data["lv"] = hanReader.getString( (int)Aidon_List3Phase::ListVersionIdentifier);
data["id"] = hanReader.getString( (int)Aidon_List3Phase::MeterID); data["id"] = hanReader.getString( (int)Aidon_List3Phase::MeterID);
data["type"] = hanReader.getString( (int)Aidon_List3Phase::MeterType); data["type"] = hanReader.getString( (int)Aidon_List3Phase::MeterType);
data["P"] = hanReader.getInt( (int)Aidon_List3Phase::ActiveImportPower); data["P"] = hanReader.getInt( (int)Aidon_List3Phase::ActiveImportPower);
data["Q"] = hanReader.getInt( (int)Aidon_List3Phase::ReactiveExportPower); data["Q"] = hanReader.getInt( (int)Aidon_List3Phase::ReactiveImportPower);
data["PO"] = hanReader.getInt( (int)Aidon_List3Phase::ActiveExportPower);
data["QO"] = hanReader.getInt( (int)Aidon_List3Phase::ReactiveExportPower);
data["I1"] = ((double) hanReader.getInt( (int)Aidon_List3Phase::CurrentL1)) / 10; data["I1"] = ((double) hanReader.getInt( (int)Aidon_List3Phase::CurrentL1)) / 10;
data["I2"] = ((double) hanReader.getInt( (int)Aidon_List3Phase::CurrentL2)) / 10; data["I2"] = ((double) hanReader.getInt( (int)Aidon_List3Phase::CurrentL2)) / 10;
data["I3"] = ((double) hanReader.getInt( (int)Aidon_List3Phase::CurrentL3)) / 10; data["I3"] = ((double) hanReader.getInt( (int)Aidon_List3Phase::CurrentL3)) / 10;
@@ -94,8 +100,8 @@ static void hanToJsonAidon3phase(int listSize, JsonObject& data, HanReader& hanR
data["U3"] = ((double) hanReader.getInt( (int)Aidon_List3Phase::VoltageL3)) / 10; data["U3"] = ((double) hanReader.getInt( (int)Aidon_List3Phase::VoltageL3)) / 10;
} }
if (listSize >= (int)Aidon::List3PhaseLong) if (listSize >= (int)Aidon::List3PhaseLong) {
{ data["rtc"] = hanReader.getTime( (int)Aidon_List3Phase::Timestamp);
data["tPI"] = hanReader.getInt( (int)Aidon_List3Phase::CumulativeActiveImportEnergy); data["tPI"] = hanReader.getInt( (int)Aidon_List3Phase::CumulativeActiveImportEnergy);
data["tPO"] = hanReader.getInt( (int)Aidon_List3Phase::CumulativeActiveExportEnergy); data["tPO"] = hanReader.getInt( (int)Aidon_List3Phase::CumulativeActiveExportEnergy);
data["tQI"] = hanReader.getInt( (int)Aidon_List3Phase::CumulativeReactiveImportEnergy); data["tQI"] = hanReader.getInt( (int)Aidon_List3Phase::CumulativeReactiveImportEnergy);
@@ -111,13 +117,16 @@ static void hanToJsonAidon1phase(int listSize, JsonObject& data, HanReader& hanR
data["id"] = hanReader.getString( (int)Aidon_List1Phase::MeterID); data["id"] = hanReader.getString( (int)Aidon_List1Phase::MeterID);
data["type"] = hanReader.getString( (int)Aidon_List1Phase::MeterType); data["type"] = hanReader.getString( (int)Aidon_List1Phase::MeterType);
data["P"] = hanReader.getInt( (int)Aidon_List1Phase::ActiveImportPower); data["P"] = hanReader.getInt( (int)Aidon_List1Phase::ActiveImportPower);
data["Q"] = hanReader.getInt( (int)Aidon_List1Phase::ReactiveExportPower); data["Q"] = hanReader.getInt( (int)Aidon_List1Phase::ReactiveImportPower);
data["PO"] = hanReader.getInt( (int)Aidon_List1Phase::ActiveExportPower);
data["QO"] = hanReader.getInt( (int)Aidon_List1Phase::ReactiveExportPower);
data["I1"] = ((double) hanReader.getInt( (int)Aidon_List1Phase::CurrentL1)) / 10; data["I1"] = ((double) hanReader.getInt( (int)Aidon_List1Phase::CurrentL1)) / 10;
data["U1"] = ((double) hanReader.getInt( (int)Aidon_List1Phase::VoltageL1)) / 10; data["U1"] = ((double) hanReader.getInt( (int)Aidon_List1Phase::VoltageL1)) / 10;
} }
if (listSize >= (int)Aidon::List1PhaseLong) if (listSize >= (int)Aidon::List1PhaseLong)
{ {
data["rtc"] = hanReader.getTime( (int)Aidon_List1Phase::Timestamp);
data["tPI"] = hanReader.getInt( (int)Aidon_List1Phase::CumulativeActiveImportEnergy); data["tPI"] = hanReader.getInt( (int)Aidon_List1Phase::CumulativeActiveImportEnergy);
data["tPO"] = hanReader.getInt( (int)Aidon_List1Phase::CumulativeActiveExportEnergy); data["tPO"] = hanReader.getInt( (int)Aidon_List1Phase::CumulativeActiveExportEnergy);
data["tQI"] = hanReader.getInt( (int)Aidon_List1Phase::CumulativeReactiveImportEnergy); data["tQI"] = hanReader.getInt( (int)Aidon_List1Phase::CumulativeReactiveImportEnergy);
@@ -132,25 +141,21 @@ static void hanToJsonAidon3phaseIT(int listSize, JsonObject& data, HanReader& ha
data["lv"] = hanReader.getString( (int)Aidon_List3PhaseIT::ListVersionIdentifier); data["lv"] = hanReader.getString( (int)Aidon_List3PhaseIT::ListVersionIdentifier);
data["id"] = hanReader.getString( (int)Aidon_List3PhaseIT::MeterID); data["id"] = hanReader.getString( (int)Aidon_List3PhaseIT::MeterID);
data["type"] = hanReader.getString( (int)Aidon_List3PhaseIT::MeterType); data["type"] = hanReader.getString( (int)Aidon_List3PhaseIT::MeterType);
int p = hanReader.getInt( (int)Aidon_List3PhaseIT::ActiveImportPower); data["P"] = hanReader.getInt( (int)Aidon_List3PhaseIT::ActiveImportPower);
data["P"] = p; data["Q"] = hanReader.getInt( (int)Aidon_List3PhaseIT::ReactiveImportPower);
data["Q"] = hanReader.getInt( (int)Aidon_List3PhaseIT::ReactiveExportPower); data["PO"] = hanReader.getInt( (int)Aidon_List3PhaseIT::ActiveExportPower);
double i1 = ((double) hanReader.getInt( (int)Aidon_List3PhaseIT::CurrentL1)) / 10; data["QO"] = hanReader.getInt( (int)Aidon_List3PhaseIT::ReactiveExportPower);
double i3 = ((double) hanReader.getInt( (int)Aidon_List3PhaseIT::CurrentL3)) / 10; data["I1"] = ((double) hanReader.getInt( (int)Aidon_List3PhaseIT::CurrentL1)) / 10;
double u1 = ((double) hanReader.getInt( (int)Aidon_List3PhaseIT::VoltageL1)) / 10; data["I2"] = 0;
double u2 = ((double) hanReader.getInt( (int)Aidon_List3PhaseIT::VoltageL2)) / 10; data["I3"] = ((double) hanReader.getInt( (int)Aidon_List3PhaseIT::CurrentL3)) / 10;
double u3 = ((double) hanReader.getInt( (int)Aidon_List3PhaseIT::VoltageL3)) / 10; data["U1"] = ((double) hanReader.getInt( (int)Aidon_List3PhaseIT::VoltageL1)) / 10;
double i2 = 0.00; data["U2"] = ((double) hanReader.getInt( (int)Aidon_List3PhaseIT::VoltageL2)) / 10;
data["I1"] = i1; data["U3"] = ((double) hanReader.getInt( (int)Aidon_List3PhaseIT::VoltageL3)) / 10;
data["I2"] = i2;
data["I3"] = i3;
data["U1"] = u1;
data["U2"] = u2;
data["U3"] = u3;
} }
if (listSize >= (int)Aidon::List3PhaseITLong) if (listSize >= (int)Aidon::List3PhaseITLong)
{ {
data["rtc"] = hanReader.getTime( (int)Aidon_List3PhaseIT::Timestamp);
data["tPI"] = hanReader.getInt( (int)Aidon_List3PhaseIT::CumulativeActiveImportEnergy); data["tPI"] = hanReader.getInt( (int)Aidon_List3PhaseIT::CumulativeActiveImportEnergy);
data["tPO"] = hanReader.getInt( (int)Aidon_List3PhaseIT::CumulativeActiveExportEnergy); data["tPO"] = hanReader.getInt( (int)Aidon_List3PhaseIT::CumulativeActiveExportEnergy);
data["tQI"] = hanReader.getInt( (int)Aidon_List3PhaseIT::CumulativeReactiveImportEnergy); data["tQI"] = hanReader.getInt( (int)Aidon_List3PhaseIT::CumulativeReactiveImportEnergy);
@@ -187,15 +192,15 @@ static void hanToJsonAidon(JsonObject& data, HanReader& hanReader, Stream *debug
} }
} }
static void hanToJsonKamstrup3phase(int listSize, JsonObject& data, HanReader& hanReader, Stream *debugger) static void hanToJsonKamstrup3phase(int listSize, JsonObject& data, HanReader& hanReader, Stream *debugger) {
{ if (listSize >= (int)Kamstrup::List3PhaseShort) {
if (listSize >= (int)Kamstrup::List3PhaseShort)
{
data["lv"] = hanReader.getString( (int)Kamstrup_List3Phase::ListVersionIdentifier); data["lv"] = hanReader.getString( (int)Kamstrup_List3Phase::ListVersionIdentifier);
data["id"] = hanReader.getString( (int)Kamstrup_List3Phase::MeterID); data["id"] = hanReader.getString( (int)Kamstrup_List3Phase::MeterID);
data["type"] = hanReader.getString( (int)Kamstrup_List3Phase::MeterType); data["type"] = hanReader.getString( (int)Kamstrup_List3Phase::MeterType);
data["P"] = hanReader.getInt( (int)Kamstrup_List3Phase::ActiveImportPower); data["P"] = hanReader.getInt( (int)Kamstrup_List3Phase::ActiveImportPower);
data["Q"] = hanReader.getInt( (int)Kamstrup_List3Phase::ReactiveImportPower); data["Q"] = hanReader.getInt( (int)Kamstrup_List3Phase::ReactiveImportPower);
data["PO"] = hanReader.getInt( (int)Kamstrup_List3Phase::ActiveExportPower);
data["QO"] = hanReader.getInt( (int)Kamstrup_List3Phase::ReactiveExportPower);
data["I1"] = ((double) hanReader.getInt((int)Kamstrup_List3Phase::CurrentL1)) / 100; data["I1"] = ((double) hanReader.getInt((int)Kamstrup_List3Phase::CurrentL1)) / 100;
data["I2"] = ((double) hanReader.getInt((int)Kamstrup_List3Phase::CurrentL2)) / 100; data["I2"] = ((double) hanReader.getInt((int)Kamstrup_List3Phase::CurrentL2)) / 100;
data["I3"] = ((double) hanReader.getInt((int)Kamstrup_List3Phase::CurrentL3)) / 100; data["I3"] = ((double) hanReader.getInt((int)Kamstrup_List3Phase::CurrentL3)) / 100;
@@ -204,8 +209,8 @@ static void hanToJsonKamstrup3phase(int listSize, JsonObject& data, HanReader& h
data["U3"] = hanReader.getInt( (int)Kamstrup_List3Phase::VoltageL3); data["U3"] = hanReader.getInt( (int)Kamstrup_List3Phase::VoltageL3);
} }
if (listSize >= (int)Kamstrup::List3PhaseLong) if (listSize >= (int)Kamstrup::List3PhaseLong) {
{ data["rtc"] = hanReader.getTime( (int)Kamstrup_List3Phase::MeterClock);
data["tPI"] = hanReader.getInt( (int)Kamstrup_List3Phase::CumulativeActiveImportEnergy); data["tPI"] = hanReader.getInt( (int)Kamstrup_List3Phase::CumulativeActiveImportEnergy);
data["tPO"] = hanReader.getInt( (int)Kamstrup_List3Phase::CumulativeActiveExportEnergy); data["tPO"] = hanReader.getInt( (int)Kamstrup_List3Phase::CumulativeActiveExportEnergy);
data["tQI"] = hanReader.getInt( (int)Kamstrup_List3Phase::CumulativeReactiveImportEnergy); data["tQI"] = hanReader.getInt( (int)Kamstrup_List3Phase::CumulativeReactiveImportEnergy);
@@ -213,21 +218,21 @@ static void hanToJsonKamstrup3phase(int listSize, JsonObject& data, HanReader& h
} }
} }
static void hanToJsonKamstrup1phase(int listSize, JsonObject& data, HanReader& hanReader, Stream *debugger) static void hanToJsonKamstrup1phase(int listSize, JsonObject& data, HanReader& hanReader, Stream *debugger) {
{ if (listSize >= (int)Kamstrup::List1PhaseShort) {
if (listSize >= (int)Kamstrup::List1PhaseShort)
{
data["lv"] = hanReader.getString( (int)Kamstrup_List1Phase::ListVersionIdentifier); data["lv"] = hanReader.getString( (int)Kamstrup_List1Phase::ListVersionIdentifier);
data["id"] = hanReader.getString( (int)Kamstrup_List1Phase::MeterID); data["id"] = hanReader.getString( (int)Kamstrup_List1Phase::MeterID);
data["type"] = hanReader.getString( (int)Kamstrup_List1Phase::MeterType); data["type"] = hanReader.getString( (int)Kamstrup_List1Phase::MeterType);
data["P"] = hanReader.getInt( (int)Kamstrup_List1Phase::ActiveImportPower); data["P"] = hanReader.getInt( (int)Kamstrup_List1Phase::ActiveImportPower);
data["Q"] = hanReader.getInt( (int)Kamstrup_List1Phase::ReactiveImportPower); data["Q"] = hanReader.getInt( (int)Kamstrup_List1Phase::ReactiveImportPower);
data["PO"] = hanReader.getInt( (int)Kamstrup_List1Phase::ActiveExportPower);
data["QO"] = hanReader.getInt( (int)Kamstrup_List1Phase::ReactiveExportPower);
data["I1"] = ((double) hanReader.getInt((int)Kamstrup_List1Phase::CurrentL1)) / 100; data["I1"] = ((double) hanReader.getInt((int)Kamstrup_List1Phase::CurrentL1)) / 100;
data["U1"] = hanReader.getInt( (int)Kamstrup_List1Phase::VoltageL1); data["U1"] = hanReader.getInt( (int)Kamstrup_List1Phase::VoltageL1);
} }
if (listSize >= (int)Kamstrup::List1PhaseLong) if (listSize >= (int)Kamstrup::List1PhaseLong) {
{ data["rtc"] = hanReader.getTime( (int)Kamstrup_List1Phase::MeterClock);
data["tPI"] = hanReader.getInt( (int)Kamstrup_List1Phase::CumulativeActiveImportEnergy); data["tPI"] = hanReader.getInt( (int)Kamstrup_List1Phase::CumulativeActiveImportEnergy);
data["tPO"] = hanReader.getInt( (int)Kamstrup_List1Phase::CumulativeActiveExportEnergy); data["tPO"] = hanReader.getInt( (int)Kamstrup_List1Phase::CumulativeActiveExportEnergy);
data["tQI"] = hanReader.getInt( (int)Kamstrup_List1Phase::CumulativeReactiveImportEnergy); data["tQI"] = hanReader.getInt( (int)Kamstrup_List1Phase::CumulativeReactiveImportEnergy);
@@ -235,8 +240,7 @@ static void hanToJsonKamstrup1phase(int listSize, JsonObject& data, HanReader& h
} }
} }
static void hanToJsonKamstrup(JsonObject& data, HanReader& hanReader, Stream *debugger) static void hanToJsonKamstrup(JsonObject& data, HanReader& hanReader, Stream *debugger) {
{
int listSize = hanReader.getListSize(); int listSize = hanReader.getListSize();
switch (listSize) { switch (listSize) {

View File

@@ -17,7 +17,13 @@
#include <OneWire.h> #include <OneWire.h>
#define TEMP_SENSOR_PIN 5 // Temperature sensor connected to GPIO5 #define TEMP_SENSOR_PIN 5 // Temperature sensor connected to GPIO5
#if DEBUG_MODE
#define SOFTWARE_SERIAL 1
#include <SoftwareSerial.h>
SoftwareSerial *hanSerial = new SoftwareSerial(3);
#else
HardwareSerial *hanSerial = &Serial; HardwareSerial *hanSerial = &Serial;
#endif
// Build settings for Wemos Lolin D32 // Build settings for Wemos Lolin D32
#elif defined(ARDUINO_LOLIN_D32) #elif defined(ARDUINO_LOLIN_D32)
@@ -45,7 +51,7 @@ SoftwareSerial *hanSerial = new SoftwareSerial(D1);
#define LED_ACTIVE_HIGH 1 #define LED_ACTIVE_HIGH 1
#define AP_BUTTON_PIN INVALID_BUTTON_PIN #define AP_BUTTON_PIN INVALID_BUTTON_PIN
HardwareSerial *hanSerial = &Serial; HardwareSerial *hanSerial = &Serial2;
// Default build settings // Default build settings
#else #else

View File

@@ -24,10 +24,10 @@ AmsWebServer ws;
// WiFi client and MQTT client // WiFi client and MQTT client
WiFiClient *client; WiFiClient *client;
MQTTClient mqtt(384); MQTTClient mqtt(512);
// Object used for debugging // Object used for debugging
HardwareSerial* debugger = NULL; Stream* debugger = NULL;
// The HAN Port reader, used to read serial data and decode DLMS // The HAN Port reader, used to read serial data and decode DLMS
HanReader hanReader; HanReader hanReader;
@@ -39,17 +39,15 @@ void setup() {
} }
#if DEBUG_MODE #if DEBUG_MODE
debugger = &Serial; #if HW_ROARFRED
#if SOFTWARE_SERIAL SoftwareSerial *ser = new SoftwareSerial(-1, 1);
debugger->begin(115200, SERIAL_8N1); ser->begin(115200, SWSERIAL_8N1);
#else debugger = ser;
if(config.meterType == 3) { #else
hanSerial->begin(2400, SERIAL_8N1); HardwareSerial *ser = &Serial;
} else { ser->begin(115200, SERIAL_8N1);
hanSerial->begin(2400, SERIAL_8E1); #endif
} debugger = ser;
#endif
while (!&debugger);
#endif #endif
if (debugger) { if (debugger) {
@@ -72,12 +70,6 @@ void setup() {
{ {
setupWiFi(); setupWiFi();
if(config.mqttHost) {
mqtt.begin(config.mqttHost, *client);
// Notify everyone we're here!
sendMqttData("Connected!");
}
// Configure uart for AMS data // Configure uart for AMS data
#if defined SOFTWARE_SERIAL #if defined SOFTWARE_SERIAL
if(config.meterType == 3) { if(config.meterType == 3) {
@@ -118,15 +110,16 @@ void loop()
// Reconnect to WiFi and MQTT as needed // Reconnect to WiFi and MQTT as needed
if (WiFi.status() != WL_CONNECTED) { if (WiFi.status() != WL_CONNECTED) {
WiFi_connect(); WiFi_connect();
} } else {
if (config.mqttHost) {
if (config.mqttHost) { mqtt.loop();
mqtt.loop(); yield();
delay(10); // <- fixes some issues with WiFi stability if(!mqtt.connected()) {
if(!mqtt.connected()) { MQTT_connect();
MQTT_connect(); }
} }
} }
readHanPort(); readHanPort();
} }
else else
@@ -136,6 +129,7 @@ void loop()
else led_off(); else led_off();
} }
ws.loop(); ws.loop();
delay(1); // Needed for auto modem sleep
} }
@@ -207,7 +201,7 @@ void readHanPort()
if (debugger) debugger->println(time); if (debugger) debugger->println(time);
// Define a json object to keep the data // Define a json object to keep the data
StaticJsonDocument<500> json; StaticJsonDocument<1024> json;
// Any generic useful info here // Any generic useful info here
json["id"] = WiFi.macAddress(); json["id"] = WiFi.macAddress();
@@ -297,8 +291,18 @@ void WiFi_connect() {
// Function to connect and reconnect as necessary to the MQTT server. // Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting. // Should be called in the loop function and it will take care if connecting.
void MQTT_connect()
{ unsigned long lastMqttRetry = -10000;
void MQTT_connect() {
if(!config.mqttHost) {
if(debugger) debugger->println("No MQTT config");
return;
}
if(millis() - lastMqttRetry < 5000) {
yield();
return;
}
lastMqttRetry = millis();
if(debugger) { if(debugger) {
debugger->print("Connecting to MQTT: "); debugger->print("Connecting to MQTT: ");
debugger->print(config.mqttHost); debugger->print(config.mqttHost);
@@ -306,40 +310,30 @@ void MQTT_connect()
debugger->print(config.mqttPort); debugger->print(config.mqttPort);
debugger->println(); debugger->println();
} }
// Wait for the MQTT connection to complete
while (!mqtt.connected()) {
// Connect to a unsecure or secure MQTT server
if ((config.mqttUser == 0 && mqtt.connect(config.mqttClientID)) ||
(config.mqttUser != 0 && mqtt.connect(config.mqttClientID, config.mqttUser, config.mqttPass)))
{
if (debugger) debugger->println("\nSuccessfully connected to MQTT!");
// Subscribe to the chosen MQTT topic, if set in configuration mqtt.disconnect();
if (config.mqttSubscribeTopic != 0 && strlen(config.mqttSubscribeTopic) > 0)
{
mqtt.subscribe(config.mqttSubscribeTopic);
if (debugger) debugger->printf(" Subscribing to [%s]\r\n", config.mqttSubscribeTopic);
}
}
else
{
if (debugger)
{
debugger->print(".");
debugger->print("failed, ");
debugger->println(" trying again in 5 seconds");
}
// Wait 2 seconds before retrying mqtt.begin(config.mqttHost, config.mqttPort, *client);
mqtt.disconnect();
delay(2000); // Connect to a unsecure or secure MQTT server
if ((config.mqttUser == 0 && mqtt.connect(config.mqttClientID)) ||
(config.mqttUser != 0 && mqtt.connect(config.mqttClientID, config.mqttUser, config.mqttPass))) {
if (debugger) debugger->println("\nSuccessfully connected to MQTT!");
// Subscribe to the chosen MQTT topic, if set in configuration
if (config.mqttSubscribeTopic != 0 && strlen(config.mqttSubscribeTopic) > 0) {
mqtt.subscribe(config.mqttSubscribeTopic);
if (debugger) debugger->printf(" Subscribing to [%s]\r\n", config.mqttSubscribeTopic);
} }
// Allow some resources for the WiFi connection sendMqttData("Connected!");
yield(); } else {
delay(2000); if (debugger) {
debugger->print(" failed, ");
debugger->println(" trying again in 5 seconds");
}
} }
yield();
} }
// Send a simple string embedded in json over MQTT // Send a simple string embedded in json over MQTT
@@ -349,11 +343,6 @@ void sendMqttData(String data)
if (config.mqttPublishTopic == 0 || strlen(config.mqttPublishTopic) == 0) if (config.mqttPublishTopic == 0 || strlen(config.mqttPublishTopic) == 0)
return; return;
// Make sure we're connected
if (!client->connected() || !mqtt.connected()) {
MQTT_connect();
}
// Build a json with the message in a "data" attribute // Build a json with the message in a "data" attribute
StaticJsonDocument<500> json; StaticJsonDocument<500> json;
json["id"] = WiFi.macAddress(); json["id"] = WiFi.macAddress();

View File

@@ -41,7 +41,7 @@ void AmsWebServer::loop() {
server.handleClient(); server.handleClient();
} }
void AmsWebServer::setJson(StaticJsonDocument<500> json) { void AmsWebServer::setJson(StaticJsonDocument<1024> json) {
if(!json.isNull()) { if(!json.isNull()) {
p = json["data"]["P"].as<int>(); p = json["data"]["P"].as<int>();
@@ -87,7 +87,7 @@ void AmsWebServer::setJson(StaticJsonDocument<500> json) {
} }
bool AmsWebServer::checkSecurity(byte level) { bool AmsWebServer::checkSecurity(byte level) {
bool access = !config->hasConfig() || config->authSecurity < level; bool access = WiFi.getMode() == WIFI_AP || !config->hasConfig() || config->authSecurity < level;
if(!access && config->authSecurity >= level && server.hasHeader("Authorization")) { if(!access && config->authSecurity >= level && server.hasHeader("Authorization")) {
println(" forcing web security"); println(" forcing web security");
String expectedAuth = String(config->authUser) + ":" + String(config->authPass); String expectedAuth = String(config->authUser) + ":" + String(config->authPass);

View File

@@ -24,12 +24,12 @@ class AmsWebServer {
public: public:
void setup(configuration* config, Stream* debugger); void setup(configuration* config, Stream* debugger);
void loop(); void loop();
void setJson(StaticJsonDocument<500> json); void setJson(StaticJsonDocument<1024> json);
private: private:
configuration* config; configuration* config;
Stream* debugger; Stream* debugger;
StaticJsonDocument<500> json; StaticJsonDocument<1024> json;
int maxPwr; int maxPwr;
int p; int p;
double u1, u2, u3, i1, i2, i3; double u1, u2, u3, i1, i2, i3;