On Saturday 12th and Sunday 13th December 2015 we held a Hack Day, commissioned by Flow Online Accountancy through Bert Agency and aimed at creating an Internet Of Things Pepsi Machine which would be capable of accounting its own transactions for vended cans to the cloud accountancy package Xero
We had had the machine for a few weeks prior to the hack so we were able to play with it and find out just enough information about how it worked that we could get the right equipment and parts for the hack day.
We decided that although there are many ways of doing this, we would go with a super simple approach of using Raspberry Pi GPIO (General Purpose Input – Output) which would ‘bridge’ from the real world inside the machine, to the web service which Xero provides.
Just opening up the machine required specialist skills as it was a second hand eBay job, and we didn’t have the key! Luckily we have our own in-house @safecracker who opened the lock and replaced it with a new one that we had keys for.
Once inside we could check out the internals, helped by the fact there was a full system diagram/schematic posted on the insulation door. All the can-vending mechanical stuff worked from 120VAC (there was a step-down transformer in the base to take our UK 240VAC mains down to 120VAC) and the coin-counter and control logic was running at 5VDC.
We decided that the simplest way to get an output that indicated when a can had been vended, was to use the ‘can-counter’ which is a simple electromechanical counter like an odometer in a car, which goes up by one, regardless of which kind of can has been vended. We knew that the price of a can was 70p, so we needed to send a transaction of 70p to Xero for each can-vend.
Interfacing from the Pepsi machine to a Raspberry Pi
The Raspberry Pi lives in the low voltage world of microelectronics. This creates problems when you want to interact or control a real world environment where you will find multiple different and often much higher voltages. To overcome this issue it is often necessary to translate a higher voltage down to the 5V level that the Pi is comfortable with, this requires building a ‘voltage translator’ circuit.
During our Pepsi machine hack we realised that the Pepsi machine control system was running at 120Vac. With this in mind we had to create a voltage translator to sit between the machine and the Pi to pass the 120Vac ‘vending can’ signal to a 5Vdc GPIO (general purpose input output) pin of the Pi. The circuit below shows the circuit used.
Starting from the left, the 120VAC signal is input to the circuit through the connector. The resistor limits the current to the LED of the optocoupler. The diode protects the LED from negative AC voltage.
A optocoupler (OK1) is made up of a LED and a phototransistor in a single component. The reason a optocoupler is chosen is for its electrical isolation properties – the only signal passing from LED to phototransistor is light, not electricity – this means our Pi has no electrical connection to the 120VAC voltage, and so it is protected from irregularities in the current which might fry the sensitive GPIO inputs!
Now the right hand side of the circuit: the first part is the transistor output of the optocoupler. Each positive cycle of the AC voltage turns on the LED, this light signal is detected by the phototransistor and it also turns on. Once switched on, the phototransistor makes a connection between R2 and 0V, this means when the AC voltage is positive the phototransistor output switches low. To flip this signal, so the output signal switches high once the AC voltage is detected, a second transistor is used.
The last part before the Pi gets hold of the signal is a ‘peak detector’ circuit made up of the diode (D2), capacitor (C1) and resistor (R4). This circuit smooths the pulsed output from the optocoupler so that the output to the Pi is a easy to read steady voltage. For more on how a peak detector works, checkout this short video: https://youtu.be/ic_yEUV7Y3c
Finally we have 3 connections to the Pi, +5V and 0V to power the left side of the circuit and the output signal.
Here’s Tim’s optocoupler board, lovingly mounted inside the Pepsi Machine
Inside the Raspberry Pi
I’ll come clean: I had never done any Raspberry Pi GPIO until the day we did this hack. My task was to get the GPIO readout from the Raspberry Pi and make it computable. Everyone seems to use Python for GPIO but I prefer Ruby, so lucky for me there is a wonderfully neat Ruby Gem called pi_piper which gave me an easy event-driven GPIO API for the Raspberry Pi.
We were using plain Raspbian on a Custard Pi GPIO protection shield.
Reading the GPIO pin was very, very simple. Why would it be anything else? We’re using Ruby!
after :pin => 23, :goes => :high do
#code we want executed when GPIO goes high
(our randomly chosen physical pin 16 seemed to correspond with pin 23 in software, I’ve no idea what the logic is behind the numbering of R.Pi GPIO)
To Xero and Beyond
@pacharanero @adam_prescott @Alex_Sumner
Adventures in PHP
The initial plan was to wrap some of the work of interfacing with the Xero API (which requires a self-signed certificate, and auth keys etc) in a PHP webservice, which the Raspberry Pi would just send a web request to, and the PHP would do all of the heavy lifting.
Unfortunately we had not factored in the devastating effects of @adam_prescott and @Alex_Sumner ‘s work night out (they work for Reality Mine, sounds like a party place to be employed 🙂 ) Both members of the PHP team were, erm, ‘unavailable’for the second day of the hack – so what now?
Ruby saves the day. (Again).
Turns out there’s a great Ruby Gem called Xeroizer which makes interacting with Xero pretty simple.
It was just a few minutes’ work to get the Ruby GPIO app connected to Xero and to be able to interact with it.
@client = Xeroizer::PrivateApplication.new(ENV['CONSUMER_KEY'], ENV['CONSUMER_SECRET'], ENV['PATH_TO_PRIVATE_KEY'])
But it took quite a while to fully understand Xero’s ‘internal semantics’ (ie how best to represent a transaction like 70p being put into a vending machine) and to find out, by trial and error mainly, what parameters needed to be passed to Xeriozer to get Xero to be happy. There was a little inconsistency in the Xeroizer API here. Some parameters were happy with a string or GUID or DateTime input, others needed to be passed the relevant Ruby Object (like an
Item or a
Contact) and yet others like
LineItem are picked from existing Items by reference to an
@mrbbalmer and @alexhartley both provided excellent moral and code support while we worked through this head-bangingly frustrating bit.
The whole thing. Working!
Here’s the Raspberry Pi
What comes next?
hacker@hackspace~$ ssh pepsi@pepsimachine
Last login: Sat Dec 19 10:13:28 2015
pepsi@pepsimachine:~$ vend fanta
user pepsi does not have permission to access /dev/vending
pepsi@pepsimachine:~$ sudo vend fanta
-o fanta orange
-l fanta lemon
-t fanta fruit twist
pepsi@pepsimachine:~$ sudo vend fanta -t
vending fanta fruit twist
despite all this cleverness you will still need to get up and go over to the machine to actually get your drink
Could we learn how to control the actual vending operations via the Raspberry Pi? It’s a thought, although it would definitely require a different interface into the Pepsi machine’s control system, we wouldn’t be able to use the easy way and just tap into the ‘can vend count’ output, we’d actually have to hack the coin counter board. Or alternatively create another switching system controlling vend operations, running in parallel to the coin counting control system.
Thanks to all who participated in the hack day and all who supported us. Thanks especially to Bert Agency who came up with the idea, and commissioned us to do the hack.