This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
esp8266-dsmr.ino
171 lines (145 loc) · 5.38 KB
/
esp8266-dsmr.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
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ArduinoJson.h>
#include "ESP8266mDNS.h"
#include "MQTTPublisher.h"
#include "WifiConnector.h"
#include "Settings.h"
#include "Logger.h"
#include "AutoConfig.h"
typedef struct
{
String name;
String key; // OBIS property key
int start; // start of value in string
int end; // end of value in string
bool online;
enum
{
STRING,
FLOAT,
INT
} valueType;
} Measurement;
const Measurement measurements[] = {
{"version", "1-3:0.2.8", 10, 12, false, Measurement::STRING},
{"power/timestamp", "0-0:1.0.0", 10, 23, false, Measurement::STRING},
{"power/device_id", "0-0:96.1.1", 11, 45, false, Measurement::STRING},
{"power/consumption", "1-0:1.7.0", 10, 16, false, Measurement::FLOAT},
{"power/production", "1-0:2.7.0", 10, 16, false, Measurement::FLOAT},
{"power/total_consumption_tariff_1", "1-0:1.8.1", 10, 20, false, Measurement::FLOAT},
{"power/total_consumption_tariff_2", "1-0:1.8.2", 10, 20, false, Measurement::FLOAT},
{"power/total_production_tariff_1", "1-0:2.8.1", 10, 20, false, Measurement::FLOAT},
{"power/total_production_tariff_2", "1-0:2.8.2", 10, 20, false, Measurement::FLOAT},
{"power/power_tariff", "0-0:96.14.0", 12, 16, false, Measurement::INT},
{"power/short_power_outages", "0-0:96.7.21", 12, 17, false, Measurement::INT},
{"power/long_power_outages", "0-0:96.7.9", 11, 16, false, Measurement::INT},
{"power/phase_1/short_power_drops", "1-0:32.32.0", 12, 17, false, Measurement::INT},
{"power/phase_2/short_power_drops", "1-0:52.32.0", 12, 17, false, Measurement::INT},
{"power/phase_3/short_power_drops", "1-0:72.32.0", 12, 17, false, Measurement::INT},
{"power/phase_1/short_power_peaks", "1-0:32.36.0", 12, 17, false, Measurement::INT},
{"power/phase_2/short_power_peaks", "1-0:52.36.0", 12, 17, false, Measurement::INT},
{"power/phase_3/short_power_peaks", "1-0:72.36.0", 12, 17, false, Measurement::INT},
{"power/phase_1/current", "1-0:31.7.0", 11, 14, false, Measurement::INT},
{"power/phase_2/current", "1-0:51.7.0", 11, 14, false, Measurement::INT},
{"power/phase_3/current", "1-0:71.7.0", 11, 14, false, Measurement::INT},
{"power/phase_1/consumption", "1-0:21.7.0", 11, 17, false, Measurement::FLOAT},
{"power/phase_2/consumption", "1-0:41.7.0", 11, 17, false, Measurement::FLOAT},
{"power/phase_3/consumption", "1-0:61.7.0", 11, 17, false, Measurement::FLOAT},
{"power/phase_1/production", "1-0:22.7.0", 11, 17, false, Measurement::FLOAT},
{"power/phase_2/production", "1-0:42.7.0", 11, 17, false, Measurement::FLOAT},
{"power/phase_3/production", "1-0:62.7.0", 11, 17, false, Measurement::FLOAT},
{"gas/total", "0-1:24.2.1", 26, 35, false, Measurement::FLOAT},
{"gas/device_id", "0-1:96.1.0", 11, 45, false, Measurement::STRING},
{"gas/timestamp", "0-1:24.2.1", 11, 24, false, Measurement::STRING}};
MQTTPublisher mqttPublisher;
WifiConnector wifiConnector;
AutoConfig autoConfig;
WiFiUDP ntpUDP;
String incomingString = "";
bool hasMQTT = false;
bool hasWIFI = false;
Logger logger = Logger("App");
char identifier[24];
int valueState[29];
int rxPin = 3;
int lvlPin = 5;
void setup()
{
// Start serial ESP8266 RX port (pin 3)
Serial.begin(115200);
pinMode(rxPin, FUNCTION_0); // RX
logger.info("Start setup");
// Set pin modes
pinMode(lvlPin, OUTPUT); // D1
digitalWrite(lvlPin, HIGH); // Turn on RX Trans
snprintf(identifier, sizeof(identifier), "ESP_DSMR_%X", ESP.getChipId());
// Setup Wifi
wifiConnector = WifiConnector();
wifiConnector.start();
// Setup MQTT
mqttPublisher = MQTTPublisher(identifier);
mqttPublisher.start();
// Setup OTA
ArduinoOTA.setHostname(WIFI_HOSTNAME);
ArduinoOTA.begin();
// Sent HA config
autoConfig = AutoConfig(mqttPublisher, identifier);
autoConfig.SendConfig();
logger.info("Setup complete");
}
void loop()
{
wifiConnector.handle();
yield();
ArduinoOTA.handle();
yield();
mqttPublisher.handle();
yield();
// If serial received, read until newline
if (Serial.available() > 0)
{
incomingString = Serial.readStringUntil('\n');
handleString(incomingString);
}
}
// Regex are not supported, so use indexOf and substring
void handleString(String incomingString)
{
int i;
int arraySize = sizeof(measurements) / sizeof(measurements[0]);
for (i = 0; i < arraySize; i++)
{
Measurement measurement = measurements[i];
String obisKey = measurement.key;
if (incomingString.indexOf(obisKey) > -1)
{
// found
String value = incomingString.substring(measurement.start, measurement.end);
logger.debug("DEBUG_1 " + obisKey + "=" + value);
switch (measurement.valueType)
{
case Measurement::FLOAT:
value = String(value.toFloat(), 3);
break;
case Measurement::INT:
value = String(value.toInt());
break;
default:
break;
}
// Check if measurement state is offline, if so publish online state and last reset
if (!measurement.online)
{
measurement.online = true;
mqttPublisher.publish(measurement.name + "/status", "online", true);
mqttPublisher.publish(measurement.name + "/reset", "1970-01-01T00:00:00+00:00", true);
}
// Publish measurement
mqttPublisher.publish(measurement.name, value, true);
}
}
}