Sunday 30 August 2015

Using the particle CLI on Ubuntu 14.04

I spent a bit of time playing with the cloud IDE and flashing some simple programs to the photon. I can't believe how easy it is to get up and running. The fact that you can flash your program onto the photon through the IDE without installing a million drivers and fighting with obscure errors is fantastic.

However, I'm not sure if it was my internet being lame or their servers being a bit laggy, but I sometimes found it would take over 10 seconds to compile my programs and would also time out when trying to flash my photon. I found this behaviour a little frustrating so invested a little time in getting the command line interface working so I could compile and flash programs without needing the cloud IDE. I believe the commands do connect to the particle servers so you still need internet for them to work.

So here we go:

1. Install the CLI


I followed the instructions here but found the spark-cli was deprecated and so at the end I installed the particle-cli instead.

swap

> sudo npm -g install spark-cli
for

> sudo npm -g install particle-cli


2. Compile your program


There are several ways you can compile your program into a binary file that can be flashed onto the photon. I chose the method of putting all the required files in a directory and running the following:

> cd <dir-with-program-files>
> particle compile p .

Note: the 'p' indicates you are compiling the program to run on a photon.

As an example, this was what my folder looked like:

- photon-app/
  |
  - myApp.ino
  - lib1.h
  - lib1.cpp

If your compilation was successful then a binary file will get created inside your directory. (It is named something like 'photon_firmware_1440934375581.bin'.

3. Put the photon in DFU mode

The photon needs to be put in Device Firmware Upgrade mode in order to accept a new program.
Perform the following steps:

  1. Ensure the photon is powered and the LED is pulsing cyan to indicate normal operation
  2. Press and hold the 'setup' and 'reset' buttons simultaneously on the photon 
  3. The LED should now be switched off
  4. Release just the 'reset' button and continue to hold the 'setup' button
  5. The LED will start flashing magenta and quickly change to yellow
  6. Once the LED is flashing yellow, release the 'setup' button
The photon is now in DFU mode and we are ready to flash our program onto it.


4. Flash your binary onto the photon


The command to flash a binary onto the photon is

> sudo particle flash --usb <binary-file>

e.g.
> sudo particle flash --usb photon_firmware_1440933735205.bin


Note: this needs to be done with sudo otherwise you get an error that looks like

> Error writing firmware...dfu-util: Cannot open DFU device 2b04:d006

If the flash was successful, the LED on the photon will return to its normal pulsing cyan colour.

I had to install a few bits and pieces to get this stage to work on my Ubuntu 14.04 laptop.

When I first ran the flash command it complained that dfu-util wasn't installed. After installing it with the standard 'sudo apt-get install dfu-util' I got an error along the lines of:

> 'invalid dfuse address: 0x08005000:leave'.

This led me to a helpful forum post which indicated I needed to install a later version of dfu-util as the official ubuntu repo only had version 0.5 and we need 0.7+.

So I downloaded version 0.8 from here (dfu-util_0.8.orig.tar.gz) and followed the instructions on the forum post to build it. In order to successfully run ./autogen.sh I needed to install 'autoconf'. And then in order to successfully run ./configure.sh I needed to install 'libusb-1.0.0-dev'.

After finishing up the dfu-util instructions I was able to successfully flash my program onto the photon. The only slightly annoying thing is that I get an error out of dfu-util despite the successful flash. From what I can tell from other people on the internet this is not unexpected. 

Here is an example output:

jenny@jenny-940X3G:~/code/sht71-photon$ sudo particle flash --usb photon_firmware_1440934375581.bin [sudo] password for jenny: Found DFU device 2b04:d006checking file  photon_firmware_1440934375581.binspawning dfu-util -d 2b04:d006 -a 0 -i 0 -s 0x080A0000:leave -D photon_firmware_1440934375581.bindfu-util 0.8
Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.Copyright 2010-2014 Tormod Volden and Stefan SchmidtThis program is Free Software and has ABSOLUTELY NO WARRANTYPlease report bugs to dfu-util@lists.gnumonks.org
dfu-util: Invalid DFU suffix signaturedfu-util: A valid DFU suffix will be required in a future dfu-util release!!!Opening DFU capable USB device...ID 2b04:d006Run-time device DFU version 011aClaiming USB DFU Interface...Setting Alternate Setting #0 ...Determining device status: state = dfuIDLE, status = 0dfuIDLE, continuingDFU mode device DFU version 011aDevice returned transfer size 4096DfuSe interface name: "Internal Flash   "Downloading to address = 0x080a0000, size = 7756Download [=========================] 100%         7756 bytesDownload done.File downloaded successfullydfu-util: Error during download get_status
Error writing firmware...dfu-util: Invalid DFU suffix signaturedfu-util: A valid DFU suffix will be required in a future dfu-util release!!!
dfu-util: Error during download get_status


2 comments:

  1. Hello..!

    Thank you for writing step by step guideline. It is very important.

    I am also getting same output after following your instruction. But I am not able to see D7 LED blinking :-(.

    I am compiling and downloading blink binary from cloud.

    How can I proceed?

    Please help me in this.
    Thanks,
    Kind Regards,
    Iqram

    ReplyDelete
    Replies
    1. Are you using this code from the official guide? -> https://docs.particle.io/guide/getting-started/examples/photon/#code

      If so then double check:

      1. The binary you are flashing onto the photon is definitely the one from the compilation output

      2. The photon was definitely in the correct mode (flashing yellow) before you flashed the program

      3. Is your photon definitely working ok? Use the particle 'Tinker' app (available on android and iphone) to connect to your photon and manually set pin D7 hi/low to see if the onboard LED lights up. If you need help setting this us check here -> http://codefrieze.blogspot.co.uk/2015/08/move-over-arduino-uno-particle-photon.html

      Delete