Reputation: 1129
I have a small local IoT network with a handful of Wemos D1 nodes programmed in the Arduino IDe. Some of them have two sensors connected, e.g. rain and humidity.
I want to send out values every minute, one minute the rain, one minute the humidity.
Below code generally works with delay(5000);
for both, but once I have something like delay(60000);
the node apparently starts only publishing the rain and skips the humidity (I have a mosquitto_sub -h myIP -v -t '#'
running to listen for messaged.
This is my loop()
where all the sensing and sending happens:
void loop() {
if (!client.connected() && WiFi.status() == 3) {
reconnect();
}
client.loop();
int val = digitalRead(rainSensor); // read the input pin
rain_str = String(val);
rain_str.toCharArray(rain, rain_str.length() + 1);
client.publish(rainTopic, rain);
delay(60000);
int val2 = analogRead(humidSensor); // read the input pin
humid_str = String(val2);
humid_str.toCharArray(humid, humid_str.length() + 1);
client.publish(humidTopic, humid);
delay(60000);
}
Upvotes: 3
Views: 3532
Reputation: 61
You will get a MQTT_CONNECTION_TIMEOUT if you are not calling the client.loop() function within MQTT_KEEPALIVE seconds (typically 15 s).
Make sure to call the client.loop() with much shorter interval. Use a counter to to count to 60 or better use a timer function like the Ticker library.
Upvotes: 4