3

(coco) #1

IoTea: An internet-connected tea machine


TUTORIAL


number, as per the first example. Run the code. It
won’t do anything to begin with, but you can test each
item by typing pump.on(), pump.off() etc.
import gpiozero

pump = gpiozero.LED(3, active_high=False)
valve = gpiozero.LED(26)
kettle = gpiozero.LED(5)

button = gpiozero.button(2)
All this code does is set up each relay using the
gpiozero library, which makes it really easy to control
physical hardware with a Raspberry Pi. We’re using the
LED class for the relays because although they aren’t
LEDs, it allows us to turn them on and off, which is all
they really need to do. It also sets up the button. Make
sure everything works before moving on.
Once you’ve got the relays worked out, the next
step is to write an algorithm for making a cup of tea. It
basically consists of turning on each component of the
system for a length of time, one after the other.

def makeACuppa():
pump.on()
time.sleep(pumpdelay)
pump.off()

kettle.on()
time.sleep(kettledelay)
kettle.off()

valve.on()
time.sleep(valvedelay)
valve.off()
Variables are used here for the amounts of time,
because it makes it easier to change the delay times
later (there’s a fair bit of trial and error involved in that
stage!). When that code’s done, add the line button
when_pressed = makeACuppa to trigger the algorithm
when the button’s pressed. Run the code again and
try it (without any water in the system). This code
will work fine for simply making tea at the press of a
button, but if we want our tea to be cloud-connected
we need to add another block of code.
If This Then That (IFTTT) is a great service for IoT
projects – it ties together all sorts of online services.
However, it’s not too easy to integrate the output of
an IFTTT applet with a Python script without hosting
a full-blown web server, and that’s another level of
complication that this project could do without – so the
following steps use email as a way to get the internet
to talk to the Raspberry Pi. Set up a Gmail account for
your project with a unique email address (unless you
want a cup of tea every time you receive an email).
The code for this part is mostly the same as Adafruit’s
LED Email notifier project (hsmag.cc/jGEDcx), so
that’s a good place to go if you’re looking at making
modifications to this code.
You’ll need to install the IMAPClient library – use
sudo pip install imapclient in the Terminal to do
this. Then you’ll need to modify Adafruit’s code a bit:
remove all the references to RPi.GPIO and LEDs.
Replace the code in the if newmails > NEWMAIL_
OFFSET: section with makeACuppa() and delete the else
statement so it only makes you a cup of tea if a new
email’s detected.
You also need to replace the NEWMAIL_OFFSET variable
with one called prevMails, and assign that to the
number of new mails found every time the loop runs.
This is because you want it to run whenever there’s
a new email since the loop last ran, not whenever
you have any number of unread emails (a subtle but
important distinction!).
Finally, copy and paste the contents of the loop
above the code, so that when you run it, it checks the
number of emails to begin with and assigns that.

TAKING THINGS FURTHER


There are lots of ways this project could be improved. For example, if the water tank is
empty it could lead to the kettle boiling dry which isn’t a good idea – so perhaps a water
sensor could be added to alert you when it’s running out.
Another potential issue is that the system relies on the kettle being full to the bottom
of the valve before operation (any liquid under the valve level won’t be dispensed into
the cup), so maybe a liquid level sensor could be used to ensure the kettle is full enough.
The system as it stands will make a cup of tea whenever it receives an email (which
is open to hacks!), so it could be advantageous to refine the code to only make tea when
emails from a specific address are received.
You could also add a system to remove the teabag from the cup after a period of time.
There’s only one thing worse than no cup of tea, and that’s an over-brewed cup of tea!
It should go without saying that it’s important to make sure all electronics are suitable
for use in 100°C operation – if in doubt, don’t use it!

OPTIONS FOR ENHANCEMENT


Below
The kettle must be
kept filled to the
level just below the
bottom of the valve

The rc.local file
works for auto-
running scripts
on the Raspberry
Pi and is a useful
thing to keep handy.
There are also all
sorts of other ways
to do this including
using .bashrc,
crontab, and init.d.

QUICK TIP

Free download pdf