September 2019 Update

New! We're moving to the new forum, eventually it will replace this blog. Please join us there.

View the Most Recent Feature Sheet by clicking HERE!

Friday, August 16, 2019

Ever have one of those days?

Posted: 10:05pm

I've been struggling all day with a problem in the new code.

The ESP8266 has an array which holds the sensorId for any attached sensors. When it is time for the module to scan the sensors, it first fetches this list of sensorIds from the database.

Then it loops through the array, and for each sensor, it loads the configuration. 

Like this

SELECT sensorId from vw_active_sensors where moduleId = 3
Loop the results and add into array sensorIdList[x]

The array can hold 20, there is only one sensor....

Once it has loaded the list of id's, it then loads the config for each, then sends the SCAN payload to the 2560 so it will read that specific sensor and send back the result.

That process occurs repeatedly at a configurable interval, I've set it to one minute for my testing...


So the first time through, it loads the sensorId (#55)

Then it loops the array, and loads the configuration for sensor #55

The request is sent to the 2560 to scan the sensor, and it reports back with the results.

(I don't have it saving to the database yet)

That's it for the first cycle, but it will repeat in one minute, fired up by the Blynk Timer.

The second time through, and all subsequent times, it cannot load the list of sensorIds, no results are loaded, or a whole bunch of fields show up, but they are from the sensor Configuration query?


SELECT `sensorId` FROM `growbywire`.`vw_active_sensor_info` WHERE `moduleId`=3

During the read, I print out the field names and their values, and look at this


FieldName     : Field Value
---------------------------------
sensorTypeId : def
sensorName   :growbywire
moduleId       :vw_active_sensor_info


If I comment out the actual execute of the sql in the configuration load, then the list load works fine. 

It took me all day to get to the point where I could actually pinpoint where it was happening, repeatably...

All I can think is some sort of memory issue, since it still had all the field names from the previous query, but I've been using this library since day one, and never have seen anything remotely like this.

I even thought maybe it was because I was calling this from the blynk timer event handler, but I just called it from loop() every 30 seconds, same problem...

At this point, I'm happy to know where the problem is, and that I can get around it. All I need to do is close the database connection and reconnecting before I try loading the list, and it's fine. Closing the database releases the buffers according to the author, really adding weight to my theory of a memory problem.

I wonder if this has happened in other places, but I'm just not aware of it? It was so repeatable, I can't see how I'd miss it.


Update: 3:05am

I ended up closing the database after every time I access it.  I did see this occur when it was trying to fetch the current date/time from the database using a SELECT NOW();  I wonder if this has been an ongoing thing, or did I do something to suddenly cause this?  Closing the database after each use has solved it, I let it run while I had a nap :)

I initially decided to leave the database open all the time, since I was accessing it so much. The decision was mainly for speed.

No comments:

Post a Comment

Any comments deemed off topic or offensive will be removed