A very large hammer to crack a very small nut!
I'm an early riser and always bring my wife a coffee when she wakes up. Instead of her calling out for one, or even ringing a bell, what better way than to employ one of my Raspberry Pi Zeros (and a tacky, £1.50 plastic palm tree with LED lights)?
The RPi0 serves up a webpage with three buttons:
- order a coffee
- acknowledge order [me]
- acknowledge receival of coffee [wife]
When the Order button is pressed, it lights up the palm tree; Acknowledge button updates order status; and the Receive button turns it off and resets the system - simples!
sudo apt install python3-pip
pip3 install flask
Python Flask is used to serve a web page and provide RESTful APIs.
The single web page uses Javascript to call various APIs to order and receive a coffee.
The python server tracks order status in a global variable, so that I can acknowledge the order from another computer.
The web page regularly polls the python server for the order status, so my wife knows I have received the order.
Python GPIO is used to turn on/off power to the LEDs.
Various scripts are run on system startup from:
/etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
sudo /home/trevorde/coffeeServer/index.py &
exit 0
[optional] Prevent wifi going to sleep
iw wlan0 set power_save off
- this is persistent across reboots
- do not do this in
rc.local
as network interface may not be initialised
[optional] To minimise file corruption, make file system read-only:
https://medium.com/swlh/make-your-raspberry-pi-file-system-read-only-raspbian-buster-c558694de79
- very difficult to update system
date
(bash script + python) function returns incorrect date+time
order status is held on a per session basis but this information is used to control a single output. This is incorrect and the order status should be held globally, typically in something like Redis.sometimes it takes up to 20s (!) after clicking the Receive button for the LEDs to turn on. This is why the web page has a 'progress bar' (UpdatePlacingOrder) to show that something is happening. I suspect that the Python Flask server shuts down threads after a period of inactivity, as subsequent API requests (clicks on Send or Receive) are very fast.- install this as a service so it does not have to run as root