Making a Remote Controlled Power Switch part 2

This is part 2 in my project – Making a Remote Controlled Power Switch. This first entry discusses on some initial decisions. I’ve decided on a wired network interface, DHCP addressing, and a centralized mechanism for finding the device’s address. This part of the project will include a simple web interface for toggling a LED. A later part of the project will essentially replace the LED with a relay. For now I’ll be simulating power state with a LED.

My hardware consists of Arduino UNO, LED, resistor, and the Ethernet shield. The circuit uses digital pin 2 to control a red LED. The Arduino’s Ethernet port is plugged in to my local router. The router assigns IP addresses using DHCP. If you need to assign the network parameters, there will be a place to do so in the setup() function. The code is from bildr’s article on pin control. I modified the control function to toggle the pin’s state rather than blinking. My full code is available here.

void togglePin(int pin, EthernetClient client)
{
  //toggle a pin - Client needed just for HTML output purposes.
  if (pinState[pin] == 0) {
    client.print("Turning on pin ");
    client.println(pin);
    client.print("<br>");

    pinState[pin] = 1;
    digitalWrite(pin, HIGH);
  } else {
    client.print("Turning off pin ");
    client.println(pin);
    client.print("<br>");

    pinState[pin] = 0;
    digitalWrite(pin, LOW);
  }
}

The program will output the assigned IP address over the serial port. Check the serial monitor. With the IP address known, I can control the LED by requesting the URI: http://assigned_ip/?2 . Using the command-line tool curl is really handy in these situations. Assuming my device has the IP 192.168.3.33, here’s a command control sequence turning the LED on and off again.


dspisak@sawfish-db:~$ curl http://192.168.3.33/?2
Turning on pin 2
<br>dspisak@sawfish-db
dspisak@sawfish-db:~$ curl http://192.168.3.33/?2
Turning off pin 2
<br>dspisak@sawfish-db:~$

My next step is to interface with a relay. I ordered the Beefcake Relay Control Kit from Sparkfun. I’ll continue with the series once it arrives.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Trackback

  1. Arduino Remote Power Switch part 3 - luckyfishlab (Pingback)