-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_tutorial.ino
49 lines (40 loc) · 1.08 KB
/
mqtt_tutorial.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
#include "MQTT/MQTT.h"
void callback(char* topic, byte* payload, unsigned int length);
/**
* if want to use IP address,
* byte server[] = { XXX,XXX,XXX,XXX };
* MQTT client(server, 1883, callback);
* want to use domain name,
* MQTT client("www.sample.com", 1883, callback);
**/
MQTT client("192.168.43.35", 1883, callback);
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
if (message.equals("RED"))
RGB.color(255, 0, 0);
else if (message.equals("GREEN"))
RGB.color(0, 255, 0);
else if (message.equals("BLUE"))
RGB.color(0, 0, 255);
else
RGB.color(255, 255, 255);
delay(1000);
}
void setup() {
RGB.control(true);
// connect to the server
client.connect("192.168.43.35");
// publish/subscribe
if (client.isConnected()) {
client.publish("/outTopic","hello world");
client.subscribe("/inTopic");
}
}
void loop() {
if (client.isConnected())
client.loop();
}