-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt.ino
118 lines (96 loc) · 2.33 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// MQTT
long lastMsg = 0;
char msg[50];
int value = 0;
// setup server connction and connect
void mqttSetup()
{
Serial.println("-setup MQTT");
mqttClient.setServer(mqtt_server, 1883);
mqttClient.setCallback(callback);
mqttReconnect();
}
// incoming messages
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if (topic == topicStatusIn
&& length == 4)
{
if ((char)payload[0] == 't'
&& (char)payload[1] == 'e'
&& (char)payload[2] == 's'
&& (char)payload[3] == 't')
{
Serial.println("test request!");
mqttPublish(topicStatusOut, "test requested! I´m alive!", true);
}
else if ((char)payload[0] == 's'
&& (char)payload[1] == 'c'
&& (char)payload[2] == 'a'
&& (char)payload[3] == 'n')
{
Serial.println("wifi scan request!");
scan_wifi_to_mqtt();
}
}
else if (topic == topicIn)
{
payload[length] = '\0';
String s = String((char*)payload);
int code = s.toInt();
// codes to send
rf_send(code);
}
}
// connect to server and subscrive topics
void mqttReconnect()
{
// Loop until we're reconnected
while (!mqttClient.connected())
{
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqttClient.connect(HOSTNAME))
{
Serial.println("connected");
// Once connected, publish an announcement...
mqttPublish(topicStatusOut, "reconnect: I´m alive!", true);
// ... and resubscribe
mqttClient.subscribe(topicStatusIn);
mqttClient.subscribe(topicIn);
}
else
{
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// publish a message to the mqtt server
void mqttPublish(const char* chan, String text, bool addHostname)
{
String msg = "";
if (addHostname)
{
msg += String(HOSTNAME);
msg += " ";
}
msg += text;
msg += " ";
// make a char array
char b[msg.length() + 2];
// copy string to array
msg.toCharArray(b, msg.length());
// publish
mqttClient.publish(chan, b);
}