Go Down

Topic: Need Guidance Regarding My code. as it stops working after 5-6 Hours (Read 205 times) previous topic - next topic


Hello All,
As i am new into microcontroller stuff but knows about coding, so i want to make a stable IOT Proj and i have written the code. the problem here i am facing is the code works for 5-6 hours or a day, and then it stops responding.

I had a research regarding this and was concerned about

1.String should not be used in the Arduino is what i found.(and i have used a lot)
2.Heap,memory leak issue

If anybody could direct me the code i have written . how can i optimize the code.

I am unable to write the code here as it exceeds the character limit so attaching the github url.




Please add your .ino file as an attachment if it is too big to include.

How did it get so big before you discovered the problem? It's much easier to provide help with short programs.

What Arduino are you using?

IMHO if you are using the String class then the number 1 priority is to remove all uses of it and see if that solves the problem. Just use cstrings - char arrays terminated with '\0' (NULL).

Two or three hours spent thinking and reading documentation solves most programming problems.


As mentioned, Strings are a problem on an Arduino with little RAM. Your device will be a lot more forgiving in that regard, but since you're using them all over the place, you may have overdone it. It looks like your firebase library uses them too.

Grab a Freemem function and see how much memory you're leaking.


Grab a Freemem function and see how much memory you're leaking.
Most likely unnecessary.  The statement "My code ... stops working after 5-6 Hours" is pretty much pathognomonic.  :smiley-roll:


i am using Node MCU. i am not sure when it became big as i really dont know regarding the node mcu capabilites what it can  handle or how much it can handle.

but i felt a bit weird that every example on youtube for IOT proj requests a DB for status every sec. which is insane and i dont want to use blynk as backend and being an iPhone dev i want to create my own App for this.

so i tried to find a library which works as a socket and shows response whenever there is change in DB. which solved my problem of crashing the node mcu.

if i get know what all things i need to take in mind while using node mcu in software and hardware wise.and libraries that track memory leaks ,debugging  through which i can stable it would be very helpfull

Thanks for the reply.


Not a cause of your issue but your code contains (contained) a few like below

Code: [Select]
    if (contact == 0)//if ON then
      digitalWrite(pinNumber, HIGH);
      if (contact == 1) //check the status after 1 sec and if not set retry to set it HIGH

contact will never change so if the first if evaluates to true, the second if will always evaluate to false.

If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.


i am not sure when it became big
Didn't you notice the program getting bigger as you wrote it?

Presumably you were testing it regularly and presumably you have an earlier version that works properly. What was the last piece of code that you added to it and which caused it to stop working properly?

Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up