Control big mean devices with an Arduino

Since Codebits, i had the Arduino in the bag… also in the bag (due to time constraints) the desire to put it to work, to do something with it, anything. Today they both jumped out of the bag, so the goal is to make a remote mains switch with a big red button (the end of the world type), sure i can walk to the switch but this way is much more fun :)…

The first step is to control (switch on/off) a mains powered device with an Arduino, wich operates in low DC voltage. For that i needed a Solid State Relay (other routes possible here), and curious enough that was exactly what i had today in the mailbox from China. These are really cheap from Ebay, just take time to read specs and match up to your needs. Remember, Volts x Amps = Watts

Ex:
a 100w lamp = 220v * x amps = 100W = 0.45 amps
a 3000w heater = 220v * x amps = 3000W = 13.6 amps

so for the heater the SSR should have at least an Output Current of 15 amps (or a bit higher to play safe),  also the 220v should be within the Output Voltage range. The Arduino output Voltage is 5v, so also check the SSR Input Voltage range for 5v support (if not in range, you will not be able to control the SSR with the Arduino alone).

I used an old appliance cable, just strip the wire and there should be 3 wires, the green/yellow cable is the ground, dont touch this one, you should cut one of the other wires (normally a blue or gray). Strip each cutted side and connect the AC side of the SSR to them. Now connect the Arduino to the DC side of the SSR, one digital pin to positive and ground to negative. You can store the tools now.

You can connect now the Arduino to the computer, install the drivers if needed and download the Arduino Environment, just follow the instructions from the Arduino Website and you should be up and running in minutes. The code is as simple as it gets, it reads from serial and if receives 1 the pin goes high (with voltage) and if 0 the pin goes low (with no voltage).

int pinNumber = 13;
int incomingByte;

void setup() {
    Serial.begin(9600);
    pinMode(pinNumber, OUTPUT);
}

void loop() {
    if (Serial.available() > 0) {
        incomingByte = Serial.read();
        Serial.println(incomingByte);
    }

    if (incomingByte == 48) {
        digitalWrite(pinNumber, LOW);
    } else if (incomingByte == 49)  {
        digitalWrite(pinNumber, HIGH);
    }
}

I choose pin 13 (Arduino to SSR positive) because there is a built in led that can help in debug. You can test now from the Arduino Environment, simply open the Serial Console (under Tools) and send 1 and 0 and it should light up and down. You can also connect using the Putty – a fine ssh client with serial interface or even the venerable Windows Hyperterminal. Just remember this is serial, so one client at a time :).

When you connect/disconnect from any client there was a rapid light flicker. First i thought that it was something related to the communication that was sending zeros and ones in the handshake or something. But i was wrong (normal), this is a feature of the Arduino to simplify and automate new programs upload. For every serial connection it resets itself, hence the flicker, and waits for a new program upload (sketch in Arduino lingo) for a couple of seconds then if there is not nothing being upload it proceeds to the normal program execution from the start (with state loss). You can disable the auto-reset feature to meet your needs if you want.

Of course you want to control it in some programaticaly way, so me first try was with PHP, there is some code floating around in the internets, something like:

$port = fopen('COM1', 'w'); // COM number where the Arduino is
fwrite($port, '1');
fclose($port);

and to light off something like:

$port = fopen('COM1', 'w'); // COM number where the Arduino is
fwrite($port, '0');
fclose($port);

BUT THIS CODE OBVIOUSLY WON’T WORK ON CURRENT STANDARDS ARDUINO, due to the auto-reset feature that i mention in the previous paragraph. It will open the COM (reset), then it will send too fast the bit to the port, when the device is still on the auto-upload sketch mode (witch is the reason of the auto reset in the first place), then it will close the connection (reset again). So you will only end up with some fast light flicker….

This code will work:

if ($port = fopen('COM1', 'w')) { // open arduino port
    sleep(2);                     // wait for end of auto-reset 

    $i = 1;
    while (true) {                // loop to keep port open
        if ($i % 2)               // if i is even lights on
            fwrite($port, '1');
        else
            fwrite($port, '0');   // else lights off

        sleep(2);                 // waits 2 secs between each cycle
        $i++;
    }
    fclose($port);
} else {
    print("Check port number or previous active session");
    die();
}

from here you could work it out, to make a daemon that connects to the Arduino via serial and listens to some socket and routes/proxies the input from socket to serial.  It’s doable, and not that hard but anyway PHP is not the correct tool for the job, at all… so used the Serproxy – a proxy program for redirecting network socket connections to/from serial links – very easy to use and makes just what i wanted.

From there you simply connect and send the on/off (1/0) command to the socket, and don’t have to worry about auto-resets. When you close the socket connection the serial link always stays up. So, making a web interface from here was simple, and it was what i have done just for the fun of it (PHP works good here).

Here it is working:

You can light my desk lamp now 🙂 on http://light.waynext.com/, sorry no upload bandwidth to put a webcam stream (Porto Salvo = Juda’s ass) so you have to trust my word, call to Waynext or check out the video.

Your RGI (Reality Gateway Interface) is now complete. Next step is to put it to work remotely, with the Arduino unconnected from the computer, must dig into xbee shields. Also to dig into a direct interface with PERL, Python or Java.

Codebits 2010

Just arrived from Codebits 2010. By far the best technology event/contest/conference/meeting/etc in Portugal.

This year i submited no project, due to a logistic problem regarding (lack of) rocket propellant. Anyway, this is a project to be pursued outside Codebits, as it is just plain fun.

I attended several talks (unfortunately is impossible to attend all), but i must highlight Spacebits by Celso Martinho – 100% pure fun, Inovation u r doin it wrong by Mario Valente – food for tougth, and Everything OAuth by Bruno Pedro – made this subject clear as water. Also, attended the soldering workshop by the Lisbon AltLab folks, made a pair of reciprocate blinking leds, with some really lame soldering (come on, no soldering experience at all). Got me a tv pong kit to try to assemble next weeks and improve my soldering skills.

For sure i couldn’t miss the Quizz Show, its too much fun. This year it was the old guys (João Pedro Gonçalves and Celso) who kicked all the competition, and showed off their geek culture.

The final stage of Codebits was also really interesting, the teams project presentation, the overall projects quality was really good, as allways some guys/apps freeze while up on stage (been there, done that) which is cruel regarding the hard work, but is always funny for the audience. Overall must agree with the selected winners, maybe except the guys that presented a earthquake data collection project, only because i didn’t actually see any prototype working just an idea presentation (a very good ideia by the way).

Thinkered a lot with node.js and socket.io (Javascript continues to build up) and eated lots off pizza, m&ms and burguers… i also took home some books, photos and videos:

For me, the only (minor) glitch in Codebits, was the very low light environment, that sometimes made difficult to work on the laptop, maybe they were just promoting Apple backlit keyboards…

Note to myself: next laptop must have a backlit keyboard

Anyway, its just plain amazing how you can compress in 3 days so much learning and fun at the same time. Ear (and sometimes talk back to)  so much talented and diversified people (from CEOs to students) . Still can’t understand how some friends and colleagues couldn’t care less about Codebits.

And the cherry in top, is that its all free. Thank you SAPO, see you next year.

ps – also lost somewhere between home and Codebits, my Saturn v 10$ model that bought at KSC, really not a big deal but had some sentimental value