forked from letscontrolit/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_P127_Teleinfo.ino
296 lines (235 loc) · 8.6 KB
/
_P127_Teleinfo.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//#######################################################################################################
//#################################### Plugin 127: Teleinfo #############################################
//#################################### This plugin transmits data of energy counter to HTTP server #####
//#################################### Compatible with jeedom plugin Teleinfo #####
//#######################################################################################################
#define PLUGIN_127
#define PLUGIN_ID_127 127
#define PLUGIN_NAME_127 "Teleinfo"
#define P127_BUFFER_SIZE 128
boolean Plugin_127_init = false;
int P127_CYCLE = 0;
String P127_SendData = "";
String P127_URL = "";
int P127_PORT = 80;
String P127_HOST = "";
boolean Plugin_127(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
static byte connectionState = 0;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_127;
Device[deviceCount].SendDataOption = false;
Device[deviceCount].TimerOption = false;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].Custom = true;
Device[deviceCount].ValueCount = 0;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_127);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
break;
}
case PLUGIN_WEBFORM_LOAD:
{
char deviceTemplate[2][128];
LoadCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
string += F("<TR><TD>Host:<TD><input type='text' size='64' maxlength='128' name='Plugin_127_host' value='");
string += deviceTemplate[0];
string += F("'>");
char tmpString[128];
sprintf_P(tmpString, PSTR("<TR><TD>Port:<TD><input type='text' name='Plugin_127_port' value='%u'>"), Settings.TaskDevicePluginConfig[event->TaskIndex][0]);
string += tmpString;
string += F("<TR><TD>Start url:<TD><input type='text' size='64' maxlength='128' name='Plugin_127_url' value='");
string += deviceTemplate[1];
string += F("'>");
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
String plugin1 = WebServer.arg("Plugin_127_port");
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = plugin1.toInt();
char deviceTemplate[2][128];
char argc[25];
String arg = F("Plugin_127_host");
arg.toCharArray(argc, 25);
String tmpString = WebServer.arg(argc);
strncpy(deviceTemplate[0], tmpString.c_str(), sizeof(deviceTemplate[0]));
arg = F("Plugin_127_url");
arg.toCharArray(argc, 25);
tmpString = WebServer.arg(argc);
strncpy(deviceTemplate[1], tmpString.c_str(), sizeof(deviceTemplate[1]));
Settings.TaskDeviceID[event->TaskIndex] = 1; // temp fix, needs a dummy value
SaveCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
success = true;
break;
}
case PLUGIN_INIT: // ok
{
char deviceTemplate[2][128];
LoadCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
P127_HOST = String(deviceTemplate[0]);
P127_PORT = int(Settings.TaskDevicePluginConfig[event->TaskIndex][0]);
P127_URL = String(deviceTemplate[1]);
Serial.begin(1200, SERIAL_7E1); //Liaison série avec les paramètres
Plugin_127_init = true;
success = true;
break;
}
case PLUGIN_SERIAL_IN:
{
uint8_t serial_buf[P127_BUFFER_SIZE];
int RXWait = 10;
int timeOut = RXWait;
size_t bytes_read = 0;
char log[40];
while (timeOut > 0)
{
while (Serial.available()) {
if (bytes_read < P127_BUFFER_SIZE) {
serial_buf[bytes_read] = Serial.read();
bytes_read++;
}
else {
Serial.read(); // when the buffer is full, just read remaining input, but do not store...
}
timeOut = RXWait; // if serial received, reset timeout counter
}
delay(1);
timeOut--;
}
if (bytes_read == P127_BUFFER_SIZE) // if we have a full buffer, drop the last position to stuff with string end marker
{
while (Serial.available()) { // read possible remaining data to avoid sending rubbish...
Serial.read();
}
bytes_read--;
// and log buffer full situation
// strcpy_P(log, PSTR("Teleinfo: serial buffer full!"));
// addLog(LOG_LEVEL_ERROR, log);
}
serial_buf[bytes_read] = 0; // before logging as a char array, zero terminate the last position to be safe.
// char log[P127_BUFFER_SIZE + 40];
// sprintf_P(log, PSTR("Ser2N: S>: %s"), (char*)serial_buf);
// addLog(LOG_LEVEL_DEBUG, log);
// We can also use the rules engine for local control!
String message = (char*)serial_buf;
int NewLinePos = message.indexOf("\r\n");
if (NewLinePos > 0) {
message = message.substring(0, NewLinePos);
}
String eventString = "";
// message.replace("\r", "");
if (message.length() > 5) {
int indexValue = message.indexOf(" ");
String nameValue = message.substring(1, indexValue);
String value = message.substring(indexValue + 1, message.length() - 3);
String checksum = message.substring(message.length() - 2);
// UserVar[event->BaseVarIndex] = value.toInt();
if (P127_CYCLE == 0) {
if (P127_checksum(nameValue, value, checksum)) {
eventString = F("Teleinfo#");
eventString += nameValue;
eventString += F("=");
eventString += value;
P127_SendData += nameValue + "=" + value + "&";
}
}
if (nameValue == "ADCO") {
if (P127_CYCLE < 8) {
P127_CYCLE++;
} else {
if ( P127_checksum(nameValue, value, checksum) ) {
eventString = F("Teleinfo#SendData");
P127_CYCLE = 0;
P127_sendtoHTTP(P127_HOST, P127_PORT, P127_URL + P127_SendData);
P127_SendData = "";
}
}
}
if (eventString.length() > 0) {
rulesProcessing(eventString);
}
}
success = true;
break;
}
}
return success;
}
boolean P127_checksum(String valuename, String value, String checksum) {
String data = "";
int i;
char sum = 0;
char sumchar;
sumchar = checksum.charAt(0);
data = valuename + " " + value;
for (i = 0; i < data.length(); i++) {
sum = sum + data.charAt(i);
}
sum = (sum & 0x3F) + 0x20;
if (sum == sumchar) {
return true;
} else {
return false;
}
}
boolean P127_sendtoHTTP(String hostname, int port, String url) {
char log[80];
boolean success = false;
char host[128];
hostname.toCharArray(host, 128);
sprintf_P(log, PSTR("%s%s using port %u"), "HTTP : connecting to ", host, port);
addLog(LOG_LEVEL_DEBUG, log);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port))
{
connectionFailures++;
strcpy_P(log, PSTR("HTTP : connection failed"));
addLog(LOG_LEVEL_ERROR, log);
return false;
}
statusLED(true);
if (connectionFailures)
connectionFailures--;
url.toCharArray(log, 80);
addLog(LOG_LEVEL_DEBUG_MORE, log);
String hostName = host;
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timer = millis() + 200;
while (!client.available() && millis() < timer)
delay(1);
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\n');
line.toCharArray(log, 80);
addLog(LOG_LEVEL_DEBUG_MORE, log);
if (line.substring(0, 15) == "HTTP/1.1 200 OK")
{
strcpy_P(log, PSTR("HTTP : Succes!"));
addLog(LOG_LEVEL_DEBUG, log);
success = true;
}
delay(1);
}
strcpy_P(log, PSTR("HTTP : closing connection"));
addLog(LOG_LEVEL_DEBUG, log);
client.flush();
client.stop();
}