Introduction: MyHome - Home Automation With Arduino and XBee
**** UPDATED ******
- added Nexa power outlet control
- added one wire temperature sensors
- added control of stuff via Google Calendar Events
This is the story about my project "myHome". I wanted to build a system for monitoring temperatues, electricity and oil usage and maybe in the future control an AC unit or why not the locks, drapes and my Moccamaster...
Parts in myHome:
Wireless sensors units:
- XBee sensor units for temperatures.
- Arduino + XBee unit for electricity monitoring from the blinking LED on the houses main circuit board
- Arduino + XBee unit for monitoring oil burner from a LED which lights up when burner running
Server running Windows 7:
- XBee reciever connected with a FTDI USD cable
- Python script to recieve XBee signals and insert sensor values into mySQL database
- mySQL database on server
- Python script to get current and forecasted outside temperature from nearby airport and write this to database
- Apache webserver for showing charts, and in the future web based functionality to do stuff in the home
Step 1: Temperature Sensors
These are XBee series 1 radios sittin on top of Adafruit Adapters. I chose their adapter as I wanted to solder it together myself and their guides are really good.
For the temperature sensor I chose the cheap TMP36 sensors. Their voltage differs in relation to temperature. This voltage is connected to the analog ports on the XBee.
The XBee radios are configured to sleep the maximum time (about 5 min) and then grap just 1 set of analog voltages and send it to a coordinator XBee using API mode. The config file is attached. You can open it in a text editor to see the settings.
At first I used 4 AA batteries to power the radios but as they couldn´t last more than about a week at the time I switched to old Nokia chargers.
I used 3.5mm plugs for sensors and the power.
This unit is in the bedroom which is the room that is most relevant for this monitoring. The room is either heated with a heating element which burns oil (baaaaad) or with a fireplace (goood).
Any suggestions on how to cover the fireplace sensor and cord so it doesnt look that ugly??? It´s not an option to move the sensor as I want to monitor that exact spot (it the hottest).
Step 2: Electricity Usage Sensor Unit
On most household electricity meters there is a LED that pulses every time a certain amount of Watts have been used. On mine one pulse means 0.8Wh used. This was printed next to the pulsing LED.
Of course you could remove the LEDand direclty grab the pulse from there but as I have a great respect for that box I didn´t dare to dismantle it. Instead I decided on placing a light sensor on top of the LED and get the pulses this way.
Without properly studing light sensors I first ordered some "photo cells" but quickly realized they cant pick up a quick pulse. Instead I got some Photo Reflective Sensors that have a light and IR sensor. With this sensor there was no problem to catch the pulses. Of course I had to cover the area around no to let any additional light in.
How to count the pulses? First I wanted to do this just with the XBee but as I couldn´t figure out a way to easily do it I put a Arduino to provide some logic. The sensor is attached to an analog port and the Arduino does a count++ every time it senses a pulse. A counter on the Arduino counts to 60 seconds and then sends the count over XBee to a coordinator. The Arduino is actually a clone by Seeeduino.
Arduino sketch and XBee config attached.
Step 3: Oil Burner Runtime Sensor Unit
This is pretty much the same thing as the electricity monitor except for a few differences. The burner has a LED that lights up while the burner is running. So now we are not counting LED pulses but seconds over period of time when LED is lit. I choose 5 minutes.
On the Arduino I have a LED that is normally green and when the burner is running it switches to red.
Arduino code and XBee config attached.
At this time I dont know how much oil the burner burns per second. This depends on the nozzle inside. There should be a print on the nozzle indicating gallons per minute (I think) but I havent gotten around to check mine yet. Another way to get this info is of course to instead take the oil from a bottle or such and compare how much is spent over the time used. I´ll probably do this before next winter...
Step 4: XBee Reciever Unit (coordinator)
This is the unit connected to the computer. Assembled using Adafruits guide. Some pics above from the assembly. Nothing special...
As I love Legos (who doesn´t) I built a simple case for it.
See the next steps for info on the stuff going on in the server.
Step 5: Wireless XBee LCD to Show Key Values and Indicate Status With Background Color
This will be a small unit with all kinds of uses in the future. So far it shows key values of whats going on in the house.
Up left: El. use over last 1 minute in Wh / average el. usage this hour and this weekday from whole history of values.
Low left: oil burner runtime in minutes last 5 min period / same as above
Up right: time
Low right: current temp. If the heating element is over 1 degree warmer than the fireplace outer wall both are shown. Means it´s time to burn some wood!
The background color changes according to how much electricity is used.
Green for less than 80% of the average.
Blue for 80-120% of average. In the picture which looks green the screen is actually blue put for some reason it shows greenish in the pic.
Red for over 120% of the average. Turn off the vaccum cleaner!!!
The LCD is from Adafruit. Its has a USB + Serial backpack so its really simple to use...
XBee config attached
Step 6: Python Script for Reciever
The script does the following:
- gathers values from temp, electricity and oil unit and writes it to the mySQL database
- sends data to the LCD
The code started simple but over the time as new functions were added it became quite difficult to read. It should be refactored but as it works well I havent gotten around to it yet...
gatherValues.py is used for testing.
SER_gatherValues.py is used to install the script as a Windows service (command: python SER_gatherValues.py install)
- Clean the code!
- Sending to the LCD is now done every time we recieve something from another XBee. Reconfig the LCD XBee to sleep and on wakeup poll for new values.
Step 7: Python Script to Get Outside Temperatures From Airport
This script gets temperature values from a nearby airport using the wunderground service.
Simple stuff. Wunderground has good examples on their site...
Step 8: MyHome Website With Charts
This part covers the website that presents some of the gathered data in graphical form.
I use the flot package to draw the graphs and php to query data from the mySQL database.
I am very aware that the code does not follow any best practice guidelines... Should absolutely clean it and optimize. The page takes a while to load...
Here you can see some screenshots of what kind of things can be seen on the myHome website. Dont ask me to provide the URL to the actual page because the server is in no way capable of answering multiple requests... :-)
There is one getXXXTemps.js.php or similar per line but they are basically all the same. Only the SQL query differs.
We have a be nice policy.
Please be positive and constructive.