Adding web connectivity (via USB ) to remote controlled sockets – Part 2 (software)


This project page provides a guide to adding both USB and a Web based User Interface (UI) to remote control socket set. Through the process we will unlock even more capabilities than are available when you use the socket set as they were originally intended. Part 1 deals with the hardware, Part 2 (this post) deals with programming the chip and Web UI.

The main components are:

  • Arduino (or other AVR-MK2 programmer)
  • Web-server (I used a Raspberry pi, but any computer will work if it’s running Apache and PHP)
  • Lego (if you want to make a Raspberry pi case)

 

Okay, first step is to program the AT Mega 8 chip.

I don’t have a AVR-MK2 programmer like [befinitiv], but I do have a number of Arduino boards. There are a number of ways to program an AT Mega 8 with an Arduino, but the most successful method I have found is to  use mega-isp code for Arduino.  A very good tutorial can be found at [blog.ntsd.net].  Upload mega-isp onto your Arduino.

programming an atmega8 with arduino mega-isp

mega-isp: programming an atmega8 with arduino from [web archive - blog.ntsd.net]

If you would like to program the chip before you have soldered it all together, follow the instructions above. If you have the hardware already assembled use the programming cable (purple, grey, white, black)  that sticks out the of the remote control at the end of the first tutorial (see image below).

In this case the purple is reset (Arduino pin 10), grey  (Arduino pin 13), white  (Arduino pin 12), black  (Arduino pin 11).

Using a Linux machine with avrdude installed run the following command in the terminal

$ avrdude -p atmega8 -c avrisp -P /dev/ttyUSB0 -b 19200 -U lfuse:w:0xe4:m -U hfuse:w:0xda:m

This is a combination of the standard setup for mega-isp and setting the fuses as done by [befinitiv]. The ttyUSB0 in the code above is the Arduino, so this may need to be changed, but if you unplug all USB peripherals and plug in the Arduino first it should be ttyUSB0.

  • Plug in the remote control and hook up to the Arduino as mentioned above.
  • Now download the code from my gitHub repository.
  • Navigate to the folder “remote_controlled_socket” in the terminal.
  • Run make.
  • Now run make program

The code above largely builds on the code provided by [befinitiv], but adds extra support for up to 15 devices and adds channel selection to the mix.

Okay, now we have the remote all set up it’s time to set up the web based control. Essentially what we need here is a basic web server running PHP. Place the files from the “SerialPowerControl-web” folder on the server. As I’m running a Linux server so I have to alter the dialout group settings to allow the PHP script to communicate over the USB.

$ sudo pico /etc/group

-> add “,www-data” to end of dialout:x:***  (without quotes)

Now I’m using a Raspberry Pi as my server (as it’s a really cheap, low power and tiny computer) and I encountered a slight problem as the Apache server isn’t configured properly, to resolve the issue you need to do the following:

$ sudo groupadd www-data

$ sudo usermod -a -G www-data www-data

$ apt-get install php5-common libapache2-mod-php5 php5-cli
after installation, then restart Apache:
$ sudo service apache2 restart

If eveything is successful you should end up with the interface as shown below (point the browser to the server location of the power scripts) and once the remote is plugged into the servers USB port (make sure it’s the first USB device plugged in, so that it becomes ttyUSB0). This was easily thrown together using PHP and the jQtouch library meaning it will work in any Webkit Browser (Chrome, Safari, iOS and Android), it still works in Firefox, but loses it’s visual appeal.

Power User Interface

Feel free to edit the scripts on the server so that the text reflects your own set up. You can add more devices (up to 15 per channel), events or change the default channel (the check boxes at the bottom of the image above, there are 32 possible channels). Check out the README file that comes with the scripts for the mapping of device numbers and the settings on the sockets (4-bit code). Let me know in the comments how you get along, the changes you make, or the issues you encounter.

I think one of the next steps is to try and add manual control to the sockets, there is no switch on the socket itself. You can of course use the wall socket switch if available, but this completely disables the remote feature. I also have one socket circuit embedded in the light switch for the main room lights, again the actual switch will turn off the lights but also disables the remote feature. So the new feature will have to allow for manual control without disabling the remote feature.

 

Bonus!

You can use the power.php and the GET method to use direct links to activate devices or profiles. Say for example I wanted to turn on device 1 on channel 4 (dip switch 4 down)

http://server-IP/power.php?p=1&c=11101

similarly for events

http://server-IP/power.php?e=leave&c=11101

 

Extra Bonus!

Making a case for your Raspberry Pi out of Lego. I got the inspiration from a bunch of scouts that have a post on the Raspberry Pi blog. I spent an evening going through my old Lego collection in my attic, collecting the necessary pieces.

Raspberry Pi and Remote Control Power

Notice the clear blocks to let the power and Ethernet indicator LEDs light through.

To make your own I’ve included images of all sides, but essentially the Raspberry Pi is 7 x 11 in Lego dimensions, making the case 9 x 13.

Raspberry Pi Lego Case Bottom

Bottom

Raspberry Pi Lego Case TopTop

Raspberry Pi Lego Case Video outputs

Video and Audio Outputs

Raspberry Pi Lego Case USB and LAN

USB and LAN ports

Raspberry Pi Lego Case Power and SD ports

Power and SD ports

Raspberry Pi Lego Case HDMI output

HDMI

14 comments on “Adding web connectivity (via USB ) to remote controlled sockets – Part 2 (software)

  1. Pingback: » Adding web connectivity (via USB ) to remote controlled sockets – Part 1 (hardware) Jake Rowan Byrne

  2. Pingback: Home automation with RC wall plugs and Raspberry Pi | Share Blog

  3. Philippe on said:

    Great Job !!!

    your design is very safe and does not depend on the computer used for the webserver, letting the microcontroller talk to the real world. Since the raspberryPi is equipped with GPIOs I guess you could have done without the FTDI cable and the arduino

    • jake on said:

      Thanks Philippe!

      Yeah, I’ve considered the use of the GPIOs on the Raspberry Pi and I’ll definitely look at doing something with them in the future (maybe add occupancy detection, so lights can be turned on/off when people enter/leave the room).

      I suppose the plus of the approach here is that it’s more general and works across all devices with USB connectivity. For example I have a regular LAMP server at home and this method still works with it.

  4. Pingback: electronics | Pearltrees

  5. Wallace on said:

    I’ve just linked to this from the Pi Forums ,

    Someone was asking how to control a mains device safely from a Pi .

    http://www.raspberrypi.org/phpBB3/viewtopic.php?f=37&t=7855&p=98065#p98065

    ( very cool work btw , for both yourself and the guy who started it )

  6. Massimiliano on said:

    Hi

    interesting post :)
    But I have a question: where can I find the php script used ? :)

    thansk

    Max

  7. Kelvin on said:

    hi, i have an adruino uno compatoble device and i am Trying to load the bootloader into the ATMEGA 8. The link you have provided above (blog.ntsd.net) shows a blank website. I was wondering if you could show how you burn the bootloader to the ATMEGA 8 and if other ATMEGA would work just as well like the 168 or the 328.

  8. Kelvin on said:

    Hi Jake,
    I am new to linux and raspberry pi. Thank you for your guidance so far.

    (1)I would like to know how do you place the files into the server folder? How do you locate your server folder?

    (2)I would also like to know if there is a command to see which ttyUSB the arduino is connect to.

    Thank you for the help

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>