-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtttest.ino
80 lines (64 loc) · 1.89 KB
/
mqtttest.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
#include "MQTT.h"
/* Testcomment */
void callback(char* topic, byte* payload, unsigned int length);
byte server[] = { 192,168,43,35 };
MQTT client(server, 1883, callback);
byte bytebuffer[100];
// 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);
String message = "RED";
client.publish("sparkcore/debug",message);
}
else if (message.equals("GREEN")) {
RGB.color(0, 255, 0);
String message = "GREEN";
message.getBytes(bytebuffer, 6);
client.publish("sparkcore/debug",bytebuffer, 6);
}
else if (message.equals("BLUE")) {
RGB.color(0, 0, 255);
String message = "BLUE";
message.getBytes(bytebuffer, 5);
client.publish("sparkcore/debug",bytebuffer, 5);
}
else
RGB.color(255, 255, 255);
delay(1000);
}
void setup() {
/* Init button */
pinMode(D0, INPUT);
pinMode(A1, INPUT);
RGB.control(true);
// connect to the server
client.connect("spark_client");
// publish/subscribe
if (client.isConnected()) {
String message = "SparkCore Connected";
message.getBytes(bytebuffer, 20);
client.publish("sparkcore/debug",bytebuffer, 20);
client.subscribe("sparkcore/RGBled");
client.publish("sparkcore/button",bytebuffer, 20);
}
}
int pirValue;
void loop() {
if (client.isConnected())
client.loop();
/* Check button value */
if (digitalRead(D0)) {
client.publish("sparkcore/button", "test_measurement2 test_key=123");
while(digitalRead(D0));
delay(10);
}
/* Read analog value */
/* pirValue = analogRead(A1); */
/* delay(500); */
/* client.publish("analogread", String(pirValue)); */
}