-
Notifications
You must be signed in to change notification settings - Fork 3
Dictionary of buttons
My first idea was to define some kind of coffee-machine JSON semantics, like this:
{
"userID": "admin",
"actions": [
"turn on",
"brew espresso"
]
}
For faster deployment I use another approach right now. Maybe I introduce the JSON semantics as part of a RESTful API in a later state when there is more data accessible (like sensors and statistics).
I created two dictionaries with (I hope) self explaining keys, one for the action and one for a corresponding status message. The dictionary helps to abstract the GPIO pins to their actual functions (like brewing espresso) and make the code more readable.
Maybe an object oriented approach would have looked nicer but in this case (static buttons) there are no real advantages of defining a button class.
The value of each item is the related to a GPIO Pin. The RPi Zero's GPIO Pins are accessible via the LED()-function of the Python-module gpiozero. Each pin can be enabled and disabled via the LED(x).on() and LED(x).off().
My dictionaries look like this:
# Turn machine on/off: blue wires
outputs["onOff"] = LED(26)
message["onOff"] = "Maschine faehrt hoch oder runter!"
# Single espresso: white wires
outputs["espressoX1"] = LED(19)
message["espressoX1"] = "Einfacher Espresso im Bau!"
In concluson, transferring an HTML-button press via Flask to a button on the coffee-machine is as simple as:
@app.route("/", methods=['POST'])
def controlMachine():
# possible actions = dictionary-keys
action = request.form['action']
print("Action: {}".format(action))
# press button
outputs[str(action)].on()
# hold button for 200ms
sleep(0.2)
# release button
outputs[str(action)].off()
return str(message[str(action)])