Web Controlled Table Lamp using Arduino Yun and Yaler

Anyone reading the title will definitely wonder why a Table Lamp would ever need to be switched On or Off via the Internet. Well, until unless there is something so special with a Lamp being On or Off, there is no reason for a Lamp to be controlled via the Internet. The logic here is that if you can control a Lamp over the Internet, why not any other Electrical appliance? You can control your home cooling or heating, you can control home lighting when you are on vacation (to simulate that the house is still occupied), you can check whether you have kept an Iron switched On or not, etc. According to me, the biggest product Arduino has come out with is the Yun. It is like the controller meant for Internet of Things. Just add a relay to it and you can control any Electrical Appliance over the Internet.

Note: This project involves mains electricity and all precautions must be taken while dealing with it. The base of the Relay as well as the screws on the relay terminal carry AC voltage.

Requirements

1) A 220V Lamp

Lamp

2) Arduino Yun

ArduinoYunFront_2_450px

3) 1 Channel 5V Relay Module

5v relay

The Setup

The basis for this project is that Electricity will not be switched via the mains switch or the power button on the lamp but by a relay which we will put inline on the live wire. When we control the Relay via a web interface, the Lamp will switch On or Off.

Setup

As this project is experimental, I didn’t want to cut the existing wire of the Lamp, so I used another power cable to connect it to the relay. I removed the plug of the lamp and exposed the bare wires. The Neutral wires of the Mains cable and the Lamp wire are connected to each other directly. The Live cable of the mains wire goes to the Common terminal of the relay as depicted. Also, the live wire of the Lamp cable goes to the relay on terminal NO (normally Open). There are 3 pins for powering and controlling the relay. The “-” terminal goes to GND on Arduino. The “+” terminal goes to 5V on Arduino. The “S” terminal goes to Pin 12 on Arduino as we are using Pin 12 for controlling the relay.

Setting up Arduino Yun

In order for the Yun to have Internet connectivity, it has to be hooked to our Home Internet using either Wifi or Ethernet. I have connected the Yun to the Home Wifi network. After this, the Yun should have Internet connectivity. It is time now to upload the sketch to the Yun. When the sketch is uploaded, the web page present in the sketch www directory will also be moved to the Yun. The web page can be accessed as http://ip-address-of-Yun/sd/lamp. The sketch as well as the HTML page can be found at the end of the tutorial. Also, the explanation for it can be found on my blog here. This page will not be accessible over the Internet without much hard work. You will have to do port forwarding on your router, you will have to setup Dynamic DNS, etc. The better way to make the Yun web page accessible over the Internet is via Yaler service. You can access the procedure here. After this, the page should be accessible as http://gsiot-xxxx-xxxx.try.yaler.net/sd/lamp

Sketch and HTML Upload procedure

If you have an sd card in the Yun and an /arduino/www created in the root directory of the sd card, the Yun will automatically transfer the HTML you wrote from your sketch directory to /arduino/www/sketch folder. For example, if my sketch is in \Documents\Arduino\”sketch” folder, I will have to create a folder www in “sketch” directory and have my HTML as index.html. Now when I upload the sketch using wifi, the index.html will be placed in /arduino/www/”sketch”/index.html in the Yun and you can access this page by entering “http://ip address of yun/sd/sketch”. Following is an example video explaining the above procedure

Demo of Switching On and Off of Lamp over the Internet

Video Tutorial for accessing Arduino YUN over the Internet

HTML Code

web page for Lamp

<html>
<head>
<title>Arduino Yun Lamp Control</title>

window.onload=Pinstatus;
function Pinstatus(){
morestatus();
}
function morestatus(){
setTimeout(morestatus, 2000);
document.getElementById(“description”).innerHTML = “Processing Status”;
server = “/arduino/status/99”;
request = new XMLHttpRequest();
request.onreadystatechange = updateasyncstatus;
request.open(“GET”, server, true);
request.send(null);
}
function updateasyncstatus(){
if ((request.readyState == 4) && (request.status == 200))
{
result = request.responseText;
document.getElementById(“description”).innerHTML = result;
fullset = result.split(“#”);
document.getElementById(“description”).innerHTML = fullset;
for(i = 1; i
</head>
<font face=”Arial”>
<table name=”Table” border=”1″ cellpadding=”6″>
<tr> <th align=”center” colspan=”6″ >Lamp Control</th></tr>
<tr>
<td align=”center”>
Toggle Switch
<br>
<input type=”hidden” name=”pin” value=”12″ id=”pin12″ />
<input type=”hidden” name=”action” value=”0″ id=”action12″ />
<img src=”off.jpg” width=”50″ id=”image12″ onclick=”sendbutton(document.getElementById(‘pin12’).value,document.getElementById(‘action12’).value);”/>
</td>
</tr>
</table>
<br><br>
<br><br>
<p id=”description”> – </p>
</font>
</html>

Arduino Yun Sketch

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
int DigitalPin[] = { 12};
YunServer server;
void setup() {
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
digitalWrite(13, LOW);
Bridge.begin();
digitalWrite(13, HIGH);
server.listenOnLocalhost();
server.begin();
}
void loop() {
YunClient client = server.accept();
if (client) {
process(client);
client.stop();
}
delay(50);
}
void process(YunClient client) {
String command = client.readStringUntil(‘/’);
if (command == “digital”) {
digitalCommand(client);
}
if (command == “status”) {
statusCommand(client);
}
}
void digitalCommand(YunClient client) {
int pin, value;
pin = client.parseInt();
if (client.read() == ‘/’) {
value = client.parseInt();
digitalWrite(pin, value);
}
else {
value = digitalRead(pin);
}
client.print(F(“digital,”));
client.print(pin);
client.print(F(“,”));
client.println(value);
}
void statusCommand(YunClient client) {
int pin, value;
client.print(F(“status”));
for (int thisPin = 0; thisPin < 1; thisPin++) {
pin = DigitalPin[thisPin];
value = digitalRead(pin);
client.print(F(“#”));
client.print(pin);
client.print(F(“=”));
client.print(value);
}
client.println(“”);
}

Advertisements

32 thoughts on “Web Controlled Table Lamp using Arduino Yun and Yaler

      • ohh..so..lets say i save arduino sketch “ethernet”..so in the ethernet folder, i have to create “www” folder then save html code in that folder..am i right?

      • It is possible to save files on the Arduino YUN Flash but it is not recommended because this Flash has limited write cycles after which it will fail. So it is better to avoid it. Also, due to this I have never tried it.

  1. i want to make 3 pin output..so i have to modify a little bit coding in html..the image on, off i have make it into three..so i change this in the code

    singleset = PinPair.split(“=”);
    PN = singleset[0];
    Pinstatus = singleset[1];
    if (PN == 2)
    {
    ActNum = “action” + PN;
    ImgNum = “image” + PN;
    if (Pinstatus == 0)
    {
    ActNum
    PinAct = “1”;
    image = “off1.jpg”;
    }
    else
    {
    PinAct = “0”;
    image = “on1.jpg”;
    }
    document.getElementById(ActNum).value = PinAct;
    document.getElementById(ImgNum).src = image;
    }
    if (PN == 3)
    {
    ActNum = “action” + PN;
    ImgNum = “image” + PN;
    if (Pinstatus == 0)
    {
    ActNum
    PinAct = “1”;
    image = “off2.jpg”;
    }
    else
    {
    PinAct = “0”;
    image = “on2.jpg”;
    }
    document.getElementById(ActNum).value = PinAct;
    document.getElementById(ImgNum).src = image;
    }
    if (PN == 4)
    {
    ActNum = “action” + PN;
    ImgNum = “image” + PN;
    if (Pinstatus == 0)
    {
    ActNum
    PinAct = “1”;
    image = “off3.jpg”;
    }
    else
    {
    PinAct = “0”;
    image = “on3.jpg”;
    }
    document.getElementById(ActNum).value = PinAct;
    document.getElementById(ImgNum).src = image;
    }

  2. Hello i saw your project and it looks very interesting to me. What program do you use for html code and are there some particular libraries to add?
    thank you

  3. Pingback: Homemade Hardware Final – Nate Padgett

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s