Program crashes after ~36 hours and does not reconnect, WiFi issues suspected.

Hello everyone. First, I am an extremely novice coder, so I'll apologize in advance for the inevitably messy and disorganized nature of my code, and possible omissions of important information to diagnose the issue. Tried including everything I could think of.

I learned to code (as much as I know, anyway) solely to build a controller for my indoor garden with the intent of maintaining ideal VPD (basically, controlling temperature and humidity in relation to each other). The desired conditions vary between day and night, so the NodeMCU controller must connect to WiFi in order to check the time. The problem is, about a day and a half after I start running the code, it will experience some sort of error and stop working. This doesn't always happen, but it happens a lot of the time, and occasionally will happen in less than ~36 hours.

For whatever reason, when it errs, it runs only the swamp cooler and nothing else (probably something electrical which I don't understand, since I have the swamp cooler on the RX pin). Because the room is so humid and relatively hot, I haven't been comfortable leaving my computer in there the whole time with Serial Monitor on waiting for it to crash (plus I use my computer daily). When I attempt to check the board in Serial Monitor after it has erred, it has no output.

However, there is one other circumstance where it also runs only the swamp cooler and nothing else: when I restart the NodeMCU, and it is attempting to connect to WiFi and check the time. Before this, it only says "WiFi Disconnected..." repeatedly in the serial monitor.

My theory is: the NodeMCU fails to connect to WiFi during a scheduled two-hour restart (which I ironically included to circumvent possible memory-related freezes which could come with just running the code indefinitely). This leaves it looping the "WiFi Disconnected..." menu until its memory fills up and it freezes. I'll take this opportunity to stress my novice experience level, so I might not exactly be qualified to diagnose the problem. Because there's no specific error code, and such a specific issue, it's hard for me to look into on my own.

In short: trying to create a controller which will switch on/off 5 different relays and connect itself to WiFi. For some reason, possibly related to WiFi and possibly not, after roughly a day and a half to two days, it freezes up and has no output on the Serial Monitor.

Any insight? Code attached. I also tried including a check which would tell me if the WiFi is disconnected and shut the NodeMCU off if it's been disconnected while running for more than 15 minutes, but this portion

  • Doesn't really work - it seems to constantly say it's disconnected from WiFi, even though it must connect to get past the initial time-check.
  • Wouldn't work to reset it anyway, if the issue is the NodeMCU filling up with the "WiFi Disconnected..." message from void.setup

Integers don't hold floating point values

//Define Atmospheric Parameters
int Tdeadbar = 0.5; // Accepted fluctuation of temperature to prevent rapid switching of heating/cooling
int RHdeadbar = 1.5; // Accepted fluctuation of RH to prevent rapid switching of humidifying/dehumidfying
int Tedeadbar = 2.0; // Emergency temperature deadbar value (how high the temp can rise above Tday before being considered an emergency temp)

// --- Day
int Tday = 28.0 + 2.8; // Desired temperature during the day, plus 2.8 degrees for evaporative cooling compensation within leaf
int VPDday = 0.8; // Desired VPD during the day
int RHday = 78.8; // Desired RH during the day
//...

That is the same as (truncated integers)

//Define Atmospheric Parameters
int Tdeadbar = 0;
int RHdeadbar = 1;
int Tedeadbar = 2;

// --- Day
int Tday = 28.0 + 2;
int VPDday = 0;
int RHday = 78;

Didn't look any further at the code and probably not the problem associated with your WiFi but you have to understand the difference between integers and floating point numbers

That is helpful, thank you. It will prevent some rapid cycling of appliances I've noticed. But like you said, probably not the core issue.

Make sure your device is not getting kicked off of the wifi device it is connecting to. You might make some simple test code that just gets the time from the wifi source and nothing else and see if the program still hangs/crashes.

Sure, but my plants are already growing. In light of that, I'm less worried about perfecting the code, more worried about putting a bandaid on it to fix the problem. Isn't there something I could do to reset the NodeMCU if it hasn't had serial output in x amount of time, or some similar trick to reset the ESP when it freezes? I thought my "reset if it's been on for longer than two hours" part would work, but apparently it doesn't even get that far to run the code to reset it.

Could it possibly be a hardware problem with the NodeMCU? It's a very new controller (previous one died from a power surge) but... maybe? I don't recall experiencing this issue with the old controller.

EDIT: Or, another approach, maybe I could reset the NodeMCU once the temperature dramatically exceeds acceptable value? That way it would reset if it lost control, but I'm not sure how I'd guard against reset-loops under those conditions (it would be the same temperature when the controller restarted).