/
03_mqtt_send_and_callback.ino
86 lines (72 loc) · 2.18 KB
/
03_mqtt_send_and_callback.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
/**
* 03. MQTT send and callback example
*
* Send MQTT messages to the IoT Guru cloud and register a callback function to handle incoming messages.
*
* You need:
* - user short identifier (you can find it on the Account page)
* - the device short identifier (you can find it on the Device page)
* - the device key (you can find it on the Device page)
*
* Tutorial: https://iotguru.live/tutorials/devices
*
* Also you need:
* - the node's key (you can find it on the Node page)
* - the field name (you can find it ont the Field page)
*
* Tutorial: https://iotguru.live/tutorials/nodes
* Tutorial: https://iotguru.live/tutorials/fields
*
* You can push MQTT messages to your device by using our API:
*
* https://api.iotguru.live/mqtt/send/{nodeKey}/{fieldName}/{message}
*/
#include <IoTGuru.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif
const char* ssid = "ssid";
const char* password = "password";
WiFiClient client;
String userShortId = "xxxxxxxxxxxxxxxxxxxxxx";
String deviceShortId = "yyyyyyyyyyyyyyyyyyyyyy";
String deviceKey = "zzzzzzzzzzzzzzzzzzzzzz";
IoTGuru iotGuru = IoTGuru(userShortId, deviceShortId, deviceKey);
String nodeShortId = "wwwwwwwwwwwwwwwwwwwwww";
String fieldName = "ffffNNNN";
void setup() {
Serial.begin(115200);
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(50);
Serial.print(".");
}
Serial.println("");
/**
* Set the callback function.
*/
iotGuru.setCallback(&callback);
/**
* Set the debug printer (optional).
*/
iotGuru.setDebugPrinter(&Serial);
/**
* Set the network client.
*/
iotGuru.setNetworkClient(&client);
}
volatile unsigned long nextSendUptime = 0;
void loop() {
iotGuru.loop();
if (nextSendUptime < millis()) {
nextSendUptime = millis() + 60000;
iotGuru.sendMqttValue(nodeShortId, fieldName, millis()/1000.0f);
}
}
void callback(const char* nodeShortId, const char* fieldName, const char* message) {
Serial.print(nodeShortId);Serial.print(" - ");Serial.print(fieldName);Serial.print(": ");Serial.println(message);
}