Hi,
I'm using the MQTT.h library with success in one of my project using Adruino UNO w/Ethershield based on your ArduinoEthernetShield.ino exemple.
I wanted to add to the loop a timestamp based on RTC clock every second. (DS3231 - Tested and working)
Once the RTC commands is added to the "if (millis() - lastMillis > 1000)" time gets displayed every seconds, but the reception of MQTT messages stops working.
As if the "void messageReceived(String &topic, String &payload)" is not triggered anymore by incoming messages.
I can confirm the arduino is connected to broker and subscribe to topic.
Thx for any helpfull infos.
Here is my code sample:
#include <Ethernet.h>
#include <MQTTClient.h>
#include <MQTT.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
...
...
...
void loop() {
client.loop();
if (!client.connected()) {
Serial.print("\nNOT CONNECTED TO BROKER....");
connect();
}
// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
Serial.print("\nDEBUG\n");
DateTime now = rtc.now(); //THIS CAUSE ISSUE
Serial.print(now.hour(), DEC); //THIS CAUSE ISSUE
Serial.print(':'); //THIS CAUSE ISSUE
Serial.print(now.minute(), DEC); //THIS CAUSE ISSUE
Serial.print(':'); //THIS CAUSE ISSUE
Serial.print(now.second(), DEC); //THIS CAUSE ISSUE
Serial.println(); //THIS CAUSE ISSUE
}
}
I expected that use of serial was slowing your code down but the baud rate suggests not. What happens if you print the same amount of text, but leave the rtc out of it?
In fact, at fist I only printed one line of RTC infos (seconds). And issue appeared.
If RTC is involved, RTC works and prints correct date&time, but MQTT wont be analysing incoming messages on my subscibed topic....