They say “necessity is the mother of all invention” and this post proves that point for me. For the longest time, the power switch on my lamp at home has been broken. To turn it on, I would have to plug in the power cord. Sure, I could have just replaced the lamp or re-wired the socket to turn on with a light switch, but what fun would that be?

Instead, using an older Arduino Uno, WIFI shield and a single channel relay, I can now turn the lamp on and off using a web browser or my iPhone. This is the same principal behind Nest – the popular home thermostat that is web based (after all, a thermostat is just a bunch of solid-state relays).

My new WIFI light switch connects to my home network and creates it’s own IP address that can be accessed only from the same network. Once you upload your code, you can debug using the serial monitor to ensure it’s all working properly. It will also show you the IP address it created. Point your browser to that IP and you’re in! Then you just need a 5V power supply and you can plug this in anywhere – on reset, it will just run the code you previously uploaded. It also has a keep-alive loop so it remains connected.

I used a scavenged Sainsmart 5V single channel relay module I had left over. To connect the lamp to the relay, I cut one line of the power cord and just put the relay in between. One of the simplest and easiest to understand tutorials on relays can be found here. I love it when people make things simple to understand!

There is a really great new product on the market that would make this project pretty cheap. I ordered a couple Spark Core from Spark.io which is an Arduino clone with WIFI built in. This avoids the cost of an Arduino Yun or WIFI shield to really automate it.

I am thrilled. Imagine the possibilities – I can now automate anything in my house using a local IP based menu. Add voice recognition to this project, and you can literally talk to your appliances!

#include <SPI.h> #include <WiFi.h> char ssid[] = "XXXXXXXX"; // your network SSID (name) char pass[] = "********"; // your network password int keyIndex = 0; // your network key Index number (needed only for WEP) int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { Serial.begin(9600); // initialize serial communication pinMode(9, OUTPUT); // set the relay signal pin // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { Serial.println("WiFi shield not present"); while(true); // don't continue } String fv = WiFi.firmwareVersion(); if( fv != "1.1.0" ) Serial.println("Please upgrade the firmware"); // attempt to connect to Wifi network: while ( status != WL_CONNECTED) { Serial.print("Connecting to "); Serial.println(ssid); // print the network name (SSID); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); } server.begin(); // start the web server on port 80 printWifiStatus(); // you're connected now, so print out the status } void loop() { WiFiClient client = server.available(); // listen for incoming clients if (client) { // if you get a client, Serial.println("new client"); // print a message out the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) { // loop while the client's connected if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor if (c == '

') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); // the content of the HTTP response follows the header: client.print("<font size=\"24\" face=\"arial\">Home Automation v0.1 alpha - System Online<br><br>"); client.print("Desk Lamp 1: <a href=\"/H\">ON</a> | <a href=\"/L\">OFF</a></font>"); // The HTTP response ends with another blank line: client.println(); // break out of the while loop: break; } else { // if you got a newline, then clear currentLine: currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } // Check to see if the client request was "GET /H" or "GET /L": if (currentLine.endsWith("GET /H")) { digitalWrite(9, HIGH); // GET /H turns the LED on } if (currentLine.endsWith("GET /L")) { digitalWrite(9, LOW); // GET /L turns the LED off } } } // close the connection: client.stop(); Serial.println("Action executed successfully."); } } void printWifiStatus() { // print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID()); // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip); // print the received signal strength: long rssi = WiFi.RSSI(); Serial.print("signal strength (RSSI):"); Serial.print(rssi); Serial.println(" dBm"); // print where to go in a browser: Serial.print("To see this page in action, open a browser to http://"); Serial.println(ip); }