forked from patience4711/ESP32-read-APS-inverters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMQTT.ino
105 lines (89 loc) · 3.9 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
bool mqttConnect() { // MQTT connection (documented way from AutoConnect : https://github.com/Hieromon/AutoConnect/tree/master/examples/mqttRSSI_NA)
// we are here because w'r not connected. Signal with the LED
ledblink(2,70);
if ( Mqtt_Broker[0] == '\0' || Mqtt_Broker[0] == '0' )
{
Mqtt_Format = 0; // we don't try again
//DebugPrintln("no broker, cancel");
return false;
}
if( diagNose != 0 )("mqtt try to connect to " + String(Mqtt_Broker));
//DebugPrintln("connecting to mqtt");
if (Mqtt_Port[0] == '\0' ) strcpy(Mqtt_Port, "1883"); // just in case ....
uint8_t retry = 3;
// We generate a unique name for this device to avoid mqtt problems
// in case if you have multiple RFlink-ESP devices
String clientId = getChipId(false);
while (!MQTT_Client.connected()) {
if (MQTT_Client.connect(clientId.c_str() , Mqtt_Username, Mqtt_Password))
{
if( diagNose != 0 ) consoleOut("MQTT connection Established with ID : " + clientId);
//connected, so we and subscribe to inTopic
MQTT_Client.subscribe ( Mqtt_inTopic ) ; //
if( diagNose != 0 ) consoleOut("\nMQTT subscribed on topic " + String(Mqtt_inTopic));
Update_Log("mqtt", "connected");
return true;
}
delay(500);
if (!--retry) {
//String term = "connection failed state: " + String(MQTT_Client.state());
Update_Log("mqtt", "connection failed");
if( diagNose != 0 ) consoleOut("MQTT connection failed");
break;
}
}
// if we are here, connection failed after 3 attempts
// Mqtt_Enabled = false; // we don't try again
return false;
}
// *************************************************************************
// process received mqtt
// *************************************************************************
void MQTT_Receive_Callback(char *topic, byte *payload, unsigned int length)
{
//Serial.println("MQTT_message arrived in " + String(Mqtt_inTopic) );
//Serial.println("length of the message = " + String(length));
//String SerialRFcmd = "";
String Payload = ""; // convert the payload to a String...
for (int i = 0; i < length; i++)
{
Payload += (char)payload[i]; // convert to char, nodig???
}
if( diagNose != 0 ) consoleOut("mqtt received " + Payload);
StaticJsonDocument<1024> doc; // We use json library to parse the payload
// The function deserializeJson() parses a JSON input and puts the result in a JsonDocument.
DeserializationError error = deserializeJson(doc, Payload); // Deserialize the JSON document
if (error) // Test if parsing succeeds.
{
if( diagNose != 0 ) consoleOut("mqtt no valid json ");
return;
}
// We check the kind of command format received with MQTT
//now we have a payload like {"poll",1}
if( doc.containsKey("poll") )
{
if(!Polling)
{
int inv = doc["poll"].as<int>();
//DebugPrintln( "found {\"poll\" " + String(inv) + "}\"" );
iKeuze = inv;
if(iKeuze == 99) {
if( diagNose != 0 ) consoleOut( "found {\"poll\" " + String(inv) + "}\"" );
actionFlag = 48; // takes care for the polling of all inverters
return;
}
if ( iKeuze < inverterCount )
{
actionFlag = 47; // takes care for the polling
return;
} else {
if( diagNose != 0 ) consoleOut("mqtt error no inv " + String(iKeuze));
return;
}
}
else
{
if( diagNose != 0 ) consoleOut("polling = automatic, skipping");
}
}
}