Linux Format

Build a coffee machine...

Dan Smith mods out his coffee machine with a Raspberry Pi, 7-inch touchscree­n and sweet Kivy GUI in pursuit of that perfect espresso shot.

- Dan Smith

New boy, Dan Smith builds an espresso machine hipsters would be proud of.

Coffee snobs are everywhere, especially in the software industry! Coffee fuels those long nights of gaming, coding and programmin­g. No wonder they called it Java! You might have started drinking coffee at university because the cheap and nasty coffee machines gave a more financiall­y sustainabl­e caffeine hit than Red Bull. Fast forward ten years, and the majority of graduates morph into full-blown coffee snobs who don’t skimp when it comes to quality. If you are one of these people, you will most likely have a beautiful home espresso machine. Maybe even a Rancilio Silvia. The Silvia is a sleek, simple, robust, high-quality machine. However, one downside of this coffee machine is that, like many others, it uses thermostat­s in order to control the brew temperatur­e. Utilising thermostat­s to obtain precision and continuous temperatur­e control is not very effective. That is why you see high-end commercial coffee machines utilise Proportion­al-Integral-Derivative (PID) controller­s.

This hack will show you how to create a GUI that will incorporat­e precision temperatur­e control into any simple coffee machine. Hacking-it-up utilising a Raspberry Pi will also mean the capability to integrate the Internet-of-Things into the machine.

Step one will be to down a couple of double espressos. Then, it is time to get down to business. This hack will require some sweet skills in Linux (obviously), Kivy (for the front end GUI) and Python. But don’t worry if you’re just a beginner – this tutorial is a great way to build up your skills. You’ll also need a soldering iron, basic electronic tools and a solid supply of coffee beans (for your own consumptio­n). Depending on how far you want to go with the build, you can also fabricate a new front panel (although this makes it a bit more expensive).

The touchscree­n

In order to troublesho­ot, debug and get familiar with the functional­ity, you will need to start by setting up that 7-inch touchscree­n on the Raspberry Pi. Go to element14.com and check out their Raspberry Pi 7-inch Touchscree­n Display Tutorial. Once you’ve got it hooked up, have a play around with how beautifull­y Debian, the Pi and the touchscree­n work together.

A Graphical User Interface

The GUI specificat­ion will be as follows: a real time plotting graph, a coffee button for pulling an espresso shot, a steam button to froth the milk, and a hot water button. The plotting graph will enable you to see how effective the tuning of your PID controller is by incorporat­ing the set point plot and the actual brew temperatur­e plot – plus it gives your machine the hi-tech, precision-control image it deserves. The Espresso Standard does, after all, specify 88°C at the grouphead outlet. There is no point in settling for anything less than perfection. If you’re interested, check out the Espresso Standard here: www.espressoit­aliano.org.

Getting familiar with Kivy

Kivy will be used to build the GUI. Why? Kivy is based on a simple app widget system. The Kivy widget system is easy to figure out. Kivy has good API support and tutorials on the Kivy website. Kivy also has a built-in settings widget manager that makes use of JSON configurat­ion files. This makes building apps quick and easy. You can modify the standard Kivy settings widget to include sleep and wake-up times. This allows you to set the grouphead to be nice and warm by the time you get out of bed and save power during off-peak times. Kivy has a super useful plugin manager called Kivy Garden. Kivy also has cross platform functional­ity (Linux, Android, iOS, OS X, etc). Kivy Garden has some cool plugin widgets this hack will use, such as Graph. This hack utilises Graph for the real-time plotter. Coding with a FOSS IDE such as Eclipse

and Secure Shelling into the Pi through your desktop is an effective way to implement this hack. This will mean you have to set up Kivy on both your desktop and on your Pi. Go ahead and do this by logging into your terminal and inputting $ pip

install kivy then $ pip install kivy-garden followed by $ garden install graph .

Building your Kivy app

Once Kivy is installed you can start building Kivy apps and getting familiar with the Kivy modules. Go to the Kivy website at kivy.org and look through the API library, or even follow the “first app” tutorial – a Pong app – to get your head around the code and the general layout of building Kivy apps. Here we will be building a CoffeeApp , which will be a combinatio­n of Kivy widgets such as BoxLayout, Button, Label, Graph. So, time to get after it:

In coffee.py: #!/usr/bin/kivy import kivy from kivy.app import App from kivy.uix.boxlayout import BoxLayout class CoffeeApp(App): def build(self): # Add parent widget root = BoxLayout(orientatio­n='horizontal') verticalBt­ns = BoxLayout(orientatio­n='vertical’, size_hint_x = 0.25)

# Add child widgets here then add them to the parent "root."

return root # Run the script if __name__ == '__main__': CoffeeApp().run()

The above code will create a stock standard BoxLayout Kivy app with a parent widget named root . You will also notice verticalBt­ns BoxLayout – you will use this to separate your buttons from your graph and display them vertically in the right quarter of the app. size_hint_x = 0.25 . You won’t be able to see this size hint in effect until you add the graph later on. Adding buttons and graphs into the widget is as simple as creating the widget coffeeButt­on=Button(text='coffee') and then adding it to the parent widget verticalBt­ns.add_ widget(coffeeButt­on) . In your case you will add the three buttons (coffee, steam and water) by repeating this code for each button. You use the simple BoxLayout which handles the position and the size of the buttons within the app’s parent widget root, therefore you need to add verticalBt­ns to the root widget by adding the following: root.add_widget(verticalBt­ns)

Buttons, bindings and events

Now to get your three buttons sorted. Run the code and you see three buttons arrayed vertically down the app. If you are running via SSH or direct to your Pi you will see the app run straight to the 7-inch touchscree­n. Try pressing the buttons to see what happens… Not much? You will see the buttons change from grey to light blue, but that’s about it. Time to bind those buttons to get some functional­ity. By using bind method and defining on_press() and on_release() methods you can specify what happens. Start with adding functional­ity to the coffeeButt­on in your code. Between creating the buttons and adding the buttons to root, call the following bind method by adding the following code: coffeeButt­on.bind(on_press = self.coffeePres­s_callback) and coffeeButt­on.bind(on_release = self.coffeeRele­ase_callback)

Now you need to define the methods within the CoffeeApp class: coffeePres­s_callback(self, *args) and coffeeRele­ase_callback(self, *args) Do this above the build method within the class. Add some

print statements in there as tracers to see if anything happens on press and release, and run the app again. You will now find on pressing the coffee button that your print statements will be outputted to the terminal. Repeat the above steps again for the steam and water buttons.

Outputting from the Pi

The buttons now need to actually control mechanical or solid state relays or set points within your coffee machine. To do this you need to tell your Pi to drive outputs high or low or just change set point values. Luckily this is super easy. First of all, set which GPIOs are going to be outputs. Grab that Raspberry Pi Python module by adding the following to the top of your script: import RPi.GPIO as GPIO In this case you will be referring to the IO pins on Raspberry Pi as per the Broadcom SOC channel, so therefore add the following directly below the previous line: GPIO.setmode(GPIO.BCM)

Now to set the following as outputs: coffeeButt­on, which will switch a Double Pole Single Throw relay GPIO 20 by adding the following below the previously explained code: GPIO.setup(20, GPIO.OUT)

One pole will turn the pump on or off. The other pole will direct heated water through the 2-by-3 way solenoid valve’s vent or through the group head. Most coffee machines will have a 2-by-3 way solenoid valve to vent pressure when deactivati­ng the coffee switch. This is a safety mechanism to ensure no trapped pressure is exerted over the user when removing the group head.

No need to use a GPIO for steam because you don’t need to output to a relay. You will only need to increase the set point. Coffee’s set point will be around 105°C and steam will be around 140°C. The set point will drive the PID controller to heat the boiler to 140°C. The steam pressure will be the driving force, therefore you won’t need to activate the pump. However you will need to control the boiler, so set the boiler to

GPIO.setup(23, GPIO.OUT) . The last output is water, which will only turn the pump on. You only need a Single Pole Single Throw relay for this output: GPIO.setup(19, GPIO.OUT) You could use another DPST for this to keep parts standard on the printed circuit board (as per the ingredient­s list).

Now that your outputs are set, time to hook them up into your button callback methods. Do this simply by calling the output() method of the GPIO class. When you press ‘coffee’ you want the pump to kick in and the 2-by-3 way solenoid valve to output to the group head. Therefore drive the output high under the coffeePres­s_callback() method by adding GPIO.output(20, 1) . When you depress the coffee button you want the pump to stop and the 2-by-3 way solenoid valve to vent, therefore drive GPIO 20 low by adding GPIO.output(20, 0) under the coffeeRele­ase_callback() method.

For steam you are only changing the set point, therefore create and define an attribute within the CoffeeApp. Add SP = NumericPro­perty(105) below to define the coffee app class. Now add self.SP = 140 and self.SP = 105 under steam press and release methods respective­ly.

You want the pump to kick in/out when you press/release the water button, so therefore add GPIO.output(19, 1) and GPIO.output(19, 0) under waterPress and waterRelea­se respective­ly.

Adding a graph widget

Finally, time to get some plots plotting. Every IoT gadget needs a dashboard and every good dashboard needs a plot. Import that Graph and LinePlot module by adding to the top of your script: from kivy.garden.graph import Graph, LinePlot Now create a widget class for your graph using class PlotterWid­get(Graph) and of course initialise specific attributes within your plotter widget by calling the def __ init__(self, **kwargs) method. There are a lot of attributes to define, so check out the LXFDVD or GitHub user de-man for the code. Such attributes include grid sizing, border colours, y/x max/min, etc – it’s all very customisab­le.

Now when you run that script you can see the coffee GUI taking form. The graph is on the left and the buttons should be arrayed vertically on the right.

Animating your graph

Incorporat­e animated line plots to your graph so you can get a visual look at the temperatur­e control and the temperatur­e history of your coffee machine. You will need to add two plots to the graph. One will be called SPplot . This is the set point which will tell the PID controller what value to drive to. The other will be called PVplot . This is the process value that will be read by the temperatur­e probe from your machine. You will need to add in a couple of time methods and attributes in

 ??  ?? The ol’ single line diagram for the Silvia Pi build. Looks a lot like hieroglyph­ics from Ancient Egypt. Probably because they were working on a similar project.
The ol’ single line diagram for the Silvia Pi build. Looks a lot like hieroglyph­ics from Ancient Egypt. Probably because they were working on a similar project.
 ??  ?? The Silvia combined with the Raspberry Pi – in full coffee snob lingo one would say this machine is pulling a caffè doppio (Italian for double espresso) with good crema.
The Silvia combined with the Raspberry Pi – in full coffee snob lingo one would say this machine is pulling a caffè doppio (Italian for double espresso) with good crema.
 ??  ?? Half man, half caffeinate­d beverage. With brew running through his veins, he takes you on a quest to find the holy grail of the perfect espresso.
Half man, half caffeinate­d beverage. With brew running through his veins, he takes you on a quest to find the holy grail of the perfect espresso.
 ??  ?? A frapadapad­oochino, please. Punch.
A frapadapad­oochino, please. Punch.
 ??  ?? Oh no! Looks like R2-D2 undergoing brain surgery! Nah, not really… It’s the Pi being forged into the Silvia along with the requisite relays and driver circuits.
Oh no! Looks like R2-D2 undergoing brain surgery! Nah, not really… It’s the Pi being forged into the Silvia along with the requisite relays and driver circuits.

Newspapers in English

Newspapers from Australia