forked from roarfred/AmsToMqttBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESPDebugger.ino
191 lines (168 loc) · 5.63 KB
/
ESPDebugger.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Simple sketch to read MBus data from electrical meter
* As the protocol requires "Even" parity, and this is
* only supported on the hardware port of the ESP8266,
* we'll have to use Serial1 for debugging.
*
* This means you'll have to program the ESP using the
* regular RX/TX port, and then you must remove the FTDI
* and connect the MBus signal from the meter to the
* RS pin. The FTDI/RX can be moved to Pin2 for debugging
*
* Created 14. september 2017 by Roar Fredriksen
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "HanReader.h"
// The HAN Port reader
HanReader hanReader;
// WiFi and MQTT endpoints
const char* ssid = "Roar_Etne";
const char* password = "**********";
const char* mqtt_server = "192.168.10.203";
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
setupDebugPort();
setupWiFi();
setupMqtt();
// initialize the HanReader
// (passing Serial as the HAN port and Serial1 for debugging)
hanReader.setup(&Serial, &Serial1);
}
void setupMqtt()
{
client.setServer(mqtt_server, 1883);
}
void setupDebugPort()
{
// Initialize the Serial1 port for debugging
// (This port is fixed to Pin2 of the ESP8266)
Serial1.begin(115200);
while (!Serial1) {}
Serial1.setDebugOutput(true);
Serial1.println("Serial1");
Serial1.println("Serial debugging port initialized");
}
void setupWiFi()
{
// Initialize wifi
Serial1.print("Connecting to ");
Serial1.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial1.print(".");
}
Serial1.println("");
Serial1.println("WiFi connected");
Serial1.println("IP address: ");
Serial1.println(WiFi.localIP());
}
void loop() {
loopMqtt();
// Read one byt from the port, and see if we got a full package
if (hanReader.read())
{
// Get the list identifier
List list = hanReader.getList();
Serial1.println("");
Serial1.print("List #");
Serial1.print((byte)list, HEX);
Serial1.print(": ");
// Make sure we got a valid list
if (list == List::ListUnknown)
{
Serial1.println("Invalid list");
return;
}
// Get the timestamp (as unix time) from the package
time_t time = hanReader.getPackageTime();
// Define a json object to keep the data
StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
// Any generic useful info here
root["id"] = "espdebugger";
root["up"] = millis();
root["t"] = time;
// Add a sub-structure to the json object,
// to keep the data from the meter itself
JsonObject& data = root.createNestedObject("data");
// Based on the list number, get all details
// according to OBIS specifications for the meter
if (list == List::List1)
{
data["P"] = hanReader.getInt(List1_ObisObjects::ActivePowerImported);
}
else if (list == List::List2)
{
data["lv"] = hanReader.getString(List2_ObisObjects::ObisListVersionIdentifier);
data["id"] = hanReader.getString(List2_ObisObjects::MeterID);
data["type"] = hanReader.getString(List2_ObisObjects::MeterType);
data["P"] = hanReader.getInt(List2_ObisObjects::ActivePowerImported);
data["Q"] = hanReader.getInt(List2_ObisObjects::ReactivePowerImported);
data["I1"] = hanReader.getInt(List2_ObisObjects::CurrentPhaseL1);
data["I2"] = hanReader.getInt(List2_ObisObjects::CurrentPhaseL2);
data["I3"] = hanReader.getInt(List2_ObisObjects::CurrentPhaseL3);
data["U1"] = hanReader.getInt(List2_ObisObjects::VoltagePhaseL1);
data["U2"] = hanReader.getInt(List2_ObisObjects::VoltagePhaseL1);
data["U3"] = hanReader.getInt(List2_ObisObjects::VoltagePhaseL1);
}
else if (list == List::List3)
{
data["lv"] = hanReader.getString(List3_ObisObjects::ObisListVersionIdentifier);;
data["id"] = hanReader.getString(List3_ObisObjects::MeterID);
data["type"] = hanReader.getString(List3_ObisObjects::MeterType);
data["P"] = hanReader.getInt(List3_ObisObjects::ActivePowerImported);
data["Q"] = hanReader.getInt(List3_ObisObjects::ReactivePowerImported);
data["I1"] = hanReader.getInt(List3_ObisObjects::CurrentPhaseL1);
data["I2"] = hanReader.getInt(List3_ObisObjects::CurrentPhaseL2);
data["I3"] = hanReader.getInt(List3_ObisObjects::CurrentPhaseL3);
data["U1"] = hanReader.getInt(List3_ObisObjects::VoltagePhaseL1);
data["U2"] = hanReader.getInt(List3_ObisObjects::VoltagePhaseL1);
data["U3"] = hanReader.getInt(List3_ObisObjects::VoltagePhaseL1);
data["tPI"] = hanReader.getInt(List3_ObisObjects::TotalActiveEnergyImported);
data["tPO"] = hanReader.getInt(List3_ObisObjects::TotalActiveEnergyExported);
data["tQI"] = hanReader.getInt(List3_ObisObjects::TotalReactiveEnergyImported);
data["tQO"] = hanReader.getInt(List3_ObisObjects::TotalReactiveEnergyExported);
}
// Write the json to the debug port
root.printTo(Serial1);
Serial1.println();
// Publish the json to the MQTT server
char msg[1024];
root.printTo(msg, 1024);
client.publish("sensors/out/espdebugger", msg);
}
}
// Ensure the MQTT lirary gets some attention too
void loopMqtt()
{
if (!client.connected()) {
reconnectMqtt();
}
client.loop();
}
void reconnectMqtt() {
// Loop until we're reconnected
while (!client.connected()) {
Serial1.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial1.println("connected");
// Once connected, publish an announcement...
// client.publish("sensors", "hello world");
// ... and resubscribe
// client.subscribe("inTopic");
}
else {
Serial1.print("failed, rc=");
Serial1.print(client.state());
Serial1.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}