Add reconnect if the connection dies
this time, by recalling mqtt.connect apparently it didn't like recreating the objects very much
This commit is contained in:
parent
752045d4b0
commit
491b87c0b3
1 changed files with 40 additions and 24 deletions
|
@ -57,10 +57,10 @@
|
|||
// Ref https://stackoverflow.com/a/523737/1460422
|
||||
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
|
||||
|
||||
#ifndef MQTT_NO_TLS
|
||||
WiFiClientSecure transport;
|
||||
#else
|
||||
#ifdef MQTT_NO_TLS
|
||||
WiFiClient transport;
|
||||
#else
|
||||
WiFiClientSecure transport;
|
||||
#endif
|
||||
PubSubClient mqtt;
|
||||
|
||||
|
@ -113,6 +113,40 @@ float read_analog(int channel) {
|
|||
return parsedValue;
|
||||
}
|
||||
|
||||
void connect_mqtt() {
|
||||
if(!mqtt.connect(
|
||||
("autoplant_" + String(random(0, 999999))).c_str(),
|
||||
MQTT_USERNAME, MQTT_PASSWORD)
|
||||
) {
|
||||
/* Ref https://pubsubclient.knolleary.net/api#state
|
||||
-4 : MQTT_CONNECTION_TIMEOUT - the server didn't respond within the keepalive time
|
||||
-3 : MQTT_CONNECTION_LOST - the network connection was broken
|
||||
-2 : MQTT_CONNECT_FAILED - the network connection failed
|
||||
-1 : MQTT_DISCONNECTED - the client is disconnected cleanly
|
||||
0 : MQTT_CONNECTED - the client is connected
|
||||
1 : MQTT_CONNECT_BAD_PROTOCOL - the server doesn't support the requested version of MQTT
|
||||
2 : MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier
|
||||
3 : MQTT_CONNECT_UNAVAILABLE - the server was unable to accept the connection
|
||||
4 : MQTT_CONNECT_BAD_CREDENTIALS - the username/password were rejected
|
||||
5 : MQTT_CONNECT_UNAUTHORIZED - the client was not authorized to connect
|
||||
*/
|
||||
int error_code = mqtt.state();
|
||||
Serial.println("failed, error code "+String(error_code));
|
||||
while(true) delay(10000);
|
||||
}
|
||||
Serial.println("ok");
|
||||
}
|
||||
|
||||
void check_mqtt() {
|
||||
if(mqtt.connected() == 0) {
|
||||
Serial.print("MQTT reconnect: ");
|
||||
connect_mqtt();
|
||||
}
|
||||
else {
|
||||
Serial.println("MQTT: connection ok");
|
||||
}
|
||||
}
|
||||
|
||||
bool send_mqtt(String id, String sensor, float value) {
|
||||
// StaticJsonDocument<96> data;
|
||||
//
|
||||
|
@ -125,6 +159,7 @@ bool send_mqtt(String id, String sensor, float value) {
|
|||
// char* payload = new char[length]();
|
||||
// serializeJson(data, payload, length);
|
||||
|
||||
check_mqtt();
|
||||
bool result = mqtt.publish("sensors/data", payload.c_str());
|
||||
// delete[] payload;
|
||||
|
||||
|
@ -177,32 +212,13 @@ void setup() {
|
|||
// mqtt.setBufferSize(1000);
|
||||
mqtt.setClient(transport); Serial.print(".");
|
||||
mqtt.setServer(MQTT_SERVER, MQTT_PORT); Serial.print(".");
|
||||
mqtt.setKeepAlive(INTERVAL / 2);
|
||||
// Serial.print(MQTT_SERVER);
|
||||
// Serial.print(":");
|
||||
// Serial.print(MQTT_PORT);
|
||||
// Serial.print(".");
|
||||
|
||||
if(!mqtt.connect(
|
||||
("autoplant_" + String(random(0, 999999))).c_str(),
|
||||
MQTT_USERNAME, MQTT_PASSWORD)
|
||||
) {
|
||||
/* Ref https://pubsubclient.knolleary.net/api#state
|
||||
-4 : MQTT_CONNECTION_TIMEOUT - the server didn't respond within the keepalive time
|
||||
-3 : MQTT_CONNECTION_LOST - the network connection was broken
|
||||
-2 : MQTT_CONNECT_FAILED - the network connection failed
|
||||
-1 : MQTT_DISCONNECTED - the client is disconnected cleanly
|
||||
0 : MQTT_CONNECTED - the client is connected
|
||||
1 : MQTT_CONNECT_BAD_PROTOCOL - the server doesn't support the requested version of MQTT
|
||||
2 : MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier
|
||||
3 : MQTT_CONNECT_UNAVAILABLE - the server was unable to accept the connection
|
||||
4 : MQTT_CONNECT_BAD_CREDENTIALS - the username/password were rejected
|
||||
5 : MQTT_CONNECT_UNAUTHORIZED - the client was not authorized to connect
|
||||
*/
|
||||
int error_code = mqtt.state();
|
||||
Serial.println("failed, error code "+String(error_code));
|
||||
while(true) delay(10000);
|
||||
}
|
||||
Serial.println("ok");
|
||||
connect_mqtt();
|
||||
|
||||
Serial.print("BME280: ");
|
||||
|
||||
|
|
Loading…
Reference in a new issue