I've been thinking about how to connect the MicroGrid Connection Unit with a smartphone or similar device to read data or write configuration. The Arduino Nano 33 IoT offers both BLE and WiFi connectivity, but from my understanding they cannot be used at the same time unfortunately. Building something that presents a website using WiFi might be simpler, but it also feels a little bit inconvenient to connect to a dedicated WiFi network just to interact with the connection unit. So I decided to evaluate BLE, as I've never done anything with it on an Arduino before.
Updating the WiFiNINA module
The Nano 33 IoT uses a separate module for the WiFi and BLE connectivity, with its own firmware. Some of the guides and tutorials mention that this might be outdated when you receive the Nano 33 IoT, so I went ahead to update it. Thankfully, javagoza already wrote a great blog post about how to do that, which you can find here: VenTTracker #09 - Checking and updating WiFiNINA Firmware. He describes the process on Windows, but it's almost identical on macOS. Now, instead of running on firmware version 1.2.3, we're on the latest release, 1.4.5. On to the BLE part.
ArduinoBLE library
Arduino provides a library to interact with the BLE side of the WiFiNINA module, called "ArduinoBLE", which you can find in the Arduino Library Manager:
It comes with a few examples, like a simple example on how to control the builtin LED of the Nano 33 IoT board via BLE.
To control the Arduino and its LED from my phone, I'm using a dedicated app called "LightBlue", which is available for both Android and iOS. It allows you to connect to a BLE devices, and read and write values to it. Not really convenient, but it still works surprisingly well.
UART via BLE
The other smartphone app I found that looked interesting is "Bluefruit" by Adafruit. They have a wonderful tutorial on how to use it: https://learn.adafruit.com/bluefruit-le-connect
It's really targetted at their line of BLE devices, based on nRF BLE chips, and makes use an nRF specific emulation of UART communication via BLE. Thankfully, there's another library out there to emulate exactly this protocol using the ArduinoBLE libary, called "HardwareBLESerial".
The library works great, but the examples didn't convince me, so I wrote my own:
#include <HardwareBLESerial.h> HardwareBLESerial &bleSerial = HardwareBLESerial::getInstance(); bool connected = false; bool hello_worlded = false; void setup() { if (!bleSerial.beginAndSetupBLE("Nano33IoT UART Test")) { Serial.begin(9600); while (true) { Serial.println("failed to initialize HardwareBLESerial!"); delay(1000); } } } void loop() { // this must be called regularly to perform BLE updates bleSerial.poll(); if (connected) { if (!bleSerial) { Serial.println("BLE central disconnected"); connected = false; } else { while (bleSerial.availableLines() > 0) { Serial.print("Received data: "); char line[128]; bleSerial.readLine(line, 128); Serial.println(line); if (!hello_worlded) { Serial.println("Sending Hello World (once only)"); bleSerial.println("Hello world!"); hello_worlded = true; } } } } else { if (bleSerial) { Serial.println("BLE central connected"); connected = true; hello_worlded = false; } } delay(500); }
Now this is what that would look like in the Bluefruit App, and in the Arduino Serial Monitor:
{gallery} HardwareBLESerial and the Bluefuit App |
---|
Selecting the Nano33 IoT: In the Bluefruit App, you first need to select the device to connect to. I've filtered out the UART capable devices only, so it shows just the Nano33 with my example sketch |
Connecting to the Nano33: Connecting to the Nano33 IoT can take a few moments |
Different options to interact with the Nano33: After connecting to the Nano33 IoT, you can choose different modules to interact with the BLE device. I chose UART here. |
The UART terminal: Now I can send and receive text via the UART terminal. Bold text is messages sent from the smartphone, regular text message received from the Nano33 IoT |
Arduino Serial Monitor: This is what that communication looks like from the Arduino Serial Monitor |
The plotter is nice to visualize any sensor readings. Unfortunately, it doesn't work well if you plan to send other stuff across the serial connection, so I'm not sure it'll be all that useful to me.
There are a number of other apps for BLE around, like the nRF Toolbox, or Blynk, but this was only my first introduction, and I think I can use the UART quite well for now.