-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.ino
62 lines (56 loc) · 1.71 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
/*
* ESP8266 Energy Meter
*
* Written by Michele <[email protected]> Pinassi
* Released under GPLv3 - No any warranty
*
* MQTT procedures
*
*/
void mqttCallback(char* topic, byte* payload, unsigned int length) {
DEBUG_PRINTLN("Message arrived: "+String(topic));
}
// ************************************
// mqttTaskCB()
//
//
// ************************************
void mqttTaskCB() {
char topic[32];
// MQTT not connected? Connect!
if (!mqttClient.connected()) {
if(!mqttConnect()) {
// If connection problem, return
env["status"] = "MQTT connection error"+String(mqttClient.state());
return;
}
}
// Now prepare MQTT message...
JsonObject root = env.as<JsonObject>();
for (JsonPair keyValue : root) {
sprintf(topic,"%s/%s",config.client_id,keyValue.key().c_str());
String value = keyValue.value().as<String>();
if(mqttClient.publish(topic,value.c_str())) {
DEBUG_PRINTLN("[MQTT] Publish "+String(topic)+":"+value);
} else {
DEBUG_PRINTLN("[MQTT] Publish failed!");
env["status"] = "MQTT publish failed: "+String(mqttClient.state());
}
}
}
bool mqttConnect() {
if(strlen(config.broker_host) > 0) {
DEBUG_PRINT("[MQTT] Attempting connection to "+String(config.broker_host)+":"+String(config.broker_port));
mqttClient.setServer(config.broker_host,config.broker_port);
mqttClient.setCallback(mqttCallback);
if (mqttClient.connect(config.client_id)) {
DEBUG_PRINTLN("[MQTT] Connected as "+String(config.client_id));
env["status"] = "MQTT connected as "+String(config.client_id);
return true;
} else {
DEBUG_PRINTLN("[MQTT] Connection failed");
return false;
}
}
return false;
}