Two RPi Pico W Arduino sketches were published on GitHub as part of the Softa-ata Repository. They are now separate repositories on GitHub with some refinements.

Repositories

Target Device

The target device for both sketches is a Raspberry Pico Pico W with the earlephilhower RPi Pico W Arduino BSP installed.

Connect2WiFi

This provides various options for connecting to WiFi from the device.

Options

  • Connect using EEProm (simulated) settings
    • Option to write new set if setting found/not found there*.
  • Connect using embedded settings in Header code defines
  • Prompt for settings over Serial
  • Prompt for settings over Bluetooth
  • Call with settings as parameters
  • (Further) Option to use Serial debug messages (which is blocking at connection) or not.
* EEProm Format
  • 256 bytes in flash reserved for this
  • First 4 bytes are a key, (see header), used to detect if settings have been written.
  • Format after the key is |<SSID>|<Password>|<Hostname>|<AzureIoTHubConnectionString>|<Guid>|

Discussion

The primary target of this app was to store the WiFI settings in flash making them non-volatile. Any sketch then running on the device and requiring WiFi can then read and use teh settings. This app can be used to initially write the WiFi settings and also test them after reading them back. Other methods for defining those settings for a sketch are included, including over Bluetooth with user input.

The storage makes use of a simulated EEProm class that actually uses a small part of Flash. See See EEPROM Library.

Note that the entities are stored as one string with the ASCII pipe used as the delimiter between them.

There are currently five stored settings. With the refactored code, it is a simple matter to add more entities at source code level. The current stored settings are:

  • WiFi SSID
  • WiFi Password
  • WiFi Hostname
  • Azure IoT Hub Connection String
  • Guid

The last two could actually be any entity.

The sketch can be run in from_eeprom mode. In that case, if no settings are found in flash, it will prompt to request to input them and to write them to flash. Also if found then the user gets the option to update the settings. When the user inputs new settings they are validated before writing by connecting to the WiFi using those settings. The IoT Hub connection string is not similarly validated. The Guid also is not validated.

The code was restructured to use a Namespace, FlashStorage, so as to facilitate the direct use of the code in another Sketch. Setup code usually only runs once so the tendency might be to use a static C++ class rather than instantiating one. There is some discussion on whether this approach should be used. One recommendation was to use a Namespace instead as was done here.

To reuse this code in another sketch you need only include the header file and the .cpp file in the target sketch, add a reference to the header in source code where it is to be used, and then call WiFiConnectwithOptions() using the namespace, for example:

 // enum ConnectMode: byte {wifi_is_set, from_eeprom, is_defined, serial_prompt, bt_prompt };
  ConnectMode _connectMode = from_eeprom;

  test = FlashStorage::WiFiConnectwithOptions(115200,_connectMode,true,true, _serialDebug)

A call to connect to WiFi using flashed settings using the Namespace

The declaration of this call is:

bool WiFiConnectwithOptions(int baud, ConnectMode connectMode, bool iothub, bool guid, bool debug)
WiFiConnectwithOptions() Parameters
  • baud: Set the USB-Serial BAUD rate if used
    • That depends upon the connection mode and the debug parameter.
  • connectMode: See options in the enum comment above
  • iothub: If true use a connection string
  • guid: If true use a GUID
    • Note: Not auto-generated
  • debug: It true display Serial Debug messages

OTALedFlash

This was discussed in detail in a previous posts:
Softata: Towards OTA Over The Air Sketch Updates).

Summary

See the link just above, and the repository for details.

An example Sketch for OTA (Over-The-Air) updating of a RPi Pico W. Similar to examples at earlephilhower ArduinoOTA/examples especially the first 2. This one uses Inbuilt LED flashes to indicate where the sketch is at.

Note: If device is disconnected to USB Serial then no Serial Debug messages show and hence flashing LeD is useful.

You deploy the sketch via USB-Serial. The sketch, or any other sketch, can can then be uploaded in two ways OTA:

  • After a while after the OTA sketch upload via USB-Serial, a network option becomes available under Tools->Port. Select that and the section deployment will be OTA.
  • In the Arduino IDE, run Sketch->Export Compiled Binary. Set the target device in Uf2 mode by disconnecting the USB cable, press the BootSel button on the RPi Pico W, hold it and reconnect. A drive letter, eg D: will appear as a pop up. File copy the built binary file to that drive.

All good! :)


 TopicSubtopic
  Next: > Softata
<  Prev:   Softata