-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_wifi_arduino.ino
473 lines (408 loc) · 11 KB
/
mqtt_wifi_arduino.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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
MQTT Wifi Arduino
- connects to an MQTT server
- publishes Status messages and temperature from sensor at predefined interval
- subscribes to the command subtopic in order to publish temperature value on demand
- topic structure:
Temperature/Messages
/Alarms
/Statuses
/Command
- output structure:
{"Key":value}
where Key is Message | Error | Temperature
- input structure:
{"Command":"getValue"}
{"Command":"setFreq","Hz":val}
{"Command":"setParam","fSample":num}
{"Command":"setParam","Interval":sec}
*/
// TO DO
// Provare a cambiare Wifi.cpp http://stackoverflow.com/questions/20339952/mosquitto-socket-read-error-arduino-client
// per last will che non va
//
// HO FINITO LA MEMORIA... interval viene sovrascritto
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <OneWire/OneWire.h>
#include <ArduinoJson/ArduinoJson.h>
#include <FreqMeasure/FreqMeasure.h>
#include "mqtt_wifi_arduino.h"
// Callback Function: handle message arrived
void callback(char* topic, byte* payload, unsigned int length) {
// Allocate the correct amount of memory for the payload copy
char* cmd = (char*)malloc(length) + 1;
// Copy the payload to the new buffer
memcpy(cmd, payload, length);
cmd[length] = 0;
DEBUG_PRINT(F("---Message Received:"));
DEBUG_PRINT(cmd);
DEBUG_PRINT(F("---Message End"));
//Parse and validate JSON
// StaticJsonBuffer<100> jsonBuffer;
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(cmd);
if (!json.success()) {
if (mqttPublish(baseTopic, statusTopic, "Error", "JSON Error")) {
DEBUG_PRINT(F("Published"));
ledOn(Yellow_led);
}
else {
DEBUG_PRINT(F("Error in publishing"));
ledOn(Red_led);
}
free(cmd);
return;
}
//check and execute command getValue
if (!strcmp_P(json[cmdKey], PSTR("getValue"))) {
if (publishTemp() && publishReadFreq(lastReadFreq) && publishInterval()) {
DEBUG_PRINT(F("Published"));
ledOn(Green_led);
}
else {
DEBUG_PRINT(F("Error in publishing"));
ledOn(Red_led);
}
free(cmd);
return;
}
//check and execute command setFreq
if (!strcmp_P(json[cmdKey], PSTR("setFreq")) && json[HzKey] > 0) {
if (publishSetFreq(json[HzKey])) {
DEBUG_PRINT(F("Published"));
ledOn(Green_led);
}
else {
DEBUG_PRINT(F("Error in publishing"));
ledOn(Red_led);
}
free(cmd);
return;
}
//check and execute command setParam
if (!strcmp_P(json[cmdKey], PSTR("setParam")) && json[freqSampleKey] > 0) {
if (publishSetFreqSample(json[freqSampleKey])) {
DEBUG_PRINT(F("Published"));
ledOn(Green_led);
}
else {
DEBUG_PRINT(F("Error in publishing"));
ledOn(Red_led);
}
free(cmd);
return;
}
//check and execute command setParam
if (!strcmp_P(json[cmdKey], PSTR("setParam")) && json[intervalKey] > 0) {
if (publishSetInterval(json[intervalKey])) {
DEBUG_PRINT(F("Published"));
ledOn(Green_led);
}
else {
DEBUG_PRINT(F("Error in publishing"));
ledOn(Red_led);
}
free(cmd);
return;
}
// else: unrocognized command
if (mqttPublish(baseTopic, statusTopic, "Error", "Invalid Command")) {
DEBUG_PRINT(F("Published"));
ledOn(Yellow_led);
}
else {
DEBUG_PRINT(F("Error in publishing"));
ledOn(Red_led);
}
free(cmd);
return;
}
WiFiClient WFclient;
PubSubClient client(server, mqttPort, callback, WFclient);
OneWire ds(DS18S20_Pin);
// Turn a single LED on or off
void inline ledOn(int led) {
digitalWrite(Red_led, LOW);
digitalWrite(Yellow_led, LOW);
digitalWrite(Green_led, LOW);
digitalWrite(led, HIGH);
}
// subscribe inline function
boolean inline mqttSubscribe(const char *base, const char *sub) {
strcpy_P(printbuf, base);
strcat_P(printbuf, sub);
return (client.subscribe(printbuf));
}
// publish inline function
boolean inline mqttPublish(const char *base, const char *sub, const char *key, const char *payload) {
strcpy_P(printbuf, base);
strcat_P(printbuf, sub);
StaticJsonBuffer<100> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json[key] = payload;
json.printTo(jtempbuf, sizeof(jtempbuf));
DEBUG_PRINT(printbuf);
return (client.publish(printbuf, jtempbuf));
}
boolean inline mqttPublish(const char *base, const char *sub, const char *key, float payload) {
strcpy_P(printbuf, base);
strcat_P(printbuf, sub);
StaticJsonBuffer<100> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json[key].set(payload,2);
json.printTo(jtempbuf, sizeof(jtempbuf));
DEBUG_PRINT(printbuf);
return (client.publish(printbuf, jtempbuf));
}
// read temperature from sensor and publish it to message topic
boolean inline publishTemp() {
return mqttPublish(baseTopic, messageTopic, "Temp C", getTemp());
}
// publish message interval value to message topic
boolean inline publishInterval() {
return mqttPublish(baseTopic, messageTopic, "Msg interval sec", interval / 1000);
}
// set wave output frequency and publish it to message topic
boolean inline publishSetFreq(int freq) {
tone(freqOut_pin, freq);
return mqttPublish(baseTopic, messageTopic, "Set Freq Hz", freq);
}
// set frequency sampling value and publish it to message topic
boolean inline publishSetFreqSample(int samples) {
freqMaxCount = samples;
return mqttPublish(baseTopic, messageTopic, "Freq Samples", samples);
}
// set message interval value and publish it to message topic
boolean inline publishSetInterval(int seconds) {
interval = seconds * 1000l;
return mqttPublish(baseTopic, messageTopic, "Msg Interval sec", seconds);
}
// publish to message topic the measured frequency
boolean inline publishReadFreq(float freq) {
return mqttPublish(baseTopic, messageTopic, "Measured Freq Hz", freq);
}
void setup()
{
// use serial only in debug mode
#ifdef DEBUG
Serial.begin(9600);
#endif
// init LEDs
pinMode(Red_led, OUTPUT);
pinMode(Yellow_led, OUTPUT);
pinMode(Green_led, OUTPUT);
digitalWrite(Red_led, LOW);
digitalWrite(Yellow_led, LOW);
digitalWrite(Green_led, LOW);
// init freq pins
pinMode(freqIn_pin, INPUT);
pinMode(freqOut_pin, OUTPUT);
tone(freqOut_pin, curFreq);
FreqMeasure.begin();
// check for the presence of the shield
delay(5000); // wait 5 seconds for the shield to boot
if (WiFi.status() == WL_NO_SHIELD) {
DEBUG_PRINT(F("WiFi shield not present"));
// don't continue:
ledOn(Red_led);
while (true);
}
mqttClientId[0] = 0;
if (doConnect(mqttClientId)) {
if (mqttSubscribe(baseTopic, cmdTopic)) {
DEBUG_PRINT(F("Subscribed to cmd"));
ledOn(Green_led);
}
else {
DEBUG_PRINT(F("Error in subscribing"));
ledOn(Red_led);
}
if (mqttPublish(baseTopic, statusTopic, "Message", "Now Online")) {
DEBUG_PRINT(F("Published"));
ledOn(Green_led);
}
else {
DEBUG_PRINT(F("Error in publishing"));
ledOn(Red_led);
}
if (publishTemp()) {
DEBUG_PRINT(F("Published"));
ledOn(Green_led);
}
else {
DEBUG_PRINT(F("Error in publishing"));
ledOn(Red_led);
}
}
else {
DEBUG_PRINT(F("Connection KO!"));
ledOn(Red_led);
}
}
void loop()
{
unsigned long curTime = millis();
if (!client.connected()) {
doConnect(mqttClientId);
if (mqttSubscribe(baseTopic, cmdTopic)) {
DEBUG_PRINT(F("Subscribed to cmd"));
ledOn(Green_led);
}
else {
DEBUG_PRINT(F("Error in subscribing"));
ledOn(Red_led);
}
}
if (curTime - prevTime > interval) {
// read temperature from sensor and publish it to message topic
if (publishTemp()) {
DEBUG_PRINT(F("Published"));
ledOn(Green_led);
prevTime = curTime;
}
else {
DEBUG_PRINT(F("Error in publishing"));
ledOn(Red_led);
}
// publish measured frequency to message topic
if (publishReadFreq(lastReadFreq)) {
DEBUG_PRINT(F("Published"));
ledOn(Green_led);
prevTime = curTime;
}
else {
DEBUG_PRINT(F("Error in publishing"));
ledOn(Red_led);
}
}
// read frequency
float frequency = frequencyRead();
if (frequency > 0 && abs(frequency - lastReadFreq) > .01 ) {
lastReadFreq = frequency;
DEBUG_PRINT(F("Frequency:"));
DEBUG_PRINT(lastReadFreq);
}
delay(1000);
client.loop();
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
// use the formatted MAC address as ClientId
void MQTTgetClientId(char *ClientId)
{
uint8_t mac[WL_MAC_ADDR_LENGTH];
int j = 0;
WiFi.macAddress(mac);
for (int i = WL_MAC_ADDR_LENGTH; i > 0; i--) {
sprintf(&ClientId[j], "%02X:", mac[i]);
j += 3;
}
ClientId[j - 1] = 0;
DEBUG_PRINT(ClientId);
}
// connect to wifi network and MQTT server
bool doConnect(char *ClientId)
{
bool retval = false;
ledOn(Yellow_led);
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
DEBUG_PRINT(F("Connecting to network"));
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin((char*)ssid, (char*)wifiPass);
// wait 5 seconds for connection:
delay(5000);
}
DEBUG_PRINT(F("Connected to wifi"));
#ifdef DEBUG
printWifiStatus();
#endif
// if still empty initialize ClientId
if (ClientId[0] == 0) {
MQTTgetClientId(ClientId);
}
// try to connect to MQTT server
while (!client.connected()) {
DEBUG_PRINT(F("Connecting to MQTT server"));
strcpy(printbuf, baseTopic);
strcat(printbuf, alarmTopic); // will topic
//retval = client.connect(mqttClientId, (char*)mqttUser, (char*)mqttPass, printbuf, 0, true, "Client lost connection");
retval = client.connect(mqttClientId, (char*)mqttUser, (char*)mqttPass);
delay(5000);
}
DEBUG_PRINT(F("Connected to MQTT server"));
ledOn(Green_led);
return retval;
}
//return the temperature from one DS18S20 in DEG Celsius
float getTemp() {
byte data[12];
byte addr[8];
if (!ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
ledOn(Red_led);
return -1000;
}
if (OneWire::crc8(addr, 7) != addr[7]) {
DEBUG_PRINT(F("CRC is not valid!"));
ledOn(Red_led);
return -1000;
}
if (addr[0] != 0x10 && addr[0] != 0x28) {
DEBUG_PRINT(F("Device is not recognized"));
ledOn(Red_led);
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
// start frequency output on freqOut_pin
//void playFreq(int freq) {
// delay(curFreq * 1.3);
// curFreq = freq;
// tone(freqOut_pin, freq);
//
//}
// frequency measure
float frequencyRead() {
float frequency = -1;
if (FreqMeasure.available()) {
// average several reading together
freqSum += FreqMeasure.read();
if (freqCount++ > freqMaxCount) {
frequency = FreqMeasure.countToFrequency(freqSum / freqCount);
freqSum = 0;
freqCount = 0;
}
}
return frequency;
}