Software apps and online services
Hand tools and fabrication machines
Ever asked Alexa to make you coffee? I did, and got a negative answer…
As my coffee machine broke down this summer and we bought a new one, it was safe for me to start tinkering with it and potentially freaking the machine up, without losing the so important cups of coffee a day.
The machine I have, can make you four types of coffee: regular, cappuccino, macchiato and latte. It has a water and milk reservoir, and you put in coffee pads. It looked easy enough to control, but still not as simple as just a switching relay for power.
This is the plan:
- As I don’t have an Amazon Echo, I use the Raspberry Pi to send voice communication to Alexa. But this can be replaced by anything connected to the Alexa Voice Service (Amazon Echo, Amazon Tap, Invoxia Triby, Pebble Core, Nucleus, EchoSim.io, etc).
- Alexa will (with the help of Lambda and IoT) send commands to the Raspberry Pi.
- The Raspberry Pi will do the interaction with the Coffee Machine to read the status of the machine by listening to the LED selections and Sensors, and change the status of the machine by inserting Button presses.
Got it? Ok, here is how the Coffee Machine works:SAFETY WARNING
A Coffee Machine works on 230V and boils water (for coffee this is around 90 °C). If you are not careful, this can be very dangerous. Always unplug the machine and wait some time to let it cool down before opening it!
Directly connecting the circuits of the Raspberry Pi to the Coffee Machine is not safe, even not if you only connect the grounds. The Coffee Machine PCBs work on 5V and the Raspberry on 3.3V. I’ve used Optocouplers to split the two circuits and let them still talk to each other (see explanation later on).Detailed Bill of Materials:
Before you can start building, you will have to visit your favorite electronica store. And a supermarket for some coffee and milk.
Raspberry Pi (I used a v3)
Philips Senseo Coffee Machine (I used a Latte Select)
For the Alexa Voice Service part:
- Button (Momentary Switch)
- Some LEDs + matching resistors
For the Coffee Machine part:
- 4 breadboards
- 18 Optocouplers (I used 4N35)
- 18 Resistors (11x 1kΩ and 7x 220Ω)
- Lots wires
- Cobbler for connecting the Raspberry Pi to the breadboard
- Fancy IDE cable and modified Cobbler (or Breakout Helper) for connecting the Coffee Machine to the breadboard
For the coffee:
- Senseo coffee pads (I used Dark Roast)
- Milk for the Cappuccino, Macchiato and Latte
- Optional: Sugar
Here the fun starts, breaking open the Coffee Machine! Really, these guys at Philips know how to put a machine together, that you will not get apart afterwards (without reading the service manual first). I needed to use a drill to get the base loose of the machine part. But the result is OK; I found 3 PCBs where I have to make my connections!
The machine offers four choices of coffee, the regular coffee has three strengths, and you can select the number of cups.
Starting with the lid; Both the Choice and Strength have their own set of LEDs and for both there is a button in the lid with an UP and DOWN side. On the inside, you can see the LEDs and buttons, and at the back I soldered my wires to the PCB:
For this project I’ve ignored the 2 LEDs and button in the middle, these are for cleaning processes.
For the base the same principle is used: there are 3 buttons with corresponding LEDs. Middle one is On/Off, left is One cup, right is Two cups.
On the inside, again at the front of the PCB are the buttons and the LEDs, and on the backside the soldered wires:
And one more PCB, this time for the 3 sensors: Lid open/close (switch), Milk reservoir (HAL sensor) and Water level (HAL sensor). From this PCB, I also take the 5V and GROUND connections.
Learning moment: As I’m working with Optocouplers, I can only measure ON/OFF, not an analog signal. So after spending some hours on troubleshooting and using various resisters (and blowing up an optocoupler), I’ve only focused on connecting the Lid sensor for this project.
I mentioned my machine was broken, the water level sensor turned out to be the issue (HAL sensor was not activated anymore by the magnet inside the Water reservoir). As I now have the sensor wire which is at 2.5V for 1 cup and 3.5V for 2 cups, I connected it to 5V to let it think there is always plenty of water.
With a drill I made a hole to lead the wires out of the lid, took a multi tool with a saw to cut some parts out of the lid. And then glued an IDE header from an old PC in and soldered the wires to it:
For the base the same was done, connecting both the base and the sensors.
A funky IDE cable from my old PC is used to get all wires down to the breadboard.
Learning moment: Using an IDE cable to connect to Coffee Machine to the breadboard, sounded very easy. An IDE cable is, like a GPIO cable, a 40 wire ribbon. So I took a cobbler and go, right? Nope… the IDE has interconnected GND wires on other places then the GPIO header has. So I had to match these away. Then there is a twist in the cable that also takes away some connections. And did I mention that they have 1 pin less in the center of the connector? But I managed to get in all 10 LEDs, 7 buttons, 3 sensors, 5V and GND. When I’m soldering all this together on a perma-board later on, I’ll use a Breakout Helper, that saves me the GPIO GND interconnections.Breadboard: Coffee Machine
To connect the Coffee Machine with the Raspberry Pi, I used a breadboard to easily try the connections. But as said in the safety warning, don't mix up connections from the Coffee Machine with the Raspberry Pi. You could do this with using Switching Relays, or Optocouplers. For safety of both circuits, I used Optocouplers.
There are many good explanations on the internet, and to give credits to where they belong to, I used this video to get myself informed: "Using Optocouplers with the Raspberry Pi" by Gaven MacDonald
In short, refer to image:
- IR Diode (left): Anode is connected to PWR, the Cathode through a resistor to GND.
- Phototransistor (right): Collector to the GPIO pin (with an internal pull-up resistor), Emitter is connected to GND.
- When the IR Diode gets power and 'lights up', the Phototransistor closes the circuit.
The concept behind “listening” to the LED
Coffee Machine side:
- Take the solder points where the LED is on 5V, meaning before the resistor, and the GROUND. Luckily they share the same GND, so I only needed to connect 1 wire per LED and have 1 shared one.
- The wire from the LED wire will go to the Optocoupler
- From the Optocoupler the resister will go to the GND.
Raspberry Pi side:
- One pin is connected to GND
- The other is connected to a GPIO pin with an internal resistor.
- When the LED is switched ON, the optocoupler is powered and will start emitting light at the Coffee Machine side, allowing the power at the Raspberry Pi side to flow too.
- As there is now flowing power, this will be recognized as a change on the GPIO side.
- Interrupts will fire an action in my Python script.
The concept behind “pressing” the Buttons
Coffee Machine side:
- Take the solder points where the button is on switching side and the 5V. Again they share the same 5V, so I only needed to connect 1 wire per button and have 1 shared one.
- The switching wire will go to the Optocoupler.
- From the Optocoupler the other wire will go to 5V.
Raspberry Pi side:
- One pin is connected to GND with a resistor.
- The other is connected to a GPIO pin.
- My Python script will set the GPIO to HIGH for 0.2 seconds and returns LOW after that.
- As there is flowing power now, the optocoupler will start emitting light at the Raspberry Pi side, allowing the power at the Coffee Machine side to flow too.
- For the Coffee Machine, this is recognized as a button press.
On the breadboard you can very clearly see where both circuits are split:
Breadboard: Alexa Pi
For Alexa Pi, I need 1 momentary switch (or button), 3 LEDs.
The button is to activate the microphone. The red LED to show that the microphone is recording. The blue LED is showing that the recording is uploaded to Alexa and that we are waiting for a response. When the received audio is played back, the green LED shows.
Once the project reached final stage, I did move these from the breadboard into the Raspberry Pi case.Final hardware
For some more safety, I've put the breadboards inside a box. Coffee is made with water, and water + electricity doesn't play very well together...
This is the complete end result:
First let’s look at the concept of all interactions (also see the attached schematic):
Reading from left to right:
Raspberry Pi has a speaker, microphone, a button and some LEDs. This is to meet the requirements for using Alexa Voice Services (voice activation is only allowed on the Echo, so I need to use a button). As python program to handle this, I used the AlexaPi implementation.
Ok, command recorded. An audio file is now uploaded to Amazon Alexa Voice Service.
AVS does a Speech to Text and does push that into an Amazon Alexa Skills Kit.
ASK does recognize the commands and pushes that to AWS Lambda, based on Intents setup I did on ASK.
AWS Lambda is a programming platform where I’ve setup a python script to process the intents received. It does read the AWS IoT data from my Thing, and also uploads new data to my Thing. AWS IoT is the data broker that sends out “Delta” messages to subscribers.
The Raspberry Pi is subscribed to my Thing and thus receives the Delta messages. It does talk through GPIO with the Coffee Machine to find out the status, and applies the Delta messages as commands to the Coffee Machine.
For communicating with the AWS IoT, I used the python libraries Amazon provides on Github on both the Raspberry Pi and AWS Lambda. Reason I used the library on the Lambda implementation for consistency, and because I didn’t get boto3 to work.
That means that there are 3 programs involved:
- Alexa Voice Services and Alexa Skills Kit, connecting with AWS Lambda and AWS IoT
- Alexa Pi (modified version) on the Raspberry Pi
- Coffee Machine processing on the Raspberry Pi
To get these all working, I did read lots of forums and tried many examples, but this really took me some time to get it working.
To help you not fall off the same obstacles, here are my learning moments:
- The Amazon Root CA certificate is well hidden. You can find it here: AWS IoT Device SDK for Python (and included in my complete source file).
- In Lambda I could not get boto3 working as that does not require a certificate and IoT does. So I ended up with including the AWSIoTPythonSDK with my project, also good for consistency between the Raspberry Pi and Lambda. As I included a module, I have to upload the complete project as a ZIP-file now.
- When you connect multiple programs to the same IoT Thing to read DELTA and write REPORTED and DESIRED, different AWSIoTMQTTShadowClient IDs are needed (all examples I found were within 1 program using 1 ID, making me ending up in disconnects and queues)
We start with setting up the Amazon part that does exist of several services that talk to each other, and here you generate logins and certificates you will need later on. You will need an account for Amazon Developer Services and Amazon Web Services.
1) Alexa Voice Service
Go to the Alexa Voice Service website and create yourself a setup like this. The information from the Security Profile is needed in your Alexa Pi setup.
Go to the AWS IoT website and create 3 resources: a Thing, a Policy and a Certificate. This certificate is needed for the Lambda setup and your Raspberry setup.
3) AWS Lambda
Go to the AWS Lambda website and create yourself a function. Insert the certificates in the zip-file and change the **** entries in the python file to reflect your certificates and ARN.
4) Alexa Skills Kit:
Go to the Alexa Skills Kit website and create your Skill with Interaction Intents, Slot Types and Sample Utterances.
I do assume that you have a working Raspberry Pi, including GPIO library and Python. For the development environment, I do use the Adafruit WebIDE, also easy if you are not sure about your libraries, they install all you need. You don't need SPI, it might even be better to disable as we are using all GPIO pins available.
Some basics setup you want to do on your Raspberry Pi, Update the Raspberry Pi to the latest and greatest
sudo apt-get update sudo apt-get update
In my project, I do not register the scripts to run automatically, you will have to manually start both (that makes it easier to stop script, adjust code, start script).
Alexa Pi setup
Then we set up Alexa Pi. I took the Alexa Pi source from both Github resources from Sam Machin (and others) (https://github.com/sammachin/AlexaPi) and the (not yet committed changes) from Mason Stone (https://github.com/maso27/AlexaPi) and made modifications.
As their implementation used functionality to listen to keywords (not allowed for an Alexa AVS implementation) and included online radio functionality, I’ve cut out part of their functionality, getting back to the core. Also I made some enhancements for Voice Activation Detection to detect when I’m actually speaking/responding to Alexa.
Once you press the button, the microphone is activated and the red LED lights up at 20%. Also the Voice Activation Detection is started. It waits for you to talk, and lights up the red LED to 100% and records your voice. Once you stop talking, the LED goes back to 20% and after 3 seconds of silence the recording is stopped. The recorded file is uploaded to AVS and the blue LED lights up. Once the response is received and played back, the green LED lights up. If Alexa does expect a response, the microphone is activated and the red LED lights up, starting this circle over again.
This modified version is attached to this project.
To enable Alexa Pi, install the libraries and run the setup, last step is to open a browser and complete the creation of the Alexa token:
Learning moment: On the Raspberry Pi, the audio does by default NOT go through the 3.5mm jack, but uses AUTO or HDMI, leaving me in an empty silence. So make sure the 3.5mm jack is the default:
sudo amixer cset numid=3 1
In the code, make adjustments to which GPIO pin is used for your button and LEDs. Credentials for Alexa are taken from the creds.py file that was generated in the setup. Once all is installed, start AlexaPi.py. Alexa is now waiting for your voice input!
Coffee Machine setup
Then also on the Raspberry Pi, setup the script for the Coffee Machine.
Install the AWSIoTPythonSDK:
sudo pip install AWSIoTPythonSDK
Upload your files, don't forget to include your 2 AWS IoT certificates!
In the code, make adjustments to include your Certificates and the IoT shadow to poll.
Once all is installed, start CoffeeMachine.py. The program will start polling AWS IoT for commands.Now I'm ready to ask my Coffee Machine to make me something!
You should have already started the two Python scripts on your Raspberry Pi, so go ahead ask Alexa to make you coffee.
If you wish to control my Coffee Machine, this is the Alexa Skill!Changes for Certification
[Edit 2016-09-09] During the certification process I got some questions back and made some changes to improve my skill. As these were important learning moments for me, I want to share them with you:
The skill can now also take your order from the first command line:
- First version was: You: "Alexa, start coffee machine", Alexa: "welcome... what kind of coffee do you want...", You: "Cappuccino please", Alexa "I like cappuccino, did you put in ...."
- New version is: You: "Alexa, ask Coffee Machine to make me Cappuccino", Alexa "Welcome, I like cappuccino, did you put in ...."
Different AWSIoTMQTTShadowClient IDs
My skill tended to error out when you did run too many actions in a short timeframe. Remember my comment about different AWSIoTMQTTShadowClient IDs? They were not yet different enough... I've now implemented an even more unique method by including the last digits of the Alexa Request ID into the Lambda ID. Now several users can work at the same time, and do lots of orders within the 5 second timeout :)
Result: skill got certified!