Pi Mote - Lights Control

Posted on Wed 02 November 2016 in RaspberryPi

I started to loath my Raspberry Pi's. I brought one when they first came out and somehow amassed more over time. My head was full of all the amazing things I was going to build and automate, spurred by all the fantastic projects I'd seen online.

At first, I did what I believe everybody does, set up XBMC, Kodi, RetroPi, all the extras, linked it to my mobile and sat it at the back of the TV. All that was fun, but it wasn't really doing anything with the RaspberryPi, the pins the motors the lights. An Android TV box would have done the same job. I'd always found things a little clunky. Media things should never be clunky. Eventually my home media setup evolved and the RPi sat, at the back of the TV, unused, gathering dust.

I found myself with a little time. I was determined, for some reason, to do something with one of these things.

I figured turning a lamp on and off via a command would be pretty basic. An RPi, a relay, lamp and probably a tonne of mess. Advancing to adding a web page where the relays could be controlled. Before starting this I had very little knowledge of HTML, CSS, GPIOs, Python, Electrical Engineering or how any of it would fit together.


One thing I was quite conscious of was not burning my house down, or killing one of my cats via electrical shock. I've been zapped, badly. It hurt, badly. Electricity generates heat and sparks. They lead to fires. Rule 0 - don't be on fire. Following some general rules of thumb I figured I couldn’t go too wrong.

It shouldnt be hot - if components are heating up, things are under too much loadBare wires are dangerous, regardless of the wire, consider it liveWater is not your friendIf it looks damaged or frayed, dont use it

Using GPIO Pins to Control the Relay

The first thing I had to do was work out how to make the GPIO pins switch the relays on and off.

alt text

This video https://www.youtube.com/watch?v=3Pikhq99q5E

This website https://www.thepihut.com27968772-turning-on-an-led-with-your-raspberry-pis-gpio-pins

Basically gave me the groundings I needed. The video for the wiring and the website for the scripting.

Note that the relay has a set of pins, a light for each of the relays and the pins on the relays themselves. When the relay is active, light on, it also makes a satisfying clicking sound too. The pins are 5\3V for power from the Pi, GND, for ground back to the Pi and a signal pin for each of the relays, going back to GPIO pins on the PI.

I used:

Pin 2 for 5V

Pin 6 for GND

Pins 7, 11, 13, 15 for signal

I wrote a bunch of scripts that did various sequences, just so I knew I could properly control the relays. If you open the scripts up you'll easily be able to see how each pin is addressed.

Wiring the Lamp

Next was attaching a lamp to the other end of the relay. The video came in very handy. The live needs to pass through one side of the relay and back out to the device. The neutral needs to be re-joined together along with, if present, the earth.
I've tried my best to show this.

alt text

Running the command "python ./scriptname.py" from a terminal ran the script and activated\deactivated the GIPO. Awesome, step one complete. I can control a lamp using a relay via a command on the Raspberry Pi. FINALLY!

HTML\CSS - Writing a Webpage

Next was making a webpage. I had very little clue how to do this. I knew I needed buttons, so I made some buttons first. Gotta start somewhere! http://mygimptutorial.com/round-web-20-button-with-a-metal-ring I scratched out a rough drawing on paper so I had something to go by.

alt text

I followed this tutorial till about lesson 5. https://www.youtube.com/watch?v=A3Xgz9PHGuA By that point I had a pretty tidy looking page of switches. Basic, but exactly what I needed.

alt text

I then linked the buttons to the scripts using an href in the images HTML.

"<td><h3>Lamp<div id="1blue"><a href="lampon.php"><img src="/BlueButtonS.png">"

Lamp on .php isn't the python command that changes the GPIO state. It’s a .php script that calls the .py script and instead of redirecting to the lampon.php page (which doesn't exist) redirects to the same page.

"system("cd /var/www/html && sudo python lampon.py"); header("Location:");"

** I noticed some issues using copy and paste with putty and copying files via Filezilla. Previously working scripts stopped working. The workaround was to re-create the files and manually type it out on most occasions.

Final Thoughts

From start to finish it took me four days to put everything together. Learning each step as I went along. I created some shortcuts on my Android home screen that pointed directly at the links to the buttons. I can either go to the webpage to switch the lamps on and off or use the home screen buttons. Everything was screwed down to the underside of a cupboard near all the devices and power socket so I didn’t have to run long cables. I did have to extend each lamp's original cables but that worked in my favour as it created spare cables, eventually used for the relay to terminal block connections.

alt text