This repository was archived by the owner on Jan 21, 2021. It is now read-only.
forked from letscontrolit/ArduinoEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworking.ino
More file actions
419 lines (379 loc) · 12.6 KB
/
Networking.ino
File metadata and controls
419 lines (379 loc) · 12.6 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include "Misc.h"
#if !defined(STM32_OFFICIAL) && !defined(EthernetShield) && defined(NETSPD_FUNC)
#include "utility/w5500.h"
void wizphy_setphyconf(uint8_t pconf)
{
uint8_t tmp = 0;
tmp |= PHYCFGR_OPMD;
tmp |= pconf;
W5100.setPHYCFGR(tmp);
wizphy_reset();
}
void wizphy_reset(void)
{
uint8_t tmp = W5100.getPHYCFGR();
tmp &= PHYCFGR_RST;
W5100.setPHYCFGR(tmp);
tmp = W5100.getPHYCFGR();
tmp |= ~PHYCFGR_RST;
W5100.setPHYCFGR(tmp);
}
#endif
byte LinkState()
{
#if (!defined(UID_BASE) && defined(EthernetShield)) || defined(STM32_OFFICIAL)
return (Ethernet.linkStatus() == LinkON);
#else
return !!((W5100.getPHYCFGR()) & 1);
#endif
}
/*********************************************************************************************\
Syslog client
\*********************************************************************************************/
void syslog(const char *message)
{
#if FEATURE_UDP
if (Settings.Syslog_IP[0] != 0)
{
IPAddress broadcastIP(Settings.Syslog_IP[0], Settings.Syslog_IP[1], Settings.Syslog_IP[2], Settings.Syslog_IP[3]);
portUDP.beginPacket(broadcastIP, 514);
char str[256];
str[0] = 0;
#if defined(UID_BASE) // STM32
snprintf(str, 256, PSTR("<7>ESP Unit: %u : %s"), Settings.Unit, message);
// sprintf_P(str, PSTR("<7>ESP Unit: %u : %s"), Settings.Unit, message);
#else
snprintf_P(str, sizeof(str), PSTR("<7>ESP Unit: %u : %s"), Settings.Unit, message);
#endif
portUDP.write(str);
portUDP.endPacket(); // endpacket causes problems?
}
#endif
}
#if FEATURE_UDP
/*********************************************************************************************\
Structs for UDP messaging
\*********************************************************************************************/
struct infoStruct
{
byte header = 255;
byte ID = 3;
byte sourcelUnit;
byte destUnit;
byte sourceTaskIndex;
byte destTaskIndex;
byte deviceNumber;
char taskName[26];
char ValueNames[VARS_PER_TASK][26];
};
struct dataStruct
{
byte header = 255;
byte ID = 5;
byte sourcelUnit;
byte destUnit;
byte sourceTaskIndex;
byte destTaskIndex;
float Values[VARS_PER_TASK];
};
/*********************************************************************************************\
Check UDP messages
\*********************************************************************************************/
void checkUDP()
{
if (Settings.UDPPort == 0)
return;
// UDP events
int packetSize = portUDP.parsePacket();
if (packetSize)
{
statusLED(true);
IPAddress remoteIP = portUDP.remoteIP();
if (portUDP.remotePort() == 123)
{
// unexpected NTP reply, drop for now...
return;
}
byte packetBuffer[128];
int len = portUDP.read(packetBuffer, 128);
if (packetBuffer[0] != 255)
{
packetBuffer[len] = 0;
addLog(LOG_LEVEL_DEBUG, (char*)packetBuffer);
struct EventStruct TempEvent;
String request = (char*)packetBuffer;
parseCommandString(&TempEvent, request);
TempEvent.Source = VALUE_SOURCE_SYSTEM;
if (!PluginCall(PLUGIN_WRITE, &TempEvent, request))
ExecuteCommand(VALUE_SOURCE_SYSTEM, (char*)packetBuffer);
}
else
{
if (packetBuffer[1] > 1 && packetBuffer[1] < 6)
{
String log = (F("UDP : Sensor msg "));
for (byte x = 1; x < 6; x++)
{
log += " ";
log += (int)packetBuffer[x];
}
addLog(LOG_LEVEL_DEBUG_MORE, log);
}
// binary data!
switch (packetBuffer[1])
{
case 1: // sysinfo message
{
byte mac[6];
byte ip[4];
byte unit = packetBuffer[12];
for (byte x = 0; x < 6; x++)
mac[x] = packetBuffer[x + 2];
for (byte x = 0; x < 4; x++)
ip[x] = packetBuffer[x + 8];
if (unit < UNIT_MAX)
{
for (byte x = 0; x < 4; x++)
Nodes[unit].ip[x] = packetBuffer[x + 8];
Nodes[unit].age = 0; // reset 'age counter'
if (len >20) // extended packet size
{
Nodes[unit].build = packetBuffer[13] + 256*packetBuffer[14];
#if FEATURE_NODELIST_NAMES
//if (Nodes[unit].nodeName==0)
// Nodes[unit].nodeName = (char *)malloc(FEATURE_NODELIST_NAMESSIZE+1);
memcpy(&Nodes[unit].nodeName,(byte*)packetBuffer+15,FEATURE_NODELIST_NAMESSIZE);
Nodes[unit].nodeName[FEATURE_NODELIST_NAMESSIZE]=0;
Nodes[unit].nodeType = packetBuffer[40];
#endif
}
}
char macaddress[20];
sprintf_P(macaddress, PSTR("%02x:%02x:%02x:%02x:%02x:%02x"), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
char ipaddress[16];
sprintf_P(ipaddress, PSTR("%u.%u.%u.%u"), ip[0], ip[1], ip[2], ip[3]);
char log[80];
sprintf_P(log, PSTR("UDP : %s,%s,%u"), macaddress, ipaddress, unit);
addLog(LOG_LEVEL_DEBUG_MORE, log);
break;
}
case 2: // sensor info pull request
{
SendUDPTaskInfo(packetBuffer[2], packetBuffer[5], packetBuffer[4]);
break;
}
case 3: // sensor info
{
if (Settings.GlobalSync)
{
struct infoStruct infoReply;
memcpy((byte*)&infoReply, (byte*)&packetBuffer, sizeof(infoStruct));
// to prevent flash wear out (bugs in communication?) we can only write to an empty task
// so it will write only once and has to be cleared manually through webgui
if (Settings.TaskDeviceNumber[infoReply.destTaskIndex] == 0)
{
Settings.TaskDeviceNumber[infoReply.destTaskIndex] = infoReply.deviceNumber;
Settings.TaskDeviceDataFeed[infoReply.destTaskIndex] = 1; // remote feed
Settings.TaskDeviceSendData[infoReply.destTaskIndex] = false;
strcpy(ExtraTaskSettings.TaskDeviceName, infoReply.taskName);
for (byte x = 0; x < VARS_PER_TASK; x++)
strcpy( ExtraTaskSettings.TaskDeviceValueNames[x], infoReply.ValueNames[x]);
SaveTaskSettings(infoReply.destTaskIndex);
SaveSettings();
}
}
break;
}
case 4: // sensor data pull request
{
SendUDPTaskData(packetBuffer[2], packetBuffer[5], packetBuffer[4]);
break;
}
case 5: // sensor data
{
if (Settings.GlobalSync)
{
struct dataStruct dataReply;
memcpy((byte*)&dataReply, (byte*)&packetBuffer, sizeof(dataStruct));
// only if this task has a remote feed, update values
if (Settings.TaskDeviceDataFeed[dataReply.destTaskIndex] != 0)
{
for (byte x = 0; x < VARS_PER_TASK; x++)
{
UserVar[dataReply.destTaskIndex * VARS_PER_TASK + x] = dataReply.Values[x];
}
if (Settings.UseRules)
createRuleEvents(dataReply.destTaskIndex);
}
}
break;
}
default:
{
struct EventStruct TempEvent;
TempEvent.Data = (byte*)packetBuffer;
TempEvent.Par1 = remoteIP[3];
PluginCall(PLUGIN_UDP_IN, &TempEvent, dummyString);
break;
}
}
}
}
}
/*********************************************************************************************\
Send task info using UDP message
\*********************************************************************************************/
void SendUDPTaskInfo(byte destUnit, byte sourceTaskIndex, byte destTaskIndex)
{
struct infoStruct infoReply;
infoReply.sourcelUnit = Settings.Unit;
infoReply.sourceTaskIndex = sourceTaskIndex;
infoReply.destTaskIndex = destTaskIndex;
LoadTaskSettings(infoReply.sourceTaskIndex);
infoReply.deviceNumber = Settings.TaskDeviceNumber[infoReply.sourceTaskIndex];
strcpy(infoReply.taskName, ExtraTaskSettings.TaskDeviceName);
for (byte x = 0; x < VARS_PER_TASK; x++)
strcpy(infoReply.ValueNames[x], ExtraTaskSettings.TaskDeviceValueNames[x]);
byte firstUnit = 1;
byte lastUnit = UNIT_MAX - 1;
if (destUnit != 0)
{
firstUnit = destUnit;
lastUnit = destUnit;
}
for (byte x = firstUnit; x <= lastUnit; x++)
{
infoReply.destUnit = x;
sendUDP(x, (byte*)&infoReply, sizeof(infoStruct));
delay(10);
}
delay(50);
}
/*********************************************************************************************\
Send task data using UDP message
\*********************************************************************************************/
void SendUDPTaskData(byte destUnit, byte sourceTaskIndex, byte destTaskIndex)
{
struct dataStruct dataReply;
dataReply.sourcelUnit = Settings.Unit;
dataReply.sourceTaskIndex = sourceTaskIndex;
dataReply.destTaskIndex = destTaskIndex;
for (byte x = 0; x < VARS_PER_TASK; x++)
dataReply.Values[x] = UserVar[dataReply.sourceTaskIndex * VARS_PER_TASK + x];
byte firstUnit = 1;
byte lastUnit = UNIT_MAX - 1;
if (destUnit != 0)
{
firstUnit = destUnit;
lastUnit = destUnit;
}
for (byte x = firstUnit; x <= lastUnit; x++)
{
dataReply.destUnit = x;
sendUDP(x, (byte*) &dataReply, sizeof(dataStruct));
delay(10);
}
delay(50);
}
/*********************************************************************************************\
Send event using UDP message
\*********************************************************************************************/
void SendUDPCommand(byte destUnit, char* data, byte dataLength)
{
byte firstUnit = 1;
byte lastUnit = UNIT_MAX - 1;
if (destUnit != 0)
{
firstUnit = destUnit;
lastUnit = destUnit;
}
for (byte x = firstUnit; x <= lastUnit; x++)
{
sendUDP(x, (byte*)data, dataLength);
delay(10);
}
delay(50);
}
/*********************************************************************************************\
Send UDP message
\*********************************************************************************************/
void sendUDP(byte unit, byte* data, byte size)
{
if (Nodes[unit].ip[0] == 0)
return;
String log = F("UDP : Send UDP message to ");
log += unit;
addLog(LOG_LEVEL_DEBUG_MORE, log);
statusLED(true);
IPAddress remoteNodeIP(Nodes[unit].ip[0], Nodes[unit].ip[1], Nodes[unit].ip[2], Nodes[unit].ip[3]);
portUDP.beginPacket(remoteNodeIP, Settings.UDPPort);
portUDP.write(data, size);
portUDP.endPacket();
}
/*********************************************************************************************\
Refresh aging for remote units, drop if too old...
\*********************************************************************************************/
void refreshNodeList()
{
for (byte counter = 0; counter < UNIT_MAX; counter++)
{
if (Nodes[counter].ip[0] != 0)
{
Nodes[counter].age++; // increment age counter
if (Nodes[counter].age > 10) // if entry to old, clear this node ip from the list.
for (byte x = 0; x < 4; x++)
Nodes[counter].ip[x] = 0;
}
}
}
void sendSysInfoUDP(byte repeats)
{
char log[80];
if (Settings.UDPPort == 0)
return;
// 1 byte 'binary token 255'
// 1 byte id '1'
// 6 byte mac
// 4 byte ip
// 1 byte unit
// 2 byte build
// 25 char name
// 1 byte node type id
// send my info to the world...
strcpy_P(log, PSTR("UDP : Send Sysinfo message"));
addLog(LOG_LEVEL_DEBUG_MORE, log);
for (byte counter = 0; counter < repeats; counter++)
{
byte data[80];
data[0] = 255;
data[1] = 1;
for (byte x = 0; x < 6; x++)
data[x + 2] = mac[x];
IPAddress ip = Ethernet.localIP();
for (byte x = 0; x < 4; x++)
data[x + 8] = ip[x];
data[12] = Settings.Unit;
data[13] = Settings.Build & 0xff;
data[14] = Settings.Build >> 8;
memcpy((byte*)data+15,Settings.Name,25);
data[40] = NODE_TYPE_ID;
statusLED(true);
IPAddress broadcastIP(255, 255, 255, 255);
portUDP.beginPacket(broadcastIP, Settings.UDPPort);
portUDP.write(data, 80);
portUDP.endPacket();
if (counter < (repeats - 1))
delay(500);
}
// store my own info also in the list...
if (Settings.Unit < UNIT_MAX)
{
IPAddress ip = Ethernet.localIP();
for (byte x = 0; x < 4; x++)
Nodes[Settings.Unit].ip[x] = ip[x];
Nodes[Settings.Unit].age = 0;
Nodes[Settings.Unit].build = Settings.Build;
Nodes[Settings.Unit].nodeType = NODE_TYPE_ID;
}
}
#endif