Closed asbs-eng closed 1 week ago
Unfortunately, the library doesn't queue messages for sending while disconnected from the broker.
You can implement such logic in your sketch, outside the library.
To know if a qos=1 message was delivered to the broker, check the value returned by mqtt.publish
. True means it was delivered and confirmed by the broker, false means something went wrong.
OK, thank you for the quick response I can record unsent data to spiffs or sd card and send it once the network is connected
OK, thank you for the quick response I can record unsent data to spiffs or sd card and send it once the network is connected
Small advice, to avoid premature failure of Flash you should buffer data in RAM instead, SD if you have big messages.
I use esp32 to publish data and raspberry pi as a broker and subscribe data. To keep the data received by the subscriber even if the connection is lost for a long time, I tried using qos = 1. When the connection is disconnected for a long time and then connected again. Data sent during the disconnected connection is not sent to the subscriber.
include
include
include
include
int LED_BUILTIN = 2; int cnt = 0;
const char ssid = "xxxxxxxx"; const char password = "xxxxxxxx";
IPAddress local_ip(192,168,4,100); IPAddress gateway(192,168,4,0); IPAddress subnet(255,255,255,0);
String iot_topic = ""; String id_dev = "0001"; String loccode = "qc/1014/01";
const char broker[] = "192.168.4.50"; int port = 1883; PicoMQTT::Client mqtt(broker);
static struct pt pt1;
static int protothreadBlinkLED(struct pt *pt) { static unsigned long lastTimeBlink = 0; PT_BEGIN(pt); while(1) { mqtt.publish("qc/counter",String(cnt),1,false,1); lastTimeBlink = millis();
PT_WAIT_UNTIL(pt, millis() - lastTimeBlink > 1000); Serial.println(String(cnt)); cnt = cnt + 1; if (cnt > 10000) { cnt=0; } } PT_END(pt); }
void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); WiFi.config(local_ip, gateway, subnet); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(1000); } Serial.print("connected !"); PT_INIT(&pt1); }
void loop() { mqtt.loop(); protothreadBlinkLED(&pt1); }
Regards
Andreas Budi