ConsentiumThings Arduino API with OTA Updates

Overview

This code demonstrates how to set up the ConsentiumThings Dalton IoT board to collect sensor data, send it via REST API, and check for Over-The-Air (OTA) firmware updates periodically. The code is designed to work with ESP8266, ESP32, and Raspberry Pi Pico W boards and includes provisions for connecting analog sensors and using GPIO pins for REST event indication.

Dependencies

The code depends on the ConsentiumThings.h library, which provides functionality for:

  • Wi-Fi connection

  • Sending and receiving data via RESTful APIs

  • Over-The-Air (OTA) firmware updates

Components

1. Wi-Fi Configuration

  • const char *ssid = "YOUR_WIFI_SSID";: Specifies the Wi-Fi network to which the IoT board will connect.

  • const char *pass = "YOUR_WIFI_PASSWORD";: Defines the password for the specified Wi-Fi network.

2. API Keys

  • const char *SendApiKey = "YOUR_API_KEY";: API key for sending data via the REST API.

  • const char *ReceiveApiKey = "YOUR_API_KEY";: API key used for OTA updates.

  • const char *BoardApiKey = "YOUR_BOARD_API_KEY";: API key specific to the board for authentication.

3. Firmware Version

  • #define FIRMWARE_VERSION "0.0";: Specifies the current version of the firmware running on the IoT board.

4. Data Transmission Interval

  • constexpr int interval = 7000;: Defines the delay interval between each data transmission in milliseconds (7 seconds).

5. Update Interval

  • const int updateInterval = 100;: The board checks for OTA firmware updates after every 100 cycles of the main loop.

6. Board Initialization

  • ConsentiumThingsDalton board(FIRMWARE_VERSION);: Creates an instance of the ConsentiumThingsDalton object, specifying the firmware version.

Functions

void setup()

  • This function runs once when the board is powered on or reset.

  • Wi-Fi Initialization:

    • board.initWiFi(ssid, pass);: Connects the board to the specified Wi-Fi network using the provided credentials.

  • Data Transmission Initialization:

    • board.beginSend(SendApiKey, BoardApiKey);: Authenticates the board and prepares it for sending sensor data over REST.

  • OTA Initialization:

    • board.beginOTA(ReceiveApiKey, BoardApiKey);: Enables Over-The-Air updates, allowing the firmware to be updated remotely.

void loop()

  • This function runs repeatedly after setup() and handles the following operations:

  • Sensor Data Reading:

    • double data_0 = board.busRead(0);: Reads analog sensor data from the specified pin (GPIO 34 for ESP32, A0 for ESP8266, and GPIO 26 for Raspberry Pi Pico W).

  • Sensor Data Array:

    • double sensorValues[] = {data_0};: Stores the sensor data (in this case, only one sensor) in an array.

  • Sensor Information Array:

    • const char* sensorInfo[] = {"Temperature"};: Describes the type of sensor data being read.

  • Sensor Count:

    • int sensorCount = sizeof(sensorValues)/sizeof(sensorValues[0]);: Calculates the number of connected sensors based on the size of the data array.

  • Data Transmission:

    • board.sendData(sensorValues, sensorInfo, sensorCount, LOW_PRE);: Sends the sensor data to the server using the REST API with low precision.

  • OTA Firmware Update Check:

    • loopCounter++: Increments the loop counter with each cycle.

    • if (loopCounter >= updateInterval): Checks if the loop counter has reached the update interval (100 cycles).

    • board.checkAndPerformUpdate();: Checks for available OTA firmware updates and performs an update if one is found.

    • loopCounter = 0;: Resets the counter after checking for updates.

  • Delay:

    • delay(interval);: Pauses execution for 7 seconds before running the loop again.

Customization

Wi-Fi Configuration

Replace "YOUR_WIFI_SSID" and "YOUR_WIFI_PASSWORD" with your actual Wi-Fi network credentials:

const char *ssid = "MyWiFiNetwork";
const char *pass = "MyWiFiPassword";

API Keys

You must replace the placeholders "YOUR_API_KEY" and "YOUR_BOARD_API_KEY" with the actual API keys provided by the ConsentiumThings platform. These keys are used for data transmission and OTA updates.

Data Transmission Interval

To modify the delay between data transmissions, adjust the value of the interval constant (in milliseconds):

constexpr int interval = 5000; // Wait for 5 seconds between data transmissions

Firmware Version

If you update your firmware, change the FIRMWARE_VERSION definition:

#define FIRMWARE_VERSION "0.1"; // Updated firmware version

Update Interval

The board checks for firmware updates after every 100 cycles by default. You can change the frequency by adjusting updateInterval:

const int updateInterval = 50; // Check for updates after 50 cycles

LED Indication (Optional)

You can connect an LED to specific GPIO pins to visually indicate REST events as specified in the comment header:

  • GPIO 16 for ESP8266

  • GPIO 23 for ESP32

  • GPIO 25 for Raspberry Pi Pico W

Adding More Sensors

If additional sensors are connected, expand the sensorValues and sensorInfo arrays:

double data_1 = board.busRead(1); // Read from another analog sensor
double sensorValues[] = {data_0, data_1}; // Add both sensor data to array
const char* sensorInfo[] = {"Temperature", "Humidity"}; // Add sensor descriptions

Last updated