Tuesday 1 March 2016

Python based home automation - start of the project

I have been immensely disappointed with the web server installations I made using both apache2 and lighttpd software on the Raspberry Pi. I had an infinite number of "permissions" issues when trying to get CGI scripts to run, I did get a PHP script to run, but never did manage to run any CGI Python scripts.

So I have now tried a very simple server directly built in Python. The trouble here is that the modules used in Python 2, for which I found a few tutorials on the web, are completely different from the modules used in Python 3! And since I cannot get an understanding of the Python 3 versions, I have been obliged to use Python 2. But it works and is only a few lines of code. See server.py code.

Anyone playing with the python code should realise that you can build either an HTTP server for web pages, or a CGI server to respond to CGI pages, but not one server for both. so if you wawnt CGI you have to write your HTMLpages as an executable CGI page, starting "#!/usr/bin/python2"

The second problem I had, and still have, is accessing the Raspberry Pi's GPIO pins without being superuser. The GPIO pins are memory amped and controlled through the /dev/mem device. Obviously it would be dangerous to allow anyone complete control of the memory driver, so it is all owned by "root", thus even any program wishing to use the memory mapped gPIO pins has to be run as superuser.
I installed the RPi.GPIO module, this provides two ways to access GPIO, from within a Python script - which needs the script to be run as superuser - or from the command line as a utility, which does not require superuser! Why? Using RPi.GPIO I made the home automation project I described previously.

I also tried the gpiozero module which seems not to need superuser, but I found that when run in a CGI Python script called by another program running on a server it refuses to access GPIO... no real idea why. Frustrated, I abandoned it.

So I am back to using the command utilities provided by RPi.GPIO, and calling them with a shell executive from my Python program, much as I did previously.

Why do I want the whole project to run in Python? Well I have found a supplier of sockets that are controlled by radio signals, and the same supplier, Energenie, has a small RF Transmitter board that plugs into the Raspberry Pi, and they have a Python module to control it, and through it the sockets. So what I need is a Python program controlled by an HTML web page that sends signals to the Sockets. Like this:

Server.py -running- lights.py (HTML code output) -driving- control.py (GPIO control, eventually to be Energenie control)

Here is the Python code for lights.py and control.py.

The web page display for the lights is very crude today, but is easily beautified using a few styles and a better table layout - tedious programming work, but I will get round to it maybe today...

Screen Shot 2016 02 29 at 16 31 32

The GPIO outputs I have programmed today are 22, 23, 24 & 25. These each drive a 330R + LED. So if you fancy trying to build some simple hoe automation, give it a go.

No comments: