The Pico W BSP for Arduino is available is a repository on GitHub. Alternatively, porting the BSP from the ESP8266 is a simple matter.

Setup and Run Instructions

  1. Run the Arduino IDE.

  2. Install the Azure SDK for Embedded C library.

    • On the Arduino IDE, go to menu Sketch, Include Library, Manage Libraries....

    • Search for and install azure-sdk-for-c.

  3. Install the Arduino PubSubClient library. (PubSubClient is a popular MQTT client for Arduino.)

    • On the Arduino IDE, go to menu Sketch, Include Library, Manage Libraries....

    • Search for PubSubClient (by Nick O’Leary).

    • Hover over the library item on the result list, then click on “Install”.

  4. You may also need to install the Install the Arduino TimeLib library.

    • Search for TimeLib aka Time (by Michael Margiolis).

    • Hover over the library item on the result list, then click on “Install”.

  5. Open the sample.

    • On the Arduino IDE, go to menu File, Examples, azure-sdk-for-c. You would hope to find the PicoW but the esp286 is closest.

The PicoW BSP is not going to be merged into the azure-sdk-forc-arduino repository as it contains some third party code.

  • Download the forked repository djaus2/azure-sdk-for-c-arduino and open the Sketch in this folder, in the IDE. azure-sdk-for-c-arduino\examples\Azure_IoT_Hub_RPI_Pico

    • Alt Open the azure esp8266 example as above and make the changes as (See below).
  1. Configure the sample.

    Enter your Azure IoT Hub and device information into the sample’s iot_configs.h.

  2. Connect the RPI Pico microcontroller to your USB port.

  3. On the Arduino IDE, select the board and port.

    • Go to menu Tools, Board and select Raspberry Pi Pico W.

    • Go to menu Tools, Port and select the port to which the microcontroller is connected.

  4. Upload the sketch.

    • Go to menu Sketch and click on Upload.

Typical Serial Terminal Output

.........WiFi connected, IP address: 192.168.0.14
Setting time using SNTP..........done!
Current time: Fri Dec 30 05:05:48 2022
Client ID: PicoDev137
Username: PicoHub137.azure-devices.net/PicoDev137/?api-version=2020-09-30&DeviceClientType=c%2F1.5.0-beta.1(ard;rpipico)
MQTT connecting ... connected.
13652 RPI Pico (Arduino) Sending telemetry . . . OK
16352 RPI Pico (Arduino) Sending telemetry . . . OK
19021 RPI Pico (Arduino) Sending telemetry . . . OK

===

Monitor the telemetry messages sent to the Azure IoT Hub using the connection string for the policy name iothubowner found under “Shared access policies” on your IoT Hub in the Azure portal.


```
$ az iot hub monitor-events --login <your Azure IoT Hub owner connection string in quotes> --device-id <your device id>
```
Typical output:
```
Starting event monitor, filtering on device: PicoDev137, use ctrl-c to stop...
{
    "event": {
        "origin": "PicoDev137",
        "module": "",
        "interface": "",
        "component": "",
        "payload": "{ \"msgCount\": 1 }"
    }
}
{
    "event": {
        "origin": "PicoDev137",
        "module": "",
        "interface": "",
        "component": "",
        "payload": "{ \"msgCount\": 2 }"
    }
}
{
    "event": {
        "origin": "PicoDev137",
        "module": "",
        "interface": "",
        "component": "",
        "payload": "{ \"msgCount\": 3 }"
    }
}
```

Changes made to the ESP8266 Example when porting to the RPI Pico

Porting from the Azure IoT Hub ESP8266 example to the Azure IoT Hub RPI Pico example only required a few code changes as follows. You can just open the ESP8266 example and make these changes, plus the setup as above.

////////////////////////////////////////////////////////////////
// Change ESP8266 code:
// configTime(-5 * 3600, 0 ,NTP_SERVERS);
//To:
configTime(-5 * 3600, 0, "pool.ntp.org","time.nist.gov"); 
// Got error in Pico Sketch Verifye saying 4 parameters required.
// No such error with ESP8266 though.
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// Change ESP8266 code:
//#include <ESP8266WiFi.h>
//To:
#include <WiFi.h>; 
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// Change ESP8266 code:
// Serial.print(" ESP8266 Sending telemetry . . . ");
//To:
  Serial.print(" RPI Pico (Arduino) Sending telemetry . . . ");
////////////////////////////////////////////////////////////////

In line below:

// When developing for your own Arduino-based platform,
// please follow the format '(ard;<platform>)'. (Modified this)
////////////////////////////////////////////////////////////////
// Change ESP8266 code:
//#define AZURE_SDK_CLIENT_USER_AGENT "c%2F" AZ_SDK_VERSION_STRING "(ard;ESP8266)"
//To:
#define AZURE_SDK_CLIENT_USER_AGENT "c%2F" AZ_SDK_VERSION_STRING "(ard;rpipico)"
////////////////////////////////////////////////////////////////

That is all!

Nb: As noted above, I had to add the timelib library.


 TopicSubtopic
   
 This Category Links 
Category:Pico W AzSDK Index:Pico W AzSDK
  Next: > RPI-Pico-Arduino-AzSDK
<  Prev:   RPI-Pico-Arduino-AzSDK