Two more samples. The BME280 enviromental sensor has been implemented as an option both in basic mode (telemetry only) and separately in the full mode (telemetry, messages, methods and twins). This latter version has modualised the telemetry components such that it is a simple matter to modify it for other sensors.

This is the seventeenth post in this series. Is this the final one?

Sample Sketches

The BME280 is a Bosch enviromental sensor that can measure temperature, pressure and humidity. There is additional software (not used here) for calculating such thisngs as altitude and dew point. It can be used in I2C mode or SPI modes. I2C mode is used here. Note that it can have an address of 0x76 or 0x77. 0x77 is used here. This is changed by the /CS pin. Some versions of this from other vendors have less pins. The version here can be used in either mode.

The BME280 Sensor

The Circuit

To setup for these sketches you need the BME280 library:

Sample Telemetry

1752934 RPI Pico (Arduino) Sending telemetry . . . 
Temperature: 20.64�C		Humidity: 62.61% RH		Pressure: 100975.83Pa
{"msgCount":679,"temperature":20.64,"humidity":62.6064,"pressure":100975.8281}
OK
1755698 RPI Pico (Arduino) Sending telemetry . . . 
Temperature: 20.65�C		Humidity: 62.63% RH		Pressure: 100974.90Pa
{"msgCount":680,"temperature":20.65,"humidity":62.6309,"pressure":100974.8984}
OK
1758398 RPI Pico (Arduino) Sending telemetry . . . 
Temperature: 20.64�C		Humidity: 62.63% RH		Pressure: 100974.94Pa
{"msgCount":681,"temperature":20.64,"humidity":62.6279,"pressure":100974.9375}
OK

Serial Terminal output (connected to device)

Wed Mar 01 2023 12:06:41 GMT+1100 (Australian Eastern Daylight Time):
{
  "body": {
    "msgCount": 683,
    "temperature": 20.65,
    "humidity": 62.6289,
    "pressure": 100976.6875
  },
  "enqueuedTime": "Wed Mar 01 2023 12:06:41 GMT+1100 (Australian Eastern Daylight Time)",
  "properties": {}
}
Wed Mar 01 2023 12:06:39 GMT+1100 (Australian Eastern Daylight Time):
{
  "body": {
    "msgCount": 682,
    "temperature": 20.64,
    "humidity": 62.6406,
    "pressure": 100981.1172
  },
  "enqueuedTime": "Wed Mar 01 2023 12:06:39 GMT+1100 (Australian Eastern Daylight Time)",
  "properties": {}
}

In Azure IoT Explorer (i.e. from Hub)

Some C code

In using the ArduinoJson library to generate Json strings for the telemetry, when assigning a float value to a name in a DynamicJsonDocument it was found that spurious extra digits appeared in the Json string if the assigned value was a float. The Troubleshooter on the ArduinoJson says that is needs to be double. This solved that problem. This is an interesting issue. I’d like to give a BIG shout-out for the live help available with the Troubleshooter on this site.

The sensor code returns floats for the sensor values. In solving the problem above some code was implemented to round the float/double value to a specific number of digits. This was done by multiplying the value by a power of ten, rounding that to a long, then dividing by the same power back into a double. Eg using 10000 gives a precision of 4 digits.

Modularisation of Telemetry

To implement for another sensor you need only:

  • Copy the Azure_IoT_Hub_RPI_Pico_BME280WithCDM sketch folder _(renaming folder and .ino file if required)

  • In the iot_configs.h modify the following to name the relevant entities and give an enum for each:

#define TELEMETRY_NAMES "temperature,humidity,pressure"

enum telemetryOrd {otemp,ohum,opres};
  • in the .ino file implement the specific code for the sensor including:

    • Implement hwSetup()
    • In SendTelemetry() modify the line (or leave it out):
  az_iot_message_properties * properties = GetProperties(telemetry[otemp]);

You need to index with an enum to get one telemetry value in that.

  • getTelemetryPayload() No changes!
  • ReadSensor()
    • Implement reading sensor into one or more values at the start of that method.
    • Assign these values to telemetry[] using the matching enums as indexes.

That is all! The rest is auto-magic!

Note that in getTelemetryNames() the csv list of telemetry names is parsed into an array of strings as part of the setup and can then be obtained from telemetryNames[] using the enums as indexes.


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