Thursday 3 March 2016

HURRAY!!! Home automation in ONE file

I have just discovered that an HTML "form" element can actually "action=..." the file that generates it!

What does this mean?

This means that the same Python executable CGI file can BOTH generate the HTML to display a form on the web browser, AND then receive the "POST" data and take action on it.

This means that my file light_control.py which is an amalgamation of the previous lights_v2.py and control_v2.py of the last post can just by itself give me the result I want!!! HURRAY!!!

I had no idea it would be so simple, its taken me hours of web browsing to find this, everyone doing Home Automation programming should know. We still have the clumsy business of only being able to address the Raspberry Pi's GPIOs as superuser with Python module RPi.GPIO, or use the wiringPi GPIO command line tool to control them, but hey-ho it's a step forward.

DOWNLOAD THE CODE for light_control.py NOW. And run the server_v2.py then go to "raspberrypi.local:8000/light_control.py" to see it working. (you may have to enter your Pi's IP address instead of "raspberrypi.local" above if you have not install the daemon "avahi", or do it now).

So to sum up

1 Make a new directory called "Server" (or whatever you chose) under your "pi/home"

2 Download light_control.py and server_v2.py into this location.

3 IMPORTANT!! Login to your Pi from a Terminal app using "ssh pi@raspberrypi.local

4 Then make both files executable "chmod a+x file_name" - keep forgetting this myself.

5 Run the server "./server_v2.py" which uses port 8000 by default

6 Go to raspberrypi.local:8000/light_control.py in a browser to see the page:

Screen Shot 2016 03 03 at 16 38 16

You can also browse with your iPhone/Android phone to get the page, then add it to your home page!

No comments: