Sorry, dont want to paste big amount of code,
I don’t use particle.cloud function (only for dev and flash). So “client” is:
byte server[] = { 192,168,1,23};
MQTT client(server, 1883, callback);
This server in my local network.
publish_message("home/water_count/rssi", WiFi.RSSI(), true); // <-- not shown!
check MQTT connection and publicate mesage to mqtt broker
client.loop(); // <-- does this make sense if !WiFi.ready() or !client.isConnected() ???
check MQTT connection and make background work for MQTT send\recieve
I fix my other home node - light and ventilation control - and make video with this second reconnect. This reconnection main troublemaker.
My resulting code (after my hack with wifi_uptime control):
// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"
#define DEBUG 0
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(MANUAL);
STARTUP(WiFi.selectAntenna(ANT_INTERNAL));
unsigned long currentMillis = 0;
void callback(char* topic, byte* payload, unsigned int length);
byte server[] = { 192,168,1,23};
MQTT client(server, 1883, callback);
unsigned long previous_conected = 100000; //финт ушами
unsigned long previous_wifi_uptime = 100000; //финт ушами
unsigned long wifi_uptime = 0;
bool publish_message(const char* t, const char* p, bool retain)
{
return client.publish(t, (uint8_t*)p, sizeof(p), retain);
}
bool publish_message(const char* t, int p, bool retain)
{
char buf5[12];
int n = sprintf(buf5,"%d",p);
return client.publish(t, (uint8_t*)buf5, n, retain);
}
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
String t(topic);
if (t.equals("home/water_count/spark/set"))
{
cloud_tink(message.equalsIgnoreCase("ON"));
}
}
void setup() {
#if DEBUG == 1
Serial.begin(9600); Serial.println("Serial Start");
#endif
WiFi.on();
WiFi.connect();
if (waitFor(WiFi.ready, 5000))
{
mqtt_connect();
}
}
void loop() {
currentMillis = millis();
// проверяем наличие сети и подключения к MQTT брокеру
if (currentMillis - previous_conected >= 30000 || previous_conected > currentMillis)
{
previous_conected = currentMillis;
if (!client.isConnected() & wifi_uptime > 60)
{
mqtt_connect();
}
publish_message("home/water_count/rssi", WiFi.RSSI(), true);
}
if (currentMillis - previous_wifi_uptime >= 1000 || previous_wifi_uptime > currentMillis)
{
previous_wifi_uptime = currentMillis;
WiFi.ready() ? wifi_uptime++ : wifi_uptime = 0;
}
client.loop();
}
void mqtt_connect()
{
if (client.connect("water_count"))
{ //подпись на spark и публикуем послднее состояние
client.subscribe("home/water_count/spark/set");
publish_message("home/water_count/spark", Particle.connected() ? "ON" : "OFF", true);
client.subscribe("home/water_count/+/set");
}
}
void cloud_tink(bool command)
{
if (command)
{
Particle.connect();
if (waitFor(Particle.connected, 10000))
{client.publish("home/water_count/spark", "ON");}
else
{Particle.disconnect(); client.publish("home/water_count/spark", "OFF");}
}
else
{
Particle.disconnect();
client.publish("home/water_count/spark", "OFF");
}
}