Posts tagged: electricity

Ooh Arr-duino

Santa this year was very generous to my son and gifted him with a Maverick Atom XT RTR 1/18 Electric 4WD Truggy but failed to mention that the batteries that came with it could only be left in the charger for 6 hours, no longer. With grave warnings about explosions and fire, I tried to find an egg-timer on a socket kind of solution, but could not find anything that would go beyond 4 hours.

So what is a tinkerer to do? He makes one himself! ;)




A long time ago I bought an Arduino, but never really got any further with it than a blinking RGB led. Now I finally found a purpose for it :) I was going to build an Arduino controlled timer socket!

All I needed now were some kind of relay, some sockets, a few buttons and a way of letting the user my son know what the time was set to and if it was on or not. I ended up with a Ciseco Arduino Relay Shield, two Maplin push-to-make switches, a DFRobot I2C 16×2 LCD and a Maplin project box big enough to fit it all in

First a small word of warning. You are dealing with MAINS voltage, which can be lethal if you fool around. DO NOT EVER! connect the bits to mains power when parts of it are exposed. You’ve been warned

I started off with a small mockup of materials and then got my trusty Dremel clone out and started cutting away at the project case.

First off is the socket in which the battery charger can be plugged. A small template on paper is by far the easiest way to make sure you don’t cut out too much behind the socket

With the socket in place, I created the template for the LCD. I took my time with this, as I didn’t want to cut out too much. A lot of careful sanding ensured that only the black part would be visible, and nothing else. Then with the help of some small bolts and nuts, I created some spacers, enabling the LCD to sit nicely in the cutout. Lots of hotglue completes the mounting

The buttons only required some measuring to make sure they are neatly symetric and do not interfere with the LCD

All that remains is the IEC socket where the power will go into the project box

Now with everything in place, all we need to do is connect the components together with some wire using the below schematic

Warm up the soldering iron and just follow the diagram. As my soldering skills are not that great, I also used a bit of heatshrink here and there to make sure that wires could not short on each other :)

I’m using an old iPhone charger to power the Arduino as it provides a neat 5V/1A, with a small retractable USB lead with a Type A to B converter. A quick test of the wiring to make sure it all works as planned

Now with construction over, it was time to do the Arduino coding. The LCD uses the LiquidCrystal I2C library which takes care of all the hard work interfacing to it. Likewise, the Metro library takes care of the timer. So, it is just a simple case of increasing the clock by an hour every time the red button is pressed, and starting the timer when the green button is pressed. Once the timer is running, any button press will stop the timer and reset the state to the beginning.

// Arduino controlled timer socket
// (CC BY) 2011
// http://awooga.nl
 
// pull in libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Metro.h>
 
 
// arduino pins constants
#define RELAY 2 // black wire connected to input on shield
#define RED   6 // green wire
#define GREEN 7 // white wire
 
// i2c pin A5 = white wire
// i2c pin A4 = green wire
 
// allow between 1 and 6 hours
#define MINIMUM_TIME 1
#define DEFAULT_TIME 6
#define MAXIMUM_TIME 6
 
 
const unsigned long MILLISECONDS=1;
const unsigned long SECONDS=(1000*MILLISECONDS);
const unsigned long MINUTES=(60*SECONDS);
const unsigned long HOURS=(60*MINUTES);
const unsigned long DAYS=(24*HOURS);
 
const unsigned long UNITS=HOURS;
 
 
// prgram states
#define IDLE         0
#define RED_BUTTON   1
#define GREEN_BUTTON 2
#define MENU         3
#define PAUSE        4
#define STOP_TIMER   5
#define CANCEL_TIMER 6
 
 
// global variables
byte events;
byte countdowntime;
boolean menu_running;
boolean timer_running;
 
boolean debug = false;
 
byte currentRed = LOW;
byte currentGreen = LOW;
byte previousRed = LOW;
byte previousGreen = LOW;
 
// set the LCD address to 0x27 for the 16x2 display
LiquidCrystal_I2C lcd(0x27, 16, 2);
 
// initiate Metro object
Metro relayMetro = Metro(0, true);
 
 
// arduino setup routine
void setup() {
 
  // set the arduino pins
  pinMode(RELAY, OUTPUT);
  pinMode(RED, INPUT);
  pinMode(GREEN, INPUT);  
 
  // initialise the lcd
  lcd.init();
  lcd.backlight();
 
  // activate debugging on the serial port
  if (debug) {
    Serial.begin(9600);
    Serial.println("Lets go!");
  }
 
  // initialise variables
  events = IDLE;
  countdowntime = DEFAULT_TIME;
  menu_running = false;
  timer_running = false;
  digitalWrite(RELAY, LOW);
}
 
 
// arduino loop routine
void loop() {
 
  switch (events) {
 
    case IDLE:
      events = Idling();
      break;
 
    case MENU:
      events = MainMenu();
      break;
 
    case RED_BUTTON:
      events = RedButton();
      break;
 
    case GREEN_BUTTON:
      events = GreenButton();
      break;
 
    case PAUSE:
      events = Pause();
      break;
 
    case STOP_TIMER:
      events = StopTimer();
      break;
 
    case CANCEL_TIMER:
      events = CancelTimer();
      break;
 
    default:
      events = IDLE;
      break;
  }
}
 
 
int MainMenu() {
 
  if (timer_running) {
    return IDLE;
  }
 
  if (menu_running) {
    return IDLE;
  }
 
  if (debug) { Serial.println("MainMenu()"); }
 
  lcd.clear();
  lcd.print("  Red: Set time");
  lcd.setCursor(0, 1);
  lcd.print("Green: Go (");
  lcd.print(countdowntime, DEC);
  if (countdowntime == 1) {
    lcd.print("hr)");
  }
  else {
    lcd.print("hrs)");
  }
 
  menu_running = true;
 
  return IDLE;
}
 
 
int Idling() {
 
  boolean do_red = false;
  boolean do_green = false;
 
  if (timer_running) {
    if (relayMetro.check()) {
      return STOP_TIMER;
    }
  }
 
  currentRed = digitalRead(RED);
  currentGreen = digitalRead(GREEN);
 
  if (currentRed != previousRed) {
    do_red = (currentRed == HIGH);
  }
  previousRed = currentRed;
 
  if (currentGreen != previousGreen) {
    do_green = (currentGreen == HIGH);
  }
  previousGreen = currentGreen;
 
  if ((do_red) && (!do_green)) {
    return RED_BUTTON;
  }
 
  if ((!do_red) && (do_green)) {
    return GREEN_BUTTON;
  }
 
  // all other button combinations are ignored
  return MENU;
}
 
 
int RedButton() {
  if (debug) { Serial.println("RedButton()"); }
 
  if (timer_running) {
    return CANCEL_TIMER;
  }
 
  if (++countdowntime > MAXIMUM_TIME) { countdowntime = MINIMUM_TIME; }
 
  lcd.clear();
  lcd.print("Time set to");
  lcd.setCursor(0, 1);
  lcd.print(countdowntime, DEC);
  if (countdowntime == 1) {
    lcd.print(" hour");
  }
  else {
    lcd.print(" hours");
  }
 
  menu_running = false;
 
  return PAUSE;
}
 
 
int GreenButton() {
  if (debug) { Serial.println("GreenButton()"); }
 
  if (timer_running) {
    return CANCEL_TIMER;
  }
 
  lcd.clear();
  lcd.print("Running timer");
  lcd.setCursor(0, 1);
  lcd.print("for ");
  lcd.print(countdowntime, DEC);
  if (countdowntime == 1) {
    lcd.print(" hour");
  }
  else {
    lcd.print(" hours");
  }
 
  unsigned long t = countdowntime * UNITS;
 
  timer_running = true;
  if (debug) {
    Serial.print("Setting metro for ");
    Serial.print(t);
    Serial.println(" seconds");
  }
  relayMetro.interval(t);
 
  digitalWrite(RELAY, HIGH);
 
  return IDLE;
}
 
 
int StopTimer() {
  if (debug) { Serial.println("StopTimer()"); }
 
  digitalWrite(RELAY, LOW);
 
  lcd.clear();
  lcd.print("Timer finished");
 
  timer_running = false;
  menu_running = false;
  relayMetro.reset();
 
  return PAUSE;
}
 
 
int CancelTimer() {
  if (debug) { Serial.println("CancelTimer()"); }
 
  digitalWrite(RELAY, LOW);
 
  lcd.clear();
  lcd.print("Timer stopped");
 
  timer_running = false;
  menu_running = false;
  relayMetro.reset();
 
  return PAUSE;
}
 
 
int Pause() {
  delay(1000);
  return IDLE;
}

Or just download the .pde file here

Obviously you can go all crazy with options and clocks, but I just wanted something quick and working. Changing the code to do all that is just a matter of opening it up, and uploading the new software.




And just to prove it is all working, here’s a small video demonstration. It doesn’t show the timer ending, you just have to take my word on that :D

Bye Bye Standby double wall switch

I’m a big fan of Bye Bye Standby and am slowly replacing all switches and lights with their products. One thing that is sorely missed though, is a double wall switch. The kind that sits at the bottom of your stairs and controls the lights upstairs and downstairs. Home Easy do one, but that protocol is not compatible. So I combined the best of both worlds and cobbled one together myself :)




Start by stripping all switches down to their bare PCBs. The idea is to use the push button switches inside the Home Easy HE308 switch and connect them to the push button switches from the Bye Bye Standby wall switches and then stuff it all in the wall.

Carefully solder wires to each BBSB switch and then solder them to the HE308. As the PCB of the HE308 connects some of the pins, it wouldn’t work when I tested it. No problem though, it just meant that I had to get my faithful Dremel out and dremel away all PCB lines surrounding the switches.

Make sure you test it once it’s all been connected up, not just when you’re finished putting it back into your wall ;) At this point in time, make sure you have set the address codes to the correct modules.

As the whole package is larger than my backbox, I removed the bottom from a plastic backbox and used a hammer and old screwdriver to chisel away at the wall behind it. Once I had everything inside and made sure that it was still working, I discovered that the edges from the backbox were larger than the switch itself. Doh!

I took everything out again and carefully reduced the edging until it all fitted neatly underneath the switch.






For those who are worried about the batteries, they are no more difficult to replace than a normal BBSB switch. I would replace both at the same time though if you have to :)

The Mul-tea Charger

Who doesn’t have a multitude of devices, phones and other gadgets on their desks? I certainly have, and I finally had enough of all the chargers and wallwarts that each item seems to bring with them. With two iPhones almost constantly being charged, and a Nokia phone thrown in for good measure, I needed to have something flexible enough for those plus any future gadgets. So, I googled a bit, and found the IDAPT I3, and the beautiful, but ridiculously priced The Sanctuary. And then I started to think, how easy would it be to put a mains powered USB hub inside a box and have several USB charger leads coming out of the box, each charging a different device?




And the answer is, very easy :)

I started off with a 4 port powered USB hub, which I had lying in a drawer and plugged my iPhone charger leads into it, expecting them to instantly charge my two iPhones. But to my surpise, nothing happened… so another quick Google later, I came accross a blog entry of Carl Hutzler, which details why they won’t charge. All you have to do is sacrifice the hub and forego it’s PC functionality by cutting the D+/- lines and short them. A quick test shows they now finally charge themselves. Apparently it is better to stick 2x 100K Ohm resistors on the D+/- lines, which I will do at some point, but for now, this will do.

All I now had to do was find a suitable SWMBO friendly container, and as our furniture is all beech, the tea storage container I found for £1.99 at QD stores was perfect. I just needed to gut the compartiments out and put something on top of the plastic lid. At my local crafts store I found a small foam pad for 50p which was nice and soft. Unfortunately I got my measurements all wrong, so I messed up the black pad, but SWMBO came to the rescue by rummaging through my kids crafts drawers by digging up a piece of brown padding.

The final thing to do is then drill some holes at the locations convenient for your devices, and put it all together. Job done!





Current Cost Classic vs CC128

Back in November I bought (well, actually I signed up to a new deal with E.ON which included one) a Current Cost electricity monitor, and hooked it up to my server so I could gather the stats for Cacti. I do this by running a small perl script which looks as follows:

#!/usr/bin/perl
# /usr/local/bin/cc-classic.pl
 
use Device::SerialPort qw( :PARAM :STAT 0.07 );
 
$port = "/dev/currentcost";
 
$ob = Device::SerialPort->new($port)
      or die "Can not open port $port\n";
$ob->baudrate(9600);
$ob->write_settings;
$ob->close;
 
open(SERIAL, "+>$port");
while ($line = <SERIAL>)
{
  if ($line =~ m!<ch1><watts>0*(\d+)</watts></ch1>.*<tmpr>\s*(-*[\d.]+)</tmpr>!)
  {
     $watts = $1;
     $temperature = $2;
     print "watts:$watts temp:$temperature";
     last;
  }
}
close(SERIAL);

This would give me the two values I am interested in; watts and temperature (since it sits in the garage node 0 ;)) in Cacti’s format:

$ /usr/local/bin/cc-classic.pl
watts:761 temp:11.3

But today, I received my new unit, a Current Cost CC128. It’s main benefit is that it supports individual appliance monitors, which makes the output even more useful. So, armed with a draft copy of the CC128 XML output document, I prepared my script to read as follows:

#!/usr/bin/perl
# /usr/local/bin/cc-cc128.pl
 
use Device::SerialPort qw( :PARAM :STAT 0.07 );
 
$port = "/dev/currentcost";
 
$ob = Device::SerialPort->new($port)
      or die "Can not open port $port\n";
$ob->baudrate(57600);
$ob->write_settings;
$ob->close;
 
open(SERIAL, "+>$port");
while ($line = <SERIAL>)
{
  if ($line =~ m!<tmpr>\s*(-*[\d.]+)</tmpr>.*<ch1><watts>0*(\d+)</watts></ch1>!)
  {
     $watts = $2;
     $temperature = $1;
     print "watts:$watts temp:$temperature";
     last;
  }
}
close(SERIAL);

And guess what… that works just fine ;)

For those who read diff:

$ diff /usr/local/bin/cc-classic.pl /usr/local/bin/cc-cc128.pl 
2c2
< # /usr/local/bin/cc-classic.pl
---
> # /usr/local/bin/cc-cc128.pl
10c10
< $ob->baudrate(9600);
---
> $ob->baudrate(57600);
17c17
<   if ($line =~ m!<ch1><watts>0*(\d+)</watts></ch1>.*<tmpr>\s*(-*[\d.]+)</tmpr>!)
---
>   if ($line =~ m!<tmpr>\s*(-*[\d.]+)</tmpr>.*<ch1><watts>0*(\d+)</watts></ch1>!)
19,20c19,20
<      $watts = $1;
<      $temperature = $2;
---
>      $watts = $2;
>      $temperature = $1;

Please note, the above only works with 1 sensor (the main transmitter), so it is likely to change in the future. For now it suits my need.

Lies, damned lies, and statistics

I’ve been using a 1 wire network for quite some time now, but when I deleted a directory to much on my server, I lost a lot of the stats that I had gathered. A couple of weeks ago I finally got my behind in gear again and rebuild my network, this time making sure it all gets backed up ;)

One day I’ll write something about how it’s all been done, but for now you’ll have to suffice with some pretty graphs.

Here’s the daily graph for the temperatures in our bedrooms for the past 24 hours:

Daily temperatures Bedrooms

And recently I added a Current Cost meter to my network, which gives me the shocking facts about my electricity usage for the past 24 hours:

Daily electricy usage

The above graphs are updated hourly, and I’ve got other graphs too, extending the period of graphing. You can find them here for the time being.

Update 25.1.2009

And now you’re able to follow the stats on twitter: http://twitter.com/awoogadotnl