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
2) Arduino Yun
3) 1 Channel 5V Relay Module
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.
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
<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(“”);
}
hye…can i know where u put the html code?
hye can i know where you put the html code?
You have to put the HTML code in the www folder in the “sketch” folder. You will have to create the www folder.
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?
Yes. And if you have images on page also, those will also go in www folder.
hi itpossible doing this without SDcard?
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.
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;
}
You can refer to my article on input output with AJAX. It should give you good details.
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
I use wordpad as text editor but I wish I had a proper HTML text editor. There are 3 libraries used in the sketch which are enough for this setup.
These three libraries where i can find them?
Thank you
These are part of the arduino ide so you don’t have to install it. If you are using the latest ide and can see the yun,then the library should be present by default.
Hi. So basically what are the steps I need to do. kinda confuse as you didnt state the steps.
Our aim is to control the live portion of the wire to control lamp for that we insert the relay switch on the live wire. In order to control relay, we need a microcontroller, for that we use YUN. First you need to do the wiring according to the diagram of the post and then you can program the YUN to control the relay.
okay, so the diagram will lead us to the next step which is the HTML code? Am i right?
Yes
my index.html file is getting transferred on the sd card but when I open the link http://ip_addr_of_yun/sd/project_name, it shows no such file or directory
Can you mention the actual url you are using to access?
http://192.168.1.5/sd/PROJECT
Check whether in the /mnt/sda1/arduino/www/PROJECT directory there is index.html
yes index.html exists in PROJECT folder. Do I need a formatted sd card?
You have to upgrade the firmware on yun when you get it, it comes with linino by default which doesn’t support html Bridge.
ok. so after updating what linux distro i get?
and opkg update is the command?
Openwrt.there is a guide on arduino site for upgrade procedure
Thanks. I have updated my yun. Now I have a web page with toggle buttons but how do i send the data to arduino. I don’t understand that. Can you please help
What exactly are you looking for?
I just want to do the same as the tutorial above but with my web page, which have toggle buttons to on/off the bulb.
I tried your code but didn’t get the output
Try with an independent code whether the relay is working or not OR attach an led to the circuit and modify code that whenever relay is triggered, the led also should glow. You have to troubleshoot step by step.
I tried the on board led(13) from your code but nothing happened. And independently everything is correct.
Can you tell me how to send the commands to arduino sketch from the web page?
Basically the buttons of the web page has a shortcut to this url “http://ipaddress/arduino/digital/13/1”. so if you just put this url in the browser with your ip address, it should switch on the led 13. What the arduino code does is parse the url. If you study the arduino code, you will come to know that any thing after “arduino”, we can use to send specific commands like analog or digital pin and the pin number.
Pingback: Homemade Hardware Final – Nate Padgett
Hello, this is a perfect example for a beginner ! But I found there is somthing wrong or missing in the HTML-Code above ( I took it with copy and paste). Can You please check it or send me a link with the correct code ? The AJAX-Project works perfectly.
Thank you very much
https://www.dropbox.com/sh/u02db6vk6ywr65b/AAA83KWxA1WODkENA0qZ8kXCa?dl=0
This should have all the files
Hello, with this HTMLfile it now works as expected. There is a lot of text missing in the HTML code in the description of the project.
Thank you very much for your very fast reply !
Wolfgang
I know it’s an old blog and that the Yun is no longer supported but, long time ago I was able to password protect with a .htpasswd file the index.html page. Do you know how? TIA
I don’t know this in particular but if you want to password protect your sketch Web page then that can be done via Yun configuration Panel. Here you can select rest api access with password