Christmas Tree Lights Controlled by Phone? a Simple Internet of Things Hack.

by brianfit in Living > Christmas

2595 Views, 21 Favorites, 0 Comments

Christmas Tree Lights Controlled by Phone? a Simple Internet of Things Hack.

IMG_8358.jpg
41hGIcvJKAL._SX300_.jpg
Screen Shot 2014-12-22 at 23.35.18.jpg
Turning Xmas Tree Lights off and on with a phone

My son wanted to be able to turn the Christmas Tree on and off with his phone. We dug around in the Secret Mountain Laboratory (AKA the basement) and came up with the following project, which uses an Arduino to smarten up a dumb infrared remote-controlled power socket and make it controllable via a web interface within our home network. While this example uses a set of Christmas tree lights, this is a super-safe way for kids to learn about using the web to control any appliance: by interfacing with a remote control we don't need to mess around with mains electricity.

If you can use a soldering iron, copy and paste code, and you've got an internet-capable Arduino, with a couple relays you can turn a set of cheap remote-control power sockets into nodes on the Internet of Things. We built this entirely with things that were lying around, except for the relays, which cost 4 bucks.

What we used:

  • Arduino Uno & 9v Power source
  • Adafruit CC3000 Wifi Shield
  • SainSmart 5v 2-Channel Double Relay
  • Cheap Remote-Control Power Sockets (If the remote has an LED to confirm button-press, that can be a huge help when testing)

This project uses the Arduino aREST library written by Marco Schwartz, an awesome bit of code that allows the Arduino digital pins to be directly addressable via web calls, e.g. http://192.168.0.15/digital/7/0 [Sets Digital Pin 7 LOW] and http://192.168.0.15/digital/7/1 [Sets Digital Pin 7 HIGH].

Bear in mind that this setup only works within a home network on devices using the same WiFi or ethernet: your arduino, your webserver hosting the page, and the phone or other client device that visits the page all have to be in the same digital neighborhood and able to visit local IP addresses.

The final ingredient is a locally-hosted web page with big ugly ON OFF buttons designed by my 10 year old (User Experience and web design can wait, this is SCIENCE!). By calling up the webpage on his phone, he can turn the Christmas Tree lights on with a wave of his iPhone and a shout of "LUMOS!"

Hack Open the Power Socket's Remote Control

IMG_8354.jpg
IMG_8356.jpg
IMG_8357.jpg

We used a cheap remote control mains socket kit we had lying around. Ours had four sockets and one Remote Control, with each socket having a dedicated On and Off button on the remote. We only used one of the sockets, but a more ambitious project could use the same method here and more relays to create multiple smart nodes. The look of your remote may be different, but the principle of this is pretty straightforward.

Look hard at the remote control for your power socket. There's a way into it. Might be a screw, might be a matter of prying it apart with a butterknife. Lift the faceplate off, and remove any fastening screws so you can get at the back of the circuit board.

For each button you want to programme, you want to identify the two solder points -- mine were on the backside of the board -- that the button connects together when pressed. If you have an LED on the Remote that makes things easier: you'll see it go on when you put a jumper wire between the right points. (If there's no LED, you can just plug a light into the socket you're trying to control and use that to test.) Solder a loose wire to each of those points. When you complete the circuit by touching these wires together, the remote LED should go on and the power socket respond: you're basically replacing a mechanical button press with a circuit completion.

Wire Up the Relay and the Arduino

Relay Circuit.png
Screen Shot 2014-12-22 at 19.44.08.jpg
IMG_8357.jpg
IMG_8358.jpg

We used an Arduino Uno and an Adafruit CC3000 WiFi shield, but this project will work with any internet-connected Arduino, be it the YUN or another Arduino flavor with an Ethernet shield.

In the case of the Adafruit shield, you need to be careful not to use any of the pins that it uses for dedicated board functions. I used pins 9 and 10 in my first attempt at this project, and couldn't figure out why the 10 pin seemed to be randomly going high. That would be because I didn't Read The Fine Manual that came with the CC300 Shield:

On the CC3000 shield, we use the following pin connections
SCK - #13 MISO #12 MOSI #11 CS for CC3000 #10 VBAT_EN #5 CS for SD Card #4 IRQ #3

Don't use any of those for assignable values.

Digital pins 7 and 6 are safe.

Wire the loose leads from your Remote to the Common and Normally Open (NO) connectors of the relays. This means that in the rest state, the circuit between each wire is incomplete: you shouldn't see that LED light going on when you connect to the relay. If you do, you've probably connected to the common and Normally Closed (NC) connectors. Wrong way, go back! In my example, the OFF button is wired into Relay1, the ON button into Relay2.

From the Arduino/Adafruit sandwich, you want to run the following connections to the relay:

GND-GND
+5v-VCC
Pin6-In1
Pin7-In2

Code the Arduino

The Arduino file attached is a slight modification of the example script from Marco Schwartz's REST API Library.

As he explains:

I have been using the CC3000 WiFi chip for a while, and the problem that I encountered while developing web applications using the CC3000 chip is that I had to create a new Arduino sketch for every application, that needs to be coordinated with the rest of the application, for example an interface running on my computer. For example, using this REST API, switching the state from a pin on the Arduino board can be done directly in the browser by typing the following URL:

http://arduino.local/digital/8/1

With this REST API, it’s easy to load a sketch once for all on your Arduino, and then only work on the interface on your computer that makes REST calls on your Arduino board. And for now, this kind of interface was only available on the official Arduino boards, like the WiFi & Ethernet shields, and the Yun.

So he wrote a REST API for the CC3000 chip.

Use the script I uploaded here if you have an Adafruit or Sparkfun shield or breakout board based on the CC3000 chip. If you have a different WiFi or Ethernet card, use the REST libraries written for your own board. The only special elements that you need to replicate in your own script are the following:

Set Digital Pins 6 and 7 for output, and set them HIGH so we don't kill the remote's battery by putting it into a perpetual ON state

int OnSwitch = 7;
int OffSwitch = 6;
pinMode(OnSwitch, OUTPUT);
pinMode(OffSwitch, OUTPUT);
//The Songle/Sain Relay we use is off when the pin is set HIGH.
// These pins should remain high as default state to conserve battery 
//on the remote control.
digitalWrite(OnSwitch, HIGH);
digitalWrite(OffSwitch, HIGH);

Make sure you query the device's IP address as you'll need that information for the Curl script.

Load the IOT-Tree.ino script onto the Arduino, then open the Serial Monitor window. The IP address will be reported there. (To open the Serial Monitor Window click the little magnifying glass in the upper right of the IDE window, as below)

Downloads

Build Your Web Page

Before you build your web pages, test your setup by pointing a browser to your Arduino with the following URL:

http:[IP ADDRESS OF YOUR ARDUINO]/digital/7/0 (ON button)
(Turn the remote button off quickly with the same URL but with a 1 instead of a zero. The mains socket stays on ON, this just turns off the ON signal from the remote. If you want to turn the socket OFF, use the URL

http:[IP ADDRESS OF YOUR ARDUINO]/digital/6/0 and then don't forget to turn that off quickly, to preserve battery on the remote, but issuing the same URL with a 1 instead of a zero again)

If all is well, time to build your web page.

The files above need to live in the root directory of your webserver. (If you don't already have a computer on your network set up to serve webpages i.e. function as a webserver, here's handy instructions on how to set one up on Linux, Mac, or Windows). You'll need php to be installed as well -- it comes bundled with XAMPP for windows, and it's native on a Mac but you need to turn it on. If you're on a Linux machine, heck, you're geekier than I. You don't need me to tell you what to do.)

Install the files at root. On the phone you want to use as a controller, go the address

http://[IP-Address-of-your-web-server]/tree-lights.html

Press the big ON button. Shout "LUMOS," and behold! Your Christmas tree is now a part of the Internet of Things!

(The files in the archive below are also on Github.)

Downloads