Pepsi Machine Hack (supported by Bert Agency and Xero)


#1

On Saturday 12th and Sunday 13th December 2015 we held a Hack Day, commissioned by 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.

#The Machine
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
@Tim_Jennings

The RasberryPi 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
@pacharanero

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 R.Pi 2 Model B with a ready-built Custard Pi GPIO protection shield.

Reading the GPIO pin was very, very simple. Why would it be anything else? We’re using Ruby!

require 'pi_piper'

include PiPiper

after :pin => 23, :goes => :high do
  #code we want executed when GPIO goes high
end
      
PiPiper.wait

(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
https://github.com/leigh-hackspace/pepsi-xero/blob/master/rpi-gpio-to-xero.rb
@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 :slight_smile: ) 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 item_code:.

@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
>
Last login: Sat Dec 19 10:13:28 2015
root@pepsi:~$ vend fanta
> insufficient permissions
root@pepsi:~$ sudo vend fanta
> of course - orange [O], lemon [L] or tropical [T]? [O]

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.

At Leigh Hackspace our skilled and varied team are available to work on commissioned projects to create or hack unusual bespoke systems from textiles to poetry to software and hardware. Contact Marcus on 07747600617 for further information.


#3

#4

Great writeup and an awesome project!


#5

Here’s the video that Bert Agency and Flow produced (this is the ‘end product’ thing they were producing) - It’s a promo for Xero showing the capabilities and flexibility of the Xero API.


#6

AWESOME stuff !


#7

Again I should reiterate here (already said it on Facebook) I’m a little embarrassed that I’m the only one on this video - we filmed it about a week ago at Old Hackspace so we wouldn’t need to move the Pepsi Machine.

We still have footage from the Hack Day, and there is apparently a plan to produce a ‘Making Of’ version of this video, so we might be able to get some of the footage from the Hack Day into that.

Thanks to everyone who helped out on the Hack Day @phyushin @adam_prescott @alexhartley @Alex_Sumner @alan_daglish @hannah_crosby @deborahbaw @Luke_Martley @Katie_Louise_Dixon @mrbbalmer and any others I’ve missed.

Bert Agency and Flow Accounting are already planning the next Hack for us to do!
M


#8

Great work guys, love it!