-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.ino
95 lines (84 loc) · 2.65 KB
/
mqtt.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
char effecttopic[40];
char startuptopic[40];
char cycletopic[40];
char offtopic[40];
char value[5];
#ifdef ENABLE_MQTT
// char mqtt_intopic[strlen(HOSTNAME) + 4 + 5]; // Topic in will be: <HOSTNAME>/in
// char mqtt_outtopic[strlen(HOSTNAME) + 5 + 5]; // Topic out will be: <HOSTNAME>/out
#define mqtt_server "192.168.1.131"
//strncpy(MQTTHOST,HOSTNAME,20);
#define mqtt_user "art1"
#define mqtt_password "art1"
#define mqtt_port 1883
#endif // of ifdef enable_mqtt
void mqtt_setup(){
client.setServer(mqtt_server, mqtt_port); // this established connection
client.setCallback(mqttcallback); // this tells where to send us if we get a message
sprintf(effecttopic, "%s/%s", HOSTNAME, "effect");
sprintf(startuptopic, "%s/%s", HOSTNAME, "startup");
sprintf(cycletopic, "%s/%s", HOSTNAME, "cycle");
sprintf(offtopic, "%s/%s", HOSTNAME, "off");
mqttconnect();
mqttpublish();
mqttsubscribe();
}
void mqttconnect(){
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
client.connect(HOSTNAME, mqtt_user, mqtt_password);
if (client.connect(HOSTNAME, mqtt_user, mqtt_password )) {
Serial.println("mqtt connected");
}
}
}
void mqttpublish(){
String tt = String(selectedeffectno);
sprintf(value,"%u",selectedeffectno);
client.publish(effecttopic,value,false);
client.publish(startuptopic,(autoeffects ==1)? "1": "0",false);
client.publish(cycletopic,(randomeffects == 1)? "1": "0",false);
client.publish(offtopic,(autoeffects ==1)? "1": "0",false);
}
void mqttsubscribe(){
client.subscribe(effecttopic);
client.subscribe(startuptopic);
client.subscribe(cycletopic);
client.subscribe(offtopic);
}
void mqttcallback(String topic, byte* payload, unsigned int length) {
String msg = "";
Serial.print("Message received in topic: ");
Serial.println(topic);
Serial.print("payload length was: ");
Serial.println(length);
payload[length] = '\0';
msg = (char*)payload;
Serial.print("Message:");
Serial.println(msg);
if (topic == String(effecttopic)) {
Serial.print("mqtt effect message received: ");
Serial.println(msg);
selectedeffectno=msg.toInt();
savesettings();
}
if (topic == String(startuptopic)) {
Serial.print("mqtt startup message received: ");
Serial.println(msg);
if (msg == "1") { autoeffects=1; } else { autoeffects=0; }
savesettings();
}
if (topic == String(cycletopic)) {
Serial.print("Cycle topic message received: ");
Serial.println(msg);
if (msg == "1") { randomeffects = 1; } else { randomeffects=0; }
savesettings();
}
if (topic == String(offtopic)) {
Serial.print("turn off topic received: ");
autoeffects=0;
savesettings();
reboot();
Serial.println(msg);
}
}