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.