Building an Arduino powered environment monitor and iOS app

Finished product
My apartment uses baseboard heaters and anyone who’s paid for hydro can tell you, they’re pretty inefficient. I wanted to collect some information about the inside conditions of my apartment so that I could better understand when and how to turn the heaters on. Normally a simple thermostat would do, or even a Nest, but my equipment is so basic, there is no read out available.

So I built my own. Here’s what I did.

In high level terms, I have an Arduino UNO using a WiFi shield and custom PCB connected to a DHT22 temperature and humidity sensor and photocell. This information is sent to data.sparkfun.com and is displayed in snapshot, table and graph form on my iPhone.

Setting up the Arduino

The components with the Arduino are all fairly well known, but I had not made anything with them together. Here are the parts and links to where to get them

I put them together on a breadboard and build the code. I can provide code if interested, but essentially I assembled individual modules in order to upload the data every 5 minutes. As far as the program goes, it’s laid out like this

  1. Include all libraries — this uses DHT, Timer, Wifi, SPI WifiClient, WifiServer and medianFilter
  2. Define base variables — Wifi shield configuration, website, pins, etc
  3. Create objects — Timer, client, server, filters
  4. run setup() — set pin modes, connect to Wifi and set the timers
  5. loop() only updates the timer
  6. Every 30 seconds all sensors are read and added to a filter
  7. Every 5 minutes the filtered data is sent to data.sparkfun.com

Creating a circuit

schematicA breadboard is fun for prototyping, but it wouldn’t look so good on a shelf, so I took the opportunity to test out a new PCB manufacturing website by building my own circuit. The pinouts of all components is fairly easily available online, or better yet, in EAGLE itself. The board doesn’t have many pieces, just some 0.1″ header holes, but the tricky bit is making sure that there are no pin conflicts with the Wifi shield.

Moving to a PCB

environmental_board
EAGLE provides a nice way to import a schematic to a board, and since I started with an Adafruit shield piece, there was a nice outline ready for me. At that point it was a matter of making sure that all traces have clean paths and there are 5V and GND planes.

1-IMG_7920
4-IMG_7930
3-IMG_7924
I tried a PCB service previously called OSHPark.com but was unhappy with the cost and lead time required. A friend pointed me towards dirtypcbs.com and that turned out to be a great choice. The name is basically entirely what you get: printed circuit boards at affordable prices, with a very reasonable 1 week lead time. I was also impressed with the shipping from China, as it was no more than an additional week with DHL. That’s 2 weeks for 10 custom boards.

Displaying it all on a phone

The data is sent to data.sparkfun.com, which provides a nice free way to store key-value data (eg. temperature=25,humidity=38, etc) and a way to retrieve it. During the day I work for a mobile software company, so building something to retrieve the data was fairly straightforward. I ended up with a way to view different properties in the latest data, table and graph form.
IMG_1565
This is the home page where the latest data point is displayed front and centre, along with the time it was retrieved. The time is relative, so a timer runs every second to indicate how old it is, but clicking the label shows the actual timestamp. Below that is some related information that might be useful: the 24 hour high value, 24 hour low value, and the current outside temperature according to forecast.io.
IMG_1569
Additionally the menu button at the top left presents a slide out “hamburger menu” that can switch between the different properties collected.
IMG_1570
The middle tab shows the last hour of data in table format and as you scroll downward, will load the next block from Core Data. In order to keep the actual internet request simple, the app retrieves the timestamp of the most recent datapoint and only requests points from Sparkfun that are after that. All points are stored in Core Data and loaded on demand.
IMG_1567
Finally the right most tab shows a graph of whatever property is displayed at the time, with options for last 4 hours, last day or last 4 days. At first I had last hour but I realized the data I was collecting wasn’t really changing in that time frame so it was largely useless.

For the most part the graphs are consistent, with my heater keeping the temperature at about 19-20C through the day. The light graph is very interesting because it clearly shows when the light goes off and how sunlight filters through the curtains in the morning.

Next plans

There’s one thing I’d change about the PCB design, and that would be to replace the fixed photo cell resistor with a potentiometer to adjust the threshold. Right now I’m using a 16k ohm (I think, something about 10k) and that produces a nice range between full bright and darkness, but it would be good to adjust as required.

With the data collected, I’d like to now make use of it somehow. You might notice how the board has 4 pins showing “relay” on it, which is so that I can connect an AC relay and control my lights or humidifier. Ultimately I’d like to work in some geocoding to my app so that I never come home to a dark apartment and have my light be smarter so that it doesn’t turn off while I’m still at my desk.

Posted in Arduino, iPhone Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*