Web Controlled Power Strip using Arduino Yun and Yaler

With Data becoming a major Asset in the Internet generation, it has been stored at locations called Data Centers. Managing the equipments which hold the Data is simple as they are accessible over the Internet. If you are aware of how electronic peripherals work esp. related to computers, you will know that every once in a while they malfunction and need to be rebooted. The problem is that in most cases these devices don’t respond so rebooting them in a conventional way is not possible. The only way to restart them would be power cycling the device. This would not be much problem if you have a manned Data Center or you have remote hands and eyes support. The problem is when you have a malfunctioning equipment at a place where there is no one to look after it. At such times, an IP PDU (Internet Protocol Power Distribution Unit) can be very helpful. This is a Power Strip which you can control over the Internet to power cycle devices plugged into it. In this article, we will look into how to use an Arduino YUN to make an IP PDU aka IP Power strip aka IP Power extension. The reason I am making this is because with Arduino YUN “I have got the Power”.

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) Power Strip (preferably with buttons)

pl13085

 

2) Arduino Yun

ArduinoYunFront_2_450px

3) 4 Channel 5V Relay Module

4 Channel Relay

4) Junction Box

junction box

The Setup

ippdu

The base of the project is to insert our own switching system instead of utilizing the buttons of the power strip for switching. The Neutral and Earth will go intact to the power strip. We will have to break the live going to the strip and put our switching system in between. The Live will go to Common Terminal of the Relay. You can put the Live in any of the common terminal and take wires from that terminal to other 3 common terminals. There are 2 ways you can take live from the relay to the strip depending on the situation you want to use in. If you want to use the strip at a place where power to a device is critical eg. a Server then you must take Live from Normally Closed terminal. What this means is that even if anything happens to the Arduino or if the Arduino Powers Off, the NC (normally closed) will still supply power to the appliance connected. If you are using the strip for home appliances then you don’t want continuous power to the appliance if the Arduino fails. For this you will have to take Live from the Normally Opened (NO) terminal. This means that when you send a signal for the strip port to be ON, only then it will turn ON. In this project, I am connecting it to NO as I want to use it at home.

In order to control the relay, the relay board has to be provided with 5V from the Arduino 5V. The GND will be connected to Arduino GND. Pins 3, 4, 5 and 6 of YUN will be connected to CH1, CH2, CH3 and CH4 respectively on the relay board. Signals on these pins will decide the state of 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/ip_pdu. 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/ip_pdu

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 Power Strip over the Internet

Video Tutorial for accessing Arduino YUN over the Internet

Files

You can find the INO file, HTML file and buttons here

HTML Code

Screenshot_2015-04-21-13-38-30

<html>
<head>
<title>Arduino Yun I/O shield</title>

window.onload=statusPin;
function statusPin(){
morestatus();
}
function morestatus(){
setTimeout(morestatus, 2000);
document.getElementById(“description”).innerHTML = “— wait … —“;
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=”Title” border=”1″ style=”width: 256px;” cellpadding=”6″>
<tr> <th align=”center” colspan=”4″ >Arduino IP PDU</th></tr>
</table>
<table name=”Table” border=”1″ cellpadding=”6″>
<tr> <th align=”center” colspan=”4″ >Power Control</th></tr>
<tr>
<td align=”center”>
1
<br>
<input type=”hidden” name=”pin” value=”3″ id=”pin3″ />
<input type=”hidden” name=”action” value=”0″ id=”action3″ />
<img src=”off.jpg” width=”50″ id=”image3″ onclick=”sendrelay(document.getElementById(‘pin3’).value,document.getElementById(‘action3’).value);”/>
</td>
<td align=”center”>
2
<br>
<input type=”hidden” name=”pin” value=”4″ id=”pin4″ />
<input type=”hidden” name=”action” value=”0″ id=”action4″ />
<img src=”off.jpg” width=”50″ id=”image4″ onclick=”sendrelay(document.getElementById(‘pin4’).value,document.getElementById(‘action4’).value);”/>
</td>
<td align=”center”>
3
<br>
<input type=”hidden” name=”pin” value=”5″ id=”pin5″ />
<input type=”hidden” name=”action” value=”0″ id=”action5″ />
<img src=”off.jpg” width=”50″ id=”image5″ onclick=”sendrelay(document.getElementById(‘pin5’).value,
document.getElementById(‘action5’).value);”/>
</td>
<td align=”center”>
4
<br>
<input type=”hidden” name=”pin” value=”6″ id=”pin6″ />
<input type=”hidden” name=”action” value=”0″ id=”action6″ />
<img src=”off.jpg” width=”50″ id=”image6″ onclick=”sendrelay(document.getElementById(‘pin6’).value,
document.getElementById(‘action6’).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[] = { 3, 4, 5, 6};
YunServer server;
void setup() {
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,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 < 4; thisPin++) {
pin = DigitalPin[thisPin];
value = digitalRead(pin);
client.print(F(“#”));
client.print(pin);
client.print(F(“=”));
client.print(value);
}
client.println(“”);
}

Advertisements

15 thoughts on “Web Controlled Power Strip using Arduino Yun and Yaler

  1. Sir, if i use Yaler service to access arduino web server,is that mean i can skip the port forwarding and DNS configuration like in the previous project “Control Input Output of Arduino Yun with AJAX”. ?

  2. Magnificent web site. Lots of useful information here.
    I am sending it to several friends ans also sharing in delicious.
    And naturally, thank you for your effort!

  3. Hi there i followed the methods above and as i enter the website i get a prompt saying:

    Authentication Required:
    The server requires a username and password. The server says: arduino.

    Im not entirely sure which username and password it requires me to enter. Please help.

    • Generally an arduino Web page will ask for password if you have secured your sketch with a password in the rest api setting in the yun configuration page . If you have not changed default settings , it should be root and arduino. If you do not want passwords for sketch Web page, you can select “open” in the rest api access in yun configuration page.

      • Thank you great project. I only have a problem with the buttons. If i leave arduino for about an hour i am unable to press the buttons on the webpage. Is there any way of making the connection stable such that i do not lose the signal. I am connecting via ethernet.

  4. sir,
    am a new user to this field
    please tell me how to access arduino yun via internet by using YALER.
    if there is any different settings for yaler access via Ethernet or a WiFi or a 3G modem access

  5. I cant make a click in the web page and the image does not changes to green. It shows wait… below the image. can anyone tell me what could be the problem.

    • Make sure you use the files from the link rather than copy and paste the code from the web page. Also, if it still does not work then let me know the procedure you are following.

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