September 2019 Update

New! We're moving to the new forum!

ALL new Blog posts will be on the forum.

Please join us there.

This Blog will no longer be updated...

Monday, August 12, 2019

We're gonna have fun tonight!

Probably the biggest weakness of the Sensor Module design is the amount of data being sent back and forth between the 8266 WiFi module and the 2560 MCU.  While it is a hardware serial line and both systems share the same motherboard, overlapping data still occurs, and this messes up the XML structure.

Currently the 2560 orchestrates everything, but that means it has to maintain a lot of configuration information, and keep it up to date. Every time it scans the sensors, it reloads the configuration for all of them, it needs to reflect any possible changes that might be made to the configuration...

So it wakes up and requests the config for all sensors, then loops through the list, and tests, and then reads each sensor, does any data manipulation (convert to % or degrees, etc), then sends a bunch of data back to the 8266, which then does a few lookups, then writes the sensor reading to the database.

Once it has finished all the sensors, the 8266 sends a total of how many records it received, skipped, and wrote to the database.


I've been toying with an idea in my head, why not let the 8266 be the brains...  When it wants a sensor reading, it sends a simple request to the 2560 over the serial port, and gets a simple response.... We don't need to send all that configuration information back and forth. Using this method, we don't need acknowledgements of data being received, since the 8266 will either get a response, or not... that's acknowledgement enough eh?

Obviously this is a HUGE HUGE HUGE change, so I'm just going to start a completely new project and have at it...

I've got my music playlist updated with some rocking tunes, a couple of Monster Energy Drinks in the fridge, and half a mason jar of nicely dried buds...

Let the fun begin! 


Update: 9:45pm

Whew!  I finally have the esp8266 connecting to wifi , loading data from the database, and taking OTA updates!  I thought I was going to wear out the cheap little dip switch while I was having to change it back and forth from upload to run... 

Overall things are progressing about as I expected, slowly :)

Update: 4:30am


I uploaded the code, and the esp8266 went crazy, rebooting constantly... Extremely frustrating, I was right back to when I was a rookie with these... and I was lost...


I have what I call blank sketches for all my boards, so in a case like this, I upload the blank sketch, and in the case of the 8266, it has WiFi code, and the OTA code as well, but that's it... It was fine, it connected to WiFi, and did not reboot...



I then installed my previous version of the Sensor Module code for the 8266, and it was rebooting too! WTH, this was working code???



Ok, back to the new version...
In the code that connects to the WiFi, it does a loop while checking to see if it is connected, and in this loop, there is a delay(1000). So, I decided to change it to a while millis() < startTime + 1000; instead. I'm not really sure where I picked this up, but it allows for finer control over delays, I can actually do something in the loop if I need to while waiting...



Now, when I was first playing with these ESP8266 modules, I had this problem a lot, and while it eventually went away, I never knew exactly why it had occurred...



Now I know... The ESP8266 has a built in Watchdog Timer, I knew this... and I knew this was what was causing the reboots... I just didn't realize HOW I had inadvertently fixed it...



The watchdog timer is there so that if your code gets into an infinite loop, the watchdog will reboot the module after, I think it's 2 seconds or something. My loop waiting for the WiFi connection looked like an infinite loop to the watchdog timer, so it kept rebooting. So the code needs a way to notify the watchdog timer that it's ok, we're still doing what we were designed for... and it is done in the Delay(x) method. Apparently a few things happen inside the delay method, but this is the one of interest now. 



I discovered this as being the culprit when I first removed the loop altogether, and it worked fine... When I added the loop back, it rebooted.



I kept the new code for the delay, but I just added a delay(0); inside the loop. This is a delay for 0 milliseconds, so it really won't add any delay, but it will do it's internal work, including telling the watchdog timer to buzz off!



So while I could have simply cut and paste the old code in, I would not have actually learned the true cause.



Oh, why did the original Sensor Module code not work any more? It turns out that when I removed the delay and replaced it with the while loop, I decided to make that same change in the original code :( You know, pot and short term memory and all...




So the moral of this story is: 
Always include a delay(x); in loops on the esp8266
even if x = 0

Now that this is solved, I can continue...   More later....


After more digging...

From: esp8266/Arduino

Within the delay method, it calls the esp_yield();method, which is where the timer gets reset. You could call esp_yield(); directly I suppose.


No comments:

Post a Comment

Any comments deemed off topic or offensive will be removed