Welcome to the 'future' in power bar 'technology' ![]()
I've been looking for a cheap network connected power strip for a while now. Most of the products out there are in the $600+ range. ![]()
The solution is to DIY!
I have the code "complete" (having issues with my ethernet shield, so I'm using serproxy at the moment), and the circuit is pretty much finished (as finished as a breadboard can be ;)).
I am using php on a windows box at the moment to send serial commands in the form '11' or '20' first byte being relay number, second byte being 1 or 0 for on/off. It will also query the arduino (by sending a single 's') for the current status of the relays which reports back with a simple binary string of the state of the relays (1011 -- on off on on). The page is very simple and such, but I can turn the relays on and off from my iphone in the driveway ![]()
I don't have a schematic at the moment, or any pics, but I'll post the code for your criticism below:
Arduino Sketch:
#include <Button.h>
#define relayCount 4
#define relay1 0
#define relay2 1
#define relay3 2
#define relay4 3
#define button1 9
#define button2 8
#define button3 7
#define button4 6
#define btn1 0
#define btn2 1
#define btn3 2
#define btn4 3
byte relays[relayCount] = {2,3,4,5};
byte buttonStates[relayCount] = {0,0,0,0};
Button buttons[relayCount] = {Button(button1, PULLUP), Button(button2, PULLUP), Button(button3, PULLUP), Button(button4, PULLUP)};
void setup() {
for (byte i=0;i<relayCount;i++) pinMode(relays[i], OUTPUT);
Serial.begin(9600);
}
void setRelayState(byte relay, byte state) {digitalWrite(relay, state);}
byte getRelayStates() {return (PORTD & 0b111100) >> 2;}
void loop() {
if (Serial.available() > 0) {
int relay, state = 0;
relay = Serial.read();
if (relay == 115) {
Serial.println(getRelayStates(),BIN);
Serial.flush();
return;
}
relay = relay - 48;
delay(20);
state = Serial.read() - 48;
Serial.flush();
if ((relay >= 1 && relay <= 4) && (state == 1 || state == 0)) {
buttonStates[relay-1] = state % 2;
setRelayState(relays[relay-1], state);
}
}
for (byte i=0;i<relayCount;i++) {
if (buttons[i].uniquePress()) {
buttonStates[i] = ++buttonStates[i] % 2;
setRelayState(relays[i], buttonStates[i]);
}
}
}
PHP Code:
<?php
$r1state = $r2state = $r3state = $r4state = 'off';
$r1 = $r2 = $r3 = $r4 = 1;
function setRelayState($relay, $state) {
if (($relay >= 0 && $relay <=4) && ($state == 1 || $state == 0)) {
$fp = fsockopen("tcp://localhost", 5334);
fwrite($fp, (int)$relay . (int)$state);
fclose($fp);
}
}
function getRelayStates() {
$fp = fsockopen("tcp://localhost", 5334);
fwrite($fp, 's');
$curState = bindec(trim(fgets($fp)));
fclose($fp);
return $curState;
}
if (isset($_GET['r']) && isset($_GET['s'])) {
$relay = (int)$_GET['r'];
$state = (int)$_GET['s'];
if (($relay >= 0 && $relay <=4) && ($state == 1 || $state == 0)) {
setRelayState($relay, $state);
}
}
$curState = getRelayStates();
$numRelays = strlen(decbin($curState));
for ($i=0;$i<$numRelays;$i++) {
$relay = "r".($numRelays-$i)."state";
$relaystate = 'r'.($numRelays-$i);
$relay = ($curState & (1 << $numRelays-$i-1)) ? 'on' : 'off';
$relaystate = ($curState & (1 << $numRelays-$i-1)) ? 0 : 1;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>BarDuino Control Panel</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<style type="text/css">
body {text-align:center;font-weight:bold;}
a {text-decoration:none;color:black;}
a:hover {text-decoration:underline;}
div#powerBar {margin:0 auto;width:inherit;}
div.port {border:1px solid black;padding:.25em;width:10em;height:5em;float:left;margin:.25em;}
table {border-collapse:collapse;}
.on {background: #0F6;}
.off {background: #F33;}
div.clear {margin:0 auto;width:44.5em;}
</style>
</head>
<body>
<a href="./">Refresh</a>
<hr/>
<div class="clear">
<div id="powerBar">
<a href="?r=1&s=<?php echo $r1;?>"><div class="port <?php echo $r1state;?>">Relay One</div></a>
<a href="?r=2&s=<?php echo $r2;?>"><div class="port <?php echo $r2state;?>">Relay Two</div></a>
<a href="?r=3&s=<?php echo $r3;?>"><div class="port <?php echo $r3state;?>">Relay Three</div></a>
<a href="?r=4&s=<?php echo $r4;?>"><div class="port <?php echo $r4state;?>">Relay Four</div></a>
</div>
</div>
</body>
</html>


