Arduino Forum

Using Arduino => Project Guidance => Topic started by: Aniketbhondave on Aug 02, 2020, 10:46 am

Title: Need Guidance Regarding My code. as it stops working after 5-6 Hours
Post by: Aniketbhondave on Aug 02, 2020, 10:46 am
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.

https://github.com/aniketbhondave/Firebase-Switch-IOT/blob/master/Firebase-IOT-Switch.ino (https://github.com/aniketbhondave/Firebase-Switch-IOT/blob/master/Firebase-IOT-Switch.ino)


Thankyou

Title: Re: Need Guidance Regarding My code. as it stops working after 5-6 Hours
Post by: Robin2 on Aug 02, 2020, 10:57 am
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 (http://www.cplusplus.com/reference/cstring/) - char arrays terminated with '\0' (NULL).


...R
Title: Re: Need Guidance Regarding My code. as it stops working after 5-6 Hours
Post by: wildbill on Aug 02, 2020, 02:59 pm
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.
Title: code stops working after 5-6 Hours
Post by: Paul__B on Aug 03, 2020, 03:55 am
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:
Title: Re: Need Guidance Regarding My code. as it stops working after 5-6 Hours
Post by: Aniketbhondave on Aug 03, 2020, 09:20 am
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.
Title: Re: Need Guidance Regarding My code. as it stops working after 5-6 Hours
Post by: sterretje on Aug 03, 2020, 09:50 am
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);
      //changeTempPinState(pinNumber,"HIGH");
      delay(2000);
      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.

Title: Re: Need Guidance Regarding My code. as it stops working after 5-6 Hours
Post by: Robin2 on Aug 03, 2020, 10:00 am
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?

...R