Close

Code updates and web page

A project log for ESP32 Greenhouse Monitor

Collect temp/humidity, soil moisture, sunlight data to be stored on local web server

jeff-taylorJeff Taylor 04/26/2018 at 17:060 Comments

I finally finished the code updates to allow storing the rules on the web server.  Unfortunately I had no luck working with char* types so I ended up writing it using strings, but at least it works.  I have added a new variable to the code to name each device.  This is used for storing the data in the database, but can also be used to define custom rules for each device.  If there is no custom rule file for that device name then it falls back to a common rule file.  Each time a data packet is sent to the server it also checks the timestamp on the current rule file, and if that timestamp has changed then it will reload the new file.

As an example of what the rules look like...

// Heaters
f>56 : R0=0
f<54 : R0=1
f>48 : R1=0
f<46 : R1=1

// Fan control
(R0=1) | (R1=1) : R2=0
(R0=0) & (R1=0) : R2=1

// Lights
(t<0600) | (t>2020) | (L1>90) : R3=0
(t>=0600) & (t<=2020) & (L1<60) : R3=1

Starting from the top, the first rule says that if the temperature in fahrenheit is greater than 56, turn off relay 0 (heater 1).  The second rule says that if the temp is less than 54, turn relay 0 on.  The fan controls turn the fan on if both heaters are off (my heaters also have fans in them) ensuring something is always running that keeps the air moving.  The rule conditions are fairly simple but they do have access to a number of variables including all of the sensors.  I would also like to add a math function for % which lets you check for incremental conditions.  For example (m%10) would let you turn a fan on and off every 10 ten minutes.  It would also be helpful to know the month, day, and day of the week.  I've read there is now a full time/date function available on the ESP32, so if I find that maybe I can pull out all of my own functions performing timekeeping and have access to full date lookups.  But all of that is just features in addition to what is already working.

I have added a new image to the gallery showing the web page.  To build this I'm using PHP and pChart.  I still need to work on the labeling for the hour of the day, but this graph will show results from the past three days.  You can see a gap in the data from yesterday where the ESP32 did something odd... it stopped reading ALL of the sensors (no results at all from the BME280 and zero values for sunlight and moisture) however it continued to send out sensor data every minute.  This resulted in f being zero which turned on both heaters, however since it was still reading the rules file I was able to add more conditions to turn off the heaters and turn on the fan, preventing my plants from being roasted until I could get back home.  Whew!

So obviously my watchdog code still isn't working quite right.  During yesterday's odd occurrence the OLED display locked up as well.  Since the watchdog timer is connected to updates to the display, I would have assumed the watchdog would cause the system to reset, but it didn't.  It is critical that I get the watchdog to do resets any time something fails, so I'll have to re-think my strategy.  Maybe I'll connect it to the BME and reset if it fails to read twice in a row...?

Someone I work with is planning on building a greenhouse this Summer and has been curious about the project.  Except he doesn't have access to all the servers like I do.  I think at some point I'll build an image for a raspberry pi that contains the web server, mySQL, and PHP.  You would still have to set the SSID and key for wifi access, and set a static IP address, but I would think most folks that could build this project could also handle that bit with some easy instructions.

Discussions