256dpi / arduino-mqtt

MQTT library for Arduino
MIT License
1.01k stars 232 forks source link

"messageReceived" not working when inserting RTC timestamp in loop? #204

Closed MrSiO closed 3 years ago

MrSiO commented 4 years ago

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
  }
}
256dpi commented 4 years ago

I think you accidentally removed the client.publish() call within the if statement?

MrSiO commented 4 years ago

Hi. This is a trunkated version.

For now It's only listening on topic and reacts to incoming topic/payload. Only publishes state later on in the code. All working fine before I introduce RTC (I2C).

Problem seems to be related to I2C and maybe interrupts...

Looks like the I2C interfere with the MQTT incoming message interrupt... Is this possible?

256dpi commented 3 years ago

Closing as outdated. Please reopen if problem persists.