Electronics and programming interspersed at various levels of difficulty. Sorry about some of the images not loading, it's a problem with blogger/blogspot.

Saturday, August 5, 2017

Failed project - WiFi to InfraRed to Coffee

In one of my most-viewed articles, from quite a few years ago, I've teared down a Saeco Talea Coffee machine and noticed a strange protrusion on top. I assumed it was an IrDa transceiver, then thought that maybe it was a coffee cup detector.

It indeed turned out to be an infrared port.

Armed with this knowledge - and some time to kill - I've decided to try and talk to the machine, wirelessly.
While it might sound easy, there are many steps involved: get hold of the service tool application, reverse engineer (RE) it, RE the service dongle, RE the machine protocol, write an IrDa implementation for Arduino, write the web app to serve the page - and coffee.

Step 1 - .NET reverse engineering

Ever since I've discovered JetBrains' DotPeek, my life has been changed. I don't know C#, in fact I barely know C, but I can pretend to be a .NET developer.
After getting hold of the SSC2 application (that's another topic in itself) we can try to see how it can talk to the machine.

The baud rate for talking to the serial tool is 19200, for my machine. Since the baud rate is different for other machines, I could make an educated guess and assume that the serial tool is just a transparent proxy between a USB to serial converter and either IR or RS232.
The serial protocol is 8N1 - 8 bits, no parity, 1 start and 1 stop bit - pretty standard.

Let's see what we can speak:

The program sends the "(MA)\r\n" command on a button press.

Let's see where Button17 is declared.

That button has a text called "BU up", which in slang means "Move brew unit to the upper position".

Similarly, we can find a pretty long list of commands that can be sent.

Step 2 - IrDa protocol

I could just buy an IrDa transceiver, but I decided not to do that as: they are hard to find, expensive and kill all the fun. Instead, I want to talk using an IR led and IR photodiode or -transistor, my case a BPW82 pin photodiode. Pictured below with a 10k resistor, but actually used with the internal ESP8266 pull-up. One less part to worry about.

The IrDa specification has a SIR subset physical layer that allows serial communications very similar to those employed by UART. I assume that's what the machines use.

Unfortunately the documents are now behind a paywall (I assume) and the links are dead. However, Vishay still has a summarized version for the physical layer:

The even more summarized version is this: the SIR protocol is almost identical to UART in terms of bit (baud) structure, with some changes: a "one" bit is represented by a logical low, a "zero" bit is a logical low with a high pulse in the middle. The pulse is 3/16ths bit-wide.

For our 19200 baud rate it means a bit width of 52.08 microseconds, a pulse width of 9.765us and a full byte should take 520.8us.

Arduino implementation

This was a quick hacking session, now how it should be done. I'm writing code for the ESP8266 (Wemos D1 v0.9), but in Arduino, for portability.

I hear a lot of flak about the Arduino dialect, but it allows almost painfree code porting to different platforms. The same code will run mostly unmodified on an AVR, an MSP430, an ESP8266 and on TM4C123.

I chose to use digitalWrite because: it's quick to write, portable and doesn't add a lot of overhead on the ESP8266.

That took just 10 minutes to write and 2 hours to debug. First mistake was that I was writing sendBit(char_ && 1), notice the boolean instead of bit operation.

The sketch was supposed to send "(MA)\n\r".

The other issue you might be seeing is that there are positive pulses where there should be a flat line. It turns out that doing a digitalWrite(pin, 0) on the ESP8266 writes a "1" for around 3us. I assume that's because it writes the entire GPIO register at once, but haven't dug out the source code. Hence my strange code and XXX comment above.

Another issue you might not be seeing is the timing:

The bit width should be 52us but it's 215us instead. Turns out I left a few debug println statements, another beginner mistake.

With those issues corrected, I got some decent timing, which is supposed to be within specifications.

The byte looks ok (~560us), starts with a zero, ends with a one, all the bits are in place (~56us), the pulse width is decent (~10us) but the machine still fails to respond.

If you are implementing this for yourself you can take a shortcut (HACK) and use any pulse duration between 1.41us and 2us and it falls within tolerances on all baud rates until 115k. No need to do the 3/16ths calculation and timing constraints. Also you can place the pulse anywhere inside the bit.

Reading

This part was not tested properly, so not sure if it works ok. Hence, I will only provide a screenshot of the code, as it's ugly and I don't want automated code machine readers in the future to associate me with that.

There are a number of problems with the code above, if/when I get this thing working I will spend more time improving it. Right now the machine is not sending anything back, so I cannot know if this thing will actually work or not.

WiFi connectivity

The plan is to have a web page and a REST Api to talk to the machine, but in the meantime I'm happy to get any data at all. Hence, I've used a Wifi to UART bridge from:

I'm just telnetting from another machine and hope to receive something. The leds are blinking, so it must do something...

Result and conclusions

The green LED lights up whenever data is received over IR. The black blob is the photodiode, the purple thing is the IR LED. The green thing is a cheap power bank. The deep red/black plastic thing is the window to the machine. The yellow things - that's just a pair of clips. The red thing is a wire.

Anyway, I'll probably need access to a Saeco SerKit or maybe someone makes a waveform recording of sending some command from the SSC2 application.