Porting AnalysIR firmware to Particle’s Photon platform

We have just completed porting our (single source) firmware from a range of other ‘Arduino’ type platforms to Particle’s Photon, having received it yesterday & thought it would be useful sharing some of our experiences for other ‘newbies’. The photon is one of a breed of modern IoT devices hitting the market at relatively low cost. It features a STM32F205 120Mhz ARM Cortex M3 processor running at 120MHz with 1MB flash, 128KB RAM and the all important WiFi. We have been wanting to support the previous Spark Core ($39), but couldn’t resist this little device at the low price point. Particle are also offering a similar embedded device in larger quantities of 10+,  for $12, including FCC certification.

Photon from Particle.io along with embedded sibling
Photon from Particle.io along with embedded sibling

Essentially, our firmware uses D2 & D3 on the Photon to read input from a pair of Infrared receivers to record signals and sends the data to our PC based AnalysIR (Infrared decoder and Analyzer) over serial @ 115,200 bps. We have been wanting to try out the Photon since it was announced last year, for $19+shipping.

The good news is that we have finally got it going after a few ‘road-blocks’ (self-inflicted) and it now seems to perform on a par with the other systems we support. At this time we are only using the cloud to update firmware on the device. Later, we will look at adding in a TCP/IP direct connection over LAN/WiFi, to replace the serial connection.

Photon from Particle.io is really small and comes in a matchbox
Photon from Particle.io is really small and comes in a matchbox

Hopefully some of this post will help others similarly starting out with the Photon.

OTA Flashing
This one resulted in the biggest ‘sink’ of our time. In our ‘loop()’ we always wrapped the actual code inside a while(true) loop to get the best performance, as Arduino does some extra processing at the end of each loop, which we don’t need. I suspect a similar approach on Photon meant that the OTA firmware requests were not being serviced (I presume this happens at the end of each loop by default [?]). As our first workaround, we placed a Spark.process() call every time thru the loop & although this allowed OTA firmware updates to work, we then started missing interrupts (maybe 25%+). The final solution came by doing 20 Spark.process() calls during the first 20 seconds in Setup and then issuing a Spark.disconnect (to turn off the cloud connection), which was enough time to allow an OTA update after reset. There may be more elegant solutions, but this approach is sufficient to meet our needs for now.

Currently, interrupts are not working on some pins on the Photon, but we are OK as we just need 2 – on pins D2 & D3. Our initial mistake here was using 0 & 1 (à la Arduino INT0 & INT1) as the interrupt pins. Once we got some assurance on the forum that interrupts were indeed working, we quickly figured out our mistake and used D2 & D3 as the pins in the attachInterrupt call. A search on the forum will show which pins can be used at present & the plans for a fix for the other pins. The interrupts in our configuration can fire as often as every 17 uSecs when an IR signal is being received and we noticed that when the cloud was connected, the timings recorded were off a bit, occasionally. Now that we disconnect the cloud after the OTA ‘window’ during setup – the timings recorded are ‘spot on’ every time.

General Code & Getting Started
Clearly the Photon is a very different from a standard Arduino or Teensy etc. etc. However, our code ported well to the Photon environment with minimal changes. Most of the issues we encountered were related to OTA & Cloud connections, which is to be expected. I guess a dose of RTFM will always help. There sure seems to be a lot of info available.

The next step is to get a few of our users to test it all out using their Photon and then later to get a direct network connection going to replace the current serial connection.. All in all, we managed to port our code with only a few extra lines of code (for OTA & cloud disconnect) & no material changes to our core Arduino code – nice.

Try it out
Existing users of AnalysIR can try out this new Photon firmware by emailing us. Currently, we have no plans to support the Spark/particle core platform, but the firmware should be similar, for anyone who wants to try it out. Just email us directly, to get a preview of the firmware.