Power Barduino

Welcome to the 'future' in power bar 'technology' :wink:

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. :frowning:

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 :smiley:

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>

You should use one of these:

http://sproutboard.com/Default.aspx

Package it in a metal 1-2U case with the power sockets and everything set up, and sell them to people for datacenter usage (ok, you might need to get UL listing and such).

Seriously, a networked enabled powerstrip that was inexpensive would be very useful for a rack of servers in a datacenter (especially if you were running Windows boxen).

:slight_smile:

That is a very very interesting product... Though it wouldn't be 'mine' :wink:

I've already gotten my home made pcb process down to a science (10mil traces with 15mil clearance comes out perfectly almost all the time).. I've already built a breakout for the relays (4 at the moment) so i can breadboard the prototype. For a rack mounted unit, a custom board should be fairly simple to make.. integrating with an off the shelf power bar... heh, that's another story :slight_smile:

What is the main function of a "network connected power strip"? Is it intended to cold boot some servers or something similar?

Pretty much, yeah... You would log into the control panel, and be presented with a list of available power ports and you could turn them on or off at will.

I'm sure future versions would allow schedules to turn things on or off. The use would not be limited to just servers, but anything that is powered by mains.

The relays I'm currently using (pc4-1c005d02) can be controlled by 5vdc and can handle either 12A @ 125vac or 7A @ 240vac.

I just finished my first 'live' (as in mains) test and it worked flawlessly! Just a simple fan, 120v @ .8A, turning it on and off from the driveway, down the block (good wifi ;)) and with the integrated (on the breadboard) buttons... MUCH FUN!!!

The next step would be to improve the control panel (add authentication so strangers cant turn off my stuff, scheduled times, etc), but the very next step is to construct the first off breadboard prototype (meaning etching another board and building some sort of case for it and 8 (gunna get more relays!) power sockets plus the control circuitry.

I'll be building the schematic in eagle in a little bit, I'll be sure to post it.. though it is very very simple (diodes, relays, transistors, leds (for status), and the arduino).

Thanks for reading!

Here's the schematic... On the breadboard I also have 4 buttons wired on d6-d9, but the final board won't make use of them...

edit: I've updated the schematic, I saw that I had the diodes backwards, and I added support circuitry for the atmega.

I've gone ahead and layed out a preliminary board for it as well..

You can get the below 8 relay box already assembled for ~$60, which might be an easier route for a relay setup. Other boards available in kits. All use 5v control signals. Need a 12v power supply, which could also power the arduino.

http://www.electronics123.com/s.nl;jsessionid=0a010c641f43aea86d2b39dd4bee82b5f59059be3ff0.e3eSc3mSaxuPe34Pa38Ta38Oax10?it=A&id=1713




http://www.electronics123.com/s.nl/it.A/id.446/.f?sc=8&category=35
http://www.mcmelectronics.com/product/28-6348&CAWELAID=220326087
http://www.electronickits.com/kit/complete/elec/ck1601.htm

Nice stuff, however so far my total costs (not including the arduino) is < $20 Canadian... The final design will be a network connected setup, with no computer needed, strictly web-based (and perhaps switches).

Does the schematic seem okay to you? I have never messed with 120v before and don't want to blow anything up :slight_smile:

Does the schematic seem okay to you?

Concerning the board layout, you have one area I would change, from a safety standpoint (can't be too careful when you mix low voltage circuitry with household current!):

You have a "zig zag" arrangement running from D1 to D4, from the left "leg" (cathode) of each diode, which is routed really close to both the anode legs of D1-D3, as well as really close to the center terminal of the relay contacts.

If you can lengthen the board a little to move the relays out more (or shift the LED/transistor/diodes down some - there's enough room above the 328), or route those lines on the other side of the board in some manner - it would be a safer layout; you could also run those lines (just those lines) with right angles instead of 45 degree angles (45 degree angles are more for signal lines, not power control, which is what is being done here) - that would allow you to shift those traces down underneath the diodes, and in between the coil contacts and that third relay contact, to move it away from the higher voltage.

I hope this makes sense. If not, let me know (PM me), and I will alter your board layout drawing to show what I mean.

:slight_smile:

yeah, i saw that too and didn't really like it much... i got lazy after routing the power and atmega lines and relied on the auto router for the rest... it sometimes works okay, but most of the time it's a coin flip...

K... I ripped up everything but the regulator and the relay's and re-ran things with separation in mind (keeping 5v away from 120) and I think I did the best I could without making the board much bigger... the power is routed with 32mil traces and the relays are routed with 70mil traces due to higher current (12A relays).

Let me know how I did :slight_smile:

I also have another question. Assuming this is all in one self contained unit, and space is of the utmost concern (must fit inside a 1u rack case at the maximum), what would be the best way of tapping into the 120vac being fed to the power sockets? Is there an easy way to step that down in a (likely completely separate and isolated pcb) thin design?

I've heard it is best to avoid right angles in your traces.
Apparently they break quicker than non-right angle traces?

Looks interesting regardless :slight_smile:

Nice Project, will try it out surely when i need a network controllable powerbar :wink:

As for the standalone networking capability i recommend you the use of a standard arduino ethernet shield. Its really easy to use. Once set-up, you need only power and network acess for your powerbar. I have one running to send data to a server. Works flawlessly 24h.

Keep up!
Jan

I have the official ethernet shield (made by tinker.it) and it doesn't work reliably... I'm almost certain that it's a defective unit, as the wiznet chip gets very very hot, and will only successfully complete a connection once in every 15-20 attempts...

My goal will be direct network connection, and I am only using serproxy right now as a work around so I can get the code working. I will likely end up just building my own ethernet board to be integrated on the main control board... There won't be an arduino in the final project, only the atmega chip itself...

I have the official ethernet shield (made by tinker.it) and it doesn't work reliably... I'm almost certain that it's a defective unit, as the wiznet chip gets very very hot, and will only successfully complete a connection once in every 15-20 attempts...

If you are running the arduino from an external power source, check the voltage at the board 5v pin. I've got an chinese arduino and when a 12v external power supply is used, the board voltage is ~7.5v. My ethernet chip runs ~30 deg F above ambient when the shield is not in use. The ethernet programming may impact the chip temperature when the shield is in use.

Right now I'm powering it by usb... i havent tried external power yet, but I doubt that would make a difference.