This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_tableleds.ino
67 lines (57 loc) · 1.59 KB
/
mqtt_tableleds.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
#include <string.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0x01 };
byte server[] = { 10, 0, 0, 4 };
byte ip[] = { 10, 0, 0, 33 };
PubSubClient client(server, 1883, callback);
void callback(char* topic, uint8_t* payload,unsigned int length) {
Serial.println("msg received");
// handle message arrived
if( String(topic) == "announce" ) {
Serial.println("received announcement");
if( length == 10 and strncmp((char*)payload,"space-open",length) == 0 ) {
analogWrite(3,255);
analogWrite(5,255);
analogWrite(6,255);
} else if ( length == 12 and strncmp((char*)payload,"space-closed",length) == 0 ) {
analogWrite(3,0);
analogWrite(5,0);
analogWrite(6,0);
}
}
if( String(topic) == "MainTableLEDs" ) {
Serial.println("received base command");
if(length == 3 ) {
analogWrite(3,payload[0]);
analogWrite(5,payload[1]);
analogWrite(6,payload[2]);
}
}
}
void setup()
{
Serial.println("MainTableController Starting...");
Serial.begin(9600);
Ethernet.begin(mac, ip);
analogWrite(3,0);
analogWrite(5,0);
analogWrite(6,0);
while( !client.connected() ) {
if (client.connect("MainTableController")) {
client.publish("announce","MainTableController");
client.subscribe("announce");
client.subscribe("MainTableLEDs");
Serial.println("ok");
} else {
delay(1000);
}
}
Serial.println("Done Setup");
}
void loop()
{
client.loop();
}