Close

Software and code

A project log for Heating Controller

Tempted to call this FrankenHeatController or something, but this time round doing it a little bit better.

h3liosphanh3liosphan 11/22/2021 at 01:170 Comments

So it's just a straight forward Pi OS Lite deploy onto a 16GB uSD card, configured totally headless, so ssh and WiFi configs put on the boot partition, apt upgraded, then the pi user removed, and my own substituted with sudo access.

Lighttpd and Python 2.7.16, plus web CGI tie-ins were installed.

I had already PoC'd the code in Python using a pi, so it was a case of moving this to the 'production' pi and adapting it for 'the web',

Here's the web page - 

Some may recognise the 'theme', this is a dark Bootstrap theme, which goes a very long way towards making it look semi-professional and not all web 1.0. This also goes a long way towards making it useable across a wide variety of devices and screen sizes - this appears perfectly sized on my Android Phone, Windows PC, Surface Tablet and Android Tablets.

This is in stark contrast to the web 1.0 pages I made for my LED Hat project, which were a pain to use on a phone.

The menus don't work, the search bar is there but not implemented at all, not needed, but left there to make it cooool.

The 4 buttons are pretty self explanatory - 

Each button is hard coded into index.html - 

<main class="container">
  <div class="bg-dark p-5 rounded">
    <h1 class="text-light bg-dark">Boost</h1>
    <a class="btn btn-lg btn-primary" href="cgi-bin/servo-once.py" role="button"
>once</a>
    <a class="btn btn-lg btn-primary" href="cgi-bin/servo-twice.py" role="button
">twice</a>
    <a class="btn btn-lg btn-primary" href="cgi-bin/servo-thrice.py" role="butto
n">thrice</a>
    <a class="btn btn-lg btn-primary" href="cgi-bin/servo-test.py" role="button"
>TEST</a>
  </div>
</main>

And here's the servo-once.py file (Apologies, the code highlighting for Python doesn't seem to work, not even if I remove the HTML at the bottom, or the #! declaration at the top) -

#!/usr/bin/python
from gpiozero import Servo, LED
from time import sleep

from gpiozero.pins.pigpio import PiGPIOFactory

import cgi

factory = PiGPIOFactory()

servo = Servo(14, min_pulse_width=0.50/1000, max_pulse_width=2.5/1000, pin_facto
ry=factory)
mosfet = LED(15, active_high=True, initial_value=False, pin_factory=factory)

homepos = -0.05
hoverover = -0.9
press = -0.98

servo.value = homepos
mosfet.on()
sleep(0.3)

servo.value = homepos

x = hoverover

while x >= press:
        servo.value = x
        sleep(0.01)
        x -= 0.001

while x < hoverover:
        servo.value = x
        sleep(0.01)
        x += 0.001

servo.value = homepos
sleep(1)

mosfet.off()
#servo.value = None

print("Content-Type: text/html\n\n")  # html markup follows
print("<html><head></head><body><script>window.history.back();</script></body></
html>")

So as you can see the code uses the PiGPIOFactory component to activate the MOSFET to power the servo and actually move the servo. The pigpio daemon is running to allow the lighttpd user to access the gpio ports without running as root / su. The code therefore runs as www-data (or whatever user the sub-component activates from cgi-bin).

There are two phases to the motion, it rapidly moves down to a 'hover' point, then slowly moves the arm down further to press the button, doing this in a while loop, it then reverses this back to the hover point, then back home. It then powers the Servo down again. Delays are inserted to give the servo time to move to that position, and to control the speed of the slow movement.

Trial and error is all that determined the homepos, hoverover and press values hard-coded above.

At the end is a small HTTP injection to return to the previous page so browsers don't freeze and get all confused! As you can see, it's just some javascript to go back a page.

You may notice the #servo.value = None line commented out. This was a remnant of a soft power off, this does work, the servo ends up non-driven and 'loose', but I noticed the servo electronics were still idling at a not insignificant power level. The MOSFET properly powers it down and uses micro/nano watts of energy doing it.

That's pretty much it, it's not presented to the Internet, only accessible inside my home network - there is ZERO requirements for me to be able to activate the heating from my workplace! I can do it from the car outside the house though if I really want!

Discussions