1

I have a loop, that iterates through my sensors (items), and then sends their state via MQTT. I am running that code on ESP8266 dev boards, and on few on them, this code works, whereas on the others, it throws an exception after a random amount of time.

Now, trying to debug this, I have discovered, that, if I uncomment Serial.println() in the line as below (it has to be that exact place, putting it on any other line, doesn't do the trick), code works perfectly stable for days. But as soon as I remove that println, it starts crashing after more or less 100 seconds. What is happening here? What is Serial.println() doing, that makes my code stable?

void _loop(String priority)
{
    for(std::vector<IItem*>::iterator it = items.begin(); it != items.end(); ++it) 
    {
        if ((*it)->loopPriority == priority)
        {
            (*it)->loop();
            for (std::map<String, String>::iterator pubChannel = (*it)->pubChannels.begin(); pubChannel != (*it)->pubChannels.end(); pubChannel++ )
            {
                //Serial.println(pubChannel->second);
                mqtt.sendMsg(pubChannel->second, (*it)->command(pubChannel->first));
            }
        }
    }
}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
ojek
  • 9,680
  • 21
  • 71
  • 110
  • Serial.println() is spending time to send data over serial. Try replacing it with sleep. – Erki Aring Jun 07 '18 at 21:09
  • I did already. This is not it. Sleeping still causes exceptions, unlike println. I even incerased sleep amount by 100ms with each iteration, still exception was there. – ojek Jun 07 '18 at 21:51
  • Since we're fishing for a mystery bug, `_loop` is probably an illegal identifier. Anything that starts with an underscore in the global namespace is reserved for use by the library implementation. For more on that read [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Jun 07 '18 at 23:13
  • You should show complete code. The problem may not be where you think it is. I've seen plenty of examples where a buffer overrun in one part of the code causes it to break and adding a print line at some seemingly random place moves things in memory enough to make it appear to work. – Delta_G Jun 08 '18 at 02:45
  • Thanks, here's my code, didn't include the link as I didn't think anyone would want to read that pile of code ;) https://github.com/ojek/OpenHabHomeAutomation/blob/master/arduino/ESP8266/ESP8266.ino – ojek Jun 08 '18 at 17:13

0 Answers0