-
Notifications
You must be signed in to change notification settings - Fork 2
/
MQTT.ino
103 lines (85 loc) · 3.42 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
bool mqttConnect() { //
/* this function checks if we are connected to the broker, if not connect anyway */
if( MQTT_Client.connected() ) {
consoleOut("mqtt was connected");
return true;
}
// we are here because w'r not connected. Signal with the LED
ledblink(2,70);
if (Mqtt_Port[0] == '\0' ) strcpy(Mqtt_Port, "1883"); // just in case ....
uint8_t retry = 3;
//char Mqtt_inTopic[11]={"ESP-ECU/in"};
while (!MQTT_Client.connected()) {
if ( MQTT_Client.connect( Mqtt_Clientid, Mqtt_Username, Mqtt_Password) )
{
//connected, so subscribe to inTopic (not for thingspeak)
if(Mqtt_Format != 5 ) {
if( MQTT_Client.subscribe ( "ESP32-ECU/in" ) ) {
//if( MQTT_Client.subscribe ( Mqtt_inTopic ) ) {
//if(diagNose) ws.textAll("subscribed to " + String(Mqtt_inTopic ));
consoleOut("subscribed to ESP32-ECU/in");
}
}
consoleOut(F("mqtt connected"));
Update_Log(3, "connected");
return true;
} else {
//String term = "connection failed state: " + String(MQTT_Client.state());
Update_Log(3, "failed");
if (!--retry) break; // stop when tried 3 times
delay(500);
}
}
// if we are here , no connection was made.
consoleOut(F("mqtt connection failed"));
return false;
}
// *************************************************************************
// process received mqtt
// *************************************************************************
void MQTT_Receive_Callback(char *topic, byte *payload, unsigned int length)
{
// String Payload = ""; // convert the payload to a String...
// for (int i = 0; i < length; i++)
// {
// Payload += (char)payload[i]; // convert to char, needed???
// }
// ws.textAll("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
DeserializationError error = deserializeJson(doc, payload); // Deserialize the JSON document
if (error) // Test if parsing succeeds.
{
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") )
{
int inv = doc["poll"].as<int>();
consoleOut( "got message {\"poll\":" + String(inv) + "}" );
if(!Polling)
{
//ws.textAll( "found {\"poll\" " + String(inv) + "}\"" );
iKeuze = inv;
if(iKeuze == 99) {
actionFlag = 48; // takes care for the polling of all inverters
return;
}
if ( iKeuze < inverterCount )
{
actionFlag = 47; // takes care for the polling
return;
} else {
consoleOut("mqtt error no inv " + String(iKeuze));
return;
}
}
else
{
consoleOut("polling = automatic, skipping");
}
}
}