Skip to content

Dictionary of buttons

misteu edited this page Mar 16, 2018 · 8 revisions

Data strucuture of button requests

My first idea was to define some kind of coffee-machine JSON semantics (see GitHub-Pages Intro). For fast deployment I use another approach right now. Maybe I introduce the JSON semantics as part of a RESTful API.

I created two dictionaries with (I hope) self explaining keys, one for the action and one for a status message. The dictionary helps to abstract the GPIO pins to their actual functions (like brewing espresso) and make the code more readable.

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)])