-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmqtt.h
80 lines (71 loc) · 1.75 KB
/
mqtt.h
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 "PubSubClient.h"
#include "system.h"
#include <WiFiClient.h>
WiFiClient wifi_client;
PubSubClient client;
bool mqtt_connected;
bool setup_mqtt(char* server) {
mqtt_connected = false;
client.setClient(wifi_client);
client.setServer(server, 1883);
}
bool connect_mqtt() {
if (!mqtt_connected) {
int attempts = 0;
while (!client.connected() && attempts <= 3) {
++attempts;
Serial.print("Connecting MQTT...");
// Attempt to connect
const char* client_name = "MudPi_" + DEVICE_ID;
if (client.connect(client_name)) {
Serial.println("Success");
// Subscribe
// client.subscribe(topic);
mqtt_connected = true;
} else {
Serial.print("Failed, rc=");
Serial.println(client.state());
delay(500);
mqtt_connected = false;
}
}
if(!mqtt_connected) {
Serial.println('MQTT Failed to Connect');
return false;
}
}
return true;
}
bool disconnect_mqtt() {
client.disconnect(); //Disconnect mqtt
return true;
}
bool send_mqtt(char* topic, String data) {
Serial.print("Sending readings to MQTT server...");
if(mqtt_connected) {
int send_attempts = 0;
// Try to post the data up to three times
bool sent_successfully = false;
while(send_attempts < 3 && ! sent_successfully) {
sent_successfully = client.publish(topic, data.c_str());
client.loop();
send_attempts++;
if(sent_successfully) {
Serial.println("Success! ");
delay(500);
client.loop();
}
else {
Serial.println("Failed");
delay(1000);
}
}
// Add failed request data to array
if(!sent_successfully) {
Serial.println("Failed to Send to MQTT Server!");
return false;
}
return true;
}
return false;
}