nRF52 – program sketches “Over the Air”

The electronics will be sealed in a water-tight canister. To simplify the debugging process I want to be able to wirelessly update the microcontroller’s firmware using Bluetooth LE from my iphone or laptop.

So I’ve upgraded to the Adafruit nRF52 Feather microcontroller. This board can be programmed using the Arduino IDE (super easy!) and offers users the ability to update the sketches wirelessly. This is referred to as Over the Air (OTA) Device Firmware Update (DFU) mode.

This is a quick tutorial on how I was able to get OTA DFU working. Please note the following warning about OTA DFU from Adafruit’s website, as of March 11, 2018:

Screen Shot 2018-03-11 at 4.08.29 PM

For this tutorial you will need the following items (sorry I’ve only tested usng iphone + mac)

  • Adafruit nRF52 Feather
  • Micro USB programing cable // connect computer to the Feather
  • iPhone // tested with iOS 11.2.6
  • NRF Toolbox app //to push OTA DFU, tested with V4.4.4
  • Adafruit Bluefruit Connect App // optional, tested with V3.3.1
  • iTunes // wireless file transfer zip files to iPhone, tested with V12.7.3.46
  • iphone data transfer cable // connect iPhone to laptop via USB
  • Arduino IDE // set to have verbose output during compilation, tested with V1.8.5

 

Step 0

Download and install the NRF Toolbox app. Optionally, you can also choose to install the Adafruit Bluefruit Connect App. *Note – the Adafruit App also offers OTA DFU, but I was not able to figure this out yet – hopefully in the near future this will work and then you would only need the Adafruit app. Also – I like the Adafruit UI better than the NRF Toolbox when testing UART messages, just a personal preference.*

IMG_0768

Step 1

Setup your Arduino IDE following the instruction on the Adafruit Learning Guide. Then verify everything is working with their included blink.ino sketch in the nRF52 Feather examples.

Step 2

Upload the sketch “_03_TEST_BLE_UART.ino” (shown below) using a wired USB connection to the nRF52 Feather. This sketch will set the Feather into BLE search mode so that you can connect to the device. Once a connection is established you can open a UART connection and receive messages to the iphone.

</pre>
/*********************************************************************
This is an example for our nRF52 based Bluefruit LE modules

Pick one up today in the adafruit shop!

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/

&nbsp;

// BLE UART - TEST
#include 

BLEUart bleuart;

void setup(void)
{
Serial.begin(115200);
Serial.println(F("Adafruit Bluefruit52 Controller App Example"));
Serial.println(F("-------------------------------------------"));

Bluefruit.begin();
Bluefruit.setName("Bluefruit52");

// Configure and start the BLE Uart service
bleuart.begin();

// Set up the advertising packet
setupAdv();

// Start advertising
Bluefruit.Advertising.start();

// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

void setupAdv(void)
{
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();

// Include the BLE UART (AKA 'NUS') 128-bit UUID
Bluefruit.Advertising.addService(bleuart);

// There is no room for 'Name' in the Advertising packet
// Use the optional secondary Scan Response packet for 'Name' instead
Bluefruit.ScanResponse.addName();
}

/**************************************************************************/
/*!
@brief Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
// send message to BLE UART
bleuart.println(" BLE UART TEST - 00");

// flash the onboard red LED
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000);

}
<pre> 

 

Step 3

Open up the Adafruit app and verify that the UART message is correct. It should look something like this:

Adafruit Bluefruit Connect App

Step 4

Setup the Arduino IDE for verbose output.

Screen Shot 2018-03-11 at 3.44.12 PM

Step 5

Modify the message within the sketch to say something new. I updated the end of the message so say “99” and saved it with a new file name “_03_TEST_BLE_UART_II.ino”.

Screen Shot 2018-03-11 at 4.20.42 PM

This is the new sketch that we want to wirelessly send to the nRF52 Feather and have it update over the air. At this point, hit the VERIFY button, but do NOT upload this new sketch to the board. This is how we test to verify the the OTA DFU worked in the later steps. If you accidentally upload this new code to the board, just change the message back to the original before proceeding.

With the verbose output during compilation turned ON, you should see messages at the bottom of the Arduino IDE screen. One of these messages tells us the location of a ZIP file that was created during the compile process – WE NEED THIS ZIP FILE!

Look for the directory location, it is a temporary file – copy and paste the address into the SEARCH toolbar. You may need to delete the zip file name, and just look for the folder.

Screen Shot 2018-03-11 at 4.26.58 PM.png

Once you find the folder that is holding the zip file, it will look something like THIS:

Screen Shot 2018-03-11 at 4.34.08 PM

Copy/paste the zip file to the desktop or your downloads folder.

Step 6

Setup your iphone + NRF Toolbox app to receive wireless file transfers.  The NRF Toolbox app provides instructions.

Open the NRF Toolbox app and click on the “DFU” button, then click on “Select File”, then click on “User Files” and then click on “How to…” for the instructions about syncing iTunes with the iphone for zip file transfer to the app.

With iTunes open and the iphone paired to the laptop, transfer the zip file to the NRF Toolbox app. You will need to use a USB to setup the paired connection, but once it has been setup you can wirelessly transfer the zip files from the laptop to the iphone app. YAY – no more cables!

Screen Shot 2018-03-11 at 4.44.34 PM

Step 7

Now when you open the NRF Toolbox app and go into the DFU mode,  you should see the zip file in your “USER” files. Pair to the nRF52 Feather, select the zip file and hit UPLOAD. You will see a progress bar to verify the upload is happening. If you see a “Connecting” message for more than 5 seconds then something is not correct and you will need to abort the upload. This may even require that you reflash the firmware via the USB cable.

 

 

Once the DFU process is complete, the nRF52 Feather will reboot itself. Open up the UART using the iphone app and check the message to see if it has changed to “99”. It should look like this if everything worked!

IMG_0784

 

YAHOO! Now we can update the code running on the microcontroller wirelessly.

BUT remember, this has the potential to brick your microcontroller so it is not recommended that you do this – at this time. Based on the Adafruit help forum, is sounds like they are working to make this process safe and reliable from within the Adafruit Bluefruit Connect app – so I’ll try to figure that out later.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s