-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.ino
338 lines (298 loc) · 9.34 KB
/
Controller.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
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
//********************************************************************************
// Interface for Sending to Controllers
//********************************************************************************
boolean sendData(struct EventStruct *event)
{
LoadTaskSettings(event->TaskIndex);
byte ProtocolIndex = getProtocolIndex(Settings.Protocol);
CPlugin_ptr[ProtocolIndex](CPLUGIN_PROTOCOL_SEND, event);
PluginCall(PLUGIN_EVENT_OUT, event, dummyString);
if (Settings.MessageDelay != 0)
{
char log[30];
sprintf_P(log, PSTR("HTTP : Delay %u ms"), Settings.MessageDelay);
addLog(LOG_LEVEL_DEBUG_MORE, log);
unsigned long timer = millis() + Settings.MessageDelay;
while (millis() < timer)
backgroundtasks();
}
}
/*********************************************************************************************\
* Handle incoming MQTT messages
\*********************************************************************************************/
// handle MQTT messages
void callback(const MQTT::Publish& pub) {
char log[80];
String message = pub.payload_string();
String topic = pub.topic();
char c_topic[80];
topic.toCharArray(c_topic, 80);
sprintf_P(log, PSTR("%s%s"), "MQTT : Topic ", c_topic);
addLog(LOG_LEVEL_DEBUG, log);
struct EventStruct TempEvent;
TempEvent.String1 = topic;
TempEvent.String2 = message;
byte ProtocolIndex = getProtocolIndex(Settings.Protocol);
CPlugin_ptr[ProtocolIndex](CPLUGIN_PROTOCOL_RECV, &TempEvent);
}
/*********************************************************************************************\
* Connect to MQTT message broker
\*********************************************************************************************/
void MQTTConnect()
{
IPAddress MQTTBrokerIP(Settings.Controller_IP);
MQTTclient.set_server(MQTTBrokerIP, Settings.ControllerPort);
MQTTclient.set_callback(callback);
// MQTT needs a unique clientname to subscribe to broker
String clientid = "ESPClient";
clientid += Settings.Unit;
String subscribeTo = "";
for (byte x = 1; x < 3; x++)
{
String log = "";
if (MQTTclient.connect(clientid))
{
log = F("MQTT : Connected to broker");
addLog(LOG_LEVEL_INFO, log);
subscribeTo = Settings.MQTTsubscribe;
subscribeTo.replace("%sysname%", Settings.Name);
MQTTclient.subscribe(subscribeTo);
log = F("Subscribed to: ");
log += subscribeTo;
addLog(LOG_LEVEL_INFO, log);
break; // end loop if succesfull
}
else
{
log = F("MQTT : Failed to connected to broker");
addLog(LOG_LEVEL_ERROR, log);
}
delay(500);
}
}
/*********************************************************************************************\
* Check connection MQTT message broker
\*********************************************************************************************/
void MQTTCheck()
{
byte ProtocolIndex = getProtocolIndex(Settings.Protocol);
if (Protocol[ProtocolIndex].usesMQTT)
if (!MQTTclient.connected())
{
MQTTclient.disconnect();
delay(1000);
MQTTConnect();
}
}
struct NodeStruct
{
byte ip[4];
byte age;
} Nodes[32];
/*********************************************************************************************\
* Send data to other ESP node
\*********************************************************************************************/
boolean nodeVariableCopy(byte var, byte unit)
{
float value = UserVar[var - 1];
char log[80];
boolean success = false;
char host[20];
if (Nodes[unit].ip[0] == 0)
{
strcpy_P(log, PSTR("Remote Node unknown"));
addLog(LOG_LEVEL_DEBUG, log);
if (printToWeb)
{
printWebString += log;
printWebString += "<BR>";
}
return false;
}
sprintf_P(host, PSTR("%u.%u.%u.%u"), Nodes[unit].ip[0], Nodes[unit].ip[1], Nodes[unit].ip[2], Nodes[unit].ip[3]);
sprintf_P(log, PSTR("%s%s"), "connecting to ", host);
addLog(LOG_LEVEL_DEBUG, log);
if (printToWeb)
{
printWebString += log;
printWebString += "<BR>";
}
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, 80))
{
connectionFailures++;
strcpy_P(log, PSTR("HTTP : connection failed"));
addLog(LOG_LEVEL_ERROR, log);
if (printToWeb)
printWebString += F("connection failed<BR>");
return false;
}
if (connectionFailures)
connectionFailures--;
// We now create a URI for the request
String url = F("/?cmd=variableset%20");
url += var;
url += ",";
url += value;
url.toCharArray(log, 80);
addLog(LOG_LEVEL_DEBUG, log);
if (printToWeb)
{
printWebString += log;
printWebString += "<BR>";
}
// 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');
if (line.substring(0, 15) == "HTTP/1.1 200 OK")
{
strcpy_P(log, PSTR("HTTP : Succes!"));
addLog(LOG_LEVEL_DEBUG, log);
if (printToWeb)
printWebString += F("Success<BR>");
success = true;
}
}
strcpy_P(log, PSTR("HTTP : closing connection"));
addLog(LOG_LEVEL_DEBUG, log);
if (printToWeb)
printWebString += F("closing connection<BR>");
return success;
}
/*********************************************************************************************\
* Syslog client
\*********************************************************************************************/
void syslog(char *message)
{
if (Settings.Syslog_IP[0] != 0)
{
IPAddress broadcastIP(Settings.Syslog_IP[0], Settings.Syslog_IP[1], Settings.Syslog_IP[2], Settings.Syslog_IP[3]);
portTX.beginPacket(broadcastIP, 514);
char str[80];
str[0] = 0;
sprintf_P(str, PSTR("<7>ESP Unit: %u : %s"), Settings.Unit, message);
portTX.write(str);
portTX.endPacket();
}
}
/*********************************************************************************************\
* Check UDP messages
\*********************************************************************************************/
void checkUDP()
{
if (Settings.UDPPort == 0)
return;
// UDP events
int packetSize = portRX.parsePacket();
if (packetSize)
{
IPAddress remoteIP = portRX.remoteIP();
char packetBuffer[128];
int len = portRX.read(packetBuffer, 128);
if (packetBuffer[0] != 255)
{
packetBuffer[len] = 0;
addLog(LOG_LEVEL_DEBUG, packetBuffer);
ExecuteCommand(packetBuffer);
}
else
{
// 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 < 31)
{
for (byte x = 0; x < 4; x++)
Nodes[unit].ip[x] = packetBuffer[x + 8];
Nodes[unit].age = 0; // reset 'age counter'
}
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;
}
default:
{
struct EventStruct TempEvent;
TempEvent.Data = (byte*)packetBuffer;
TempEvent.Par1 = remoteIP[3];
PluginCall(PLUGIN_UDP_IN, &TempEvent, dummyString);
break;
}
}
}
}
}
void refreshNodeList()
{
for (byte counter = 0; counter < 32; 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
// 6 byte mac
// 4 byte ip
// 1 byte unit
// 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++)
{
uint8_t mac[] = {0, 0, 0, 0, 0, 0};
uint8_t* macread = WiFi.macAddress(mac);
byte data[20];
data[0] = 255;
data[1] = 1;
for (byte x = 0; x < 6; x++)
data[x + 2] = macread[x];
IPAddress ip = WiFi.localIP();
for (byte x = 0; x < 4; x++)
data[x + 8] = ip[x];
data[12] = Settings.Unit;
IPAddress broadcastIP(255, 255, 255, 255);
portTX.beginPacket(broadcastIP, Settings.UDPPort);
portTX.write(data, 20);
portTX.endPacket();
if (counter < (repeats - 1))
delay(500);
}
// store my own info also in the list...
IPAddress ip = WiFi.localIP();
for (byte x = 0; x < 4; x++)
Nodes[Settings.Unit].ip[x] = ip[x];
Nodes[Settings.Unit].age = 0;
}