OutoSoft

Odd Software for Odd Jobs

Grotto Time - Arduino sketch

Here's the code for my little Grotto Time project.

I make no claim that it is perfect or written to excellent coding standards. It has accreted over the years as I've experimented and changed my mind.

It's built with three main components:

  • Freetronics EtherMega - needed due to the sketch size)
  • Freetronics ProtoShield Mega - sandwiched between the EtherMega & LCD, where all the sensors are connected
  • Freetronics LCD & Keypad Shield - so I can see the time

Additionally, I'm using a DHT22 sensor for temperature/humidity, BMP085 for pressure, an LDR to measure ambient light, a PIR to automatically control the LCD backlight and a DS1307RTC real-time clock. I might add a UV light sensor later - I need to figure out how to house it as it needs direct sunlight for the best reading.

The code works roughly like this (not necessarily in this order):

  1. Get an IP address
  2. Try to sync time via NTP and store this to the RTC
  3. If it can't sync via NTP, then read the time from the RTC - note, this assumes/requires that the time is synched via NTP the very first time it's powered up, maybe I can change this to get the time from the computer when the sketch is loaded?
  4. Check if max/min readings are stored in EEPROM - it currently keeps track of the max/min weekly, resetting every Sunday midnight, by storing in EEPROM I don't lose them if I restart the Arduino for some reason (updating the code, connecting serial monitor, etc.)
  5. Startup the sensors
  6. Store the startup time so we can track uptime

Now the main loop starts:

  1. Calculate the local Sydney time, including if DST is active
  2. If it's Sunday midnight, reset the max/min readings
  3. Cycle through each display of information every 5s
  4. Check the sensors every 2s
  5. Check the PIR - if there is movement then flick back to the time/date display and activate the LCD backlight for 5s
  6. Try to post the sensor data to a PHP script on my website every minute (customise the PHP_PAGE_LOCATION and SERVER_ADDRESS)
  7. Check buttons on the LCD shield - the select button takes us back to the time/date display, for example

And that's it.