Sending LM35 Sensor Data to Consentium IoT Cloud

This documentation outlines the steps and code required to read data from an LM35 temperature sensor using an ADC pin and send the data to the Consentium IoT cloud using the ConsentiumThings library. The code also enables Over-The-Air (OTA) updates for the firmware.

Dependencies

The code relies on the ConsentiumThings.h library, which facilitates communication with the Consentium IoT platform and enables OTA updates.

Features

  • Connect to a WiFi network for cloud communication.

  • Send sensor data to the Consentium IoT platform.

  • Enable Over-The-Air (OTA) firmware updates.

  • Automatically check for updates after every 100 loops.

Constants

  • Firmware Version: The firmware version of the board is set to 0.1.

  • Interval: The loop runs every 5 seconds (5000 ms).

  • Update Interval: The firmware checks for updates every 100 cycles of the loop.

Code Overview

1. Initialization

The program begins by creating a ConsentiumThingsDalton object to manage communication with the Consentium IoT platform. The initialization requires the firmware version, WiFi credentials, and Consentium IoT API keys.

#define FIRMWARE_VERSION "0.1"

ConsentiumThingsDalton board(FIRMWARE_VERSION);

The following variables are set for WiFi credentials and API keys:

const char *ssid = "YourWiFiSSID";  // Add WiFi SSID
const char *pass = "YourWiFiPassword";  // Add WiFi password

const char *SendApiKey = "YourSendKey"; // Send API key
const char *ReceiveApiKey = "YourReceiveKey"; // Receive API key
const char *BoardApiKey = "YourBoardKey"; // Board API key

2. Setup

The setup() function initializes the WiFi connection, sets up data transmission to the Consentium IoT cloud, and enables OTA updates.

void setup() {
  board.initWiFi(ssid, pass);  // Begin WiFi connection
  board.beginSend(SendApiKey, BoardApiKey);  // Initialize sending data
  board.beginOTA(ReceiveApiKey, BoardApiKey);  // Enable OTA updates
}

3. Main Loop

The loop() function reads the temperature data from the LM35 sensor connected to an ADC pin, converts the ADC value to a voltage, and sends the data to the Consentium IoT platform.

a. Reading Data from LM35

The data from the LM35 sensor is read using the analogRead(ADC_IN) function. The value is multiplied by a constant to convert the ADC value to a corresponding temperature (depending on the calibration).

double data_0 = analogRead(ADC_IN) * 0.322;  // read voltage data

b. Sending Sensor Data

The data is sent to the Consentium cloud using the sendData function. In this example, the data array consists of one temperature sensor, but the setup allows for multiple sensors to be added in the future.

double sensorValues[] = {data_0};  // sensor data array
const char* sensorInfo[] = {"Temperature"};  // sensor information array
int sensorCount = sizeof(sensorValues)/sizeof(sensorValues[0]);  // number of sensors

board.sendData(sensorValues, sensorInfo, sensorCount, LOW_PRE);  // send sensor data

c. OTA Updates

Every 100 cycles, the firmware checks for available updates and performs the update if available.

if (loopCounter >= updateInterval) {
  board.checkAndPerformUpdate();  // Check and perform OTA updates
  loopCounter = 0;  // Reset the loop counter
}

4. Loop Control

The loop repeats every 5000 milliseconds (5 seconds).

delay(interval);

5. Final code

#include <ConsentiumThings.h>

#define FIRMWARE_VERSION "0.1"

ConsentiumThingsDalton board(FIRMWARE_VERSION); // Create ConsentiumThings object with firmware version

const char *ssid = "YourWiFiSSID"; // Add WiFi SSID
const char *pass = "YourWiFiPassword"; // Add WiFi password
constexpr int interval = 5000; // Wait for 5 seconds
const char *SendApiKey = "YourSendKey"; // Send API key
const char *ReceiveApiKey = "YourReceiveKey"; // Receive API key
const char *BoardApiKey = "YourBoardKey"; // Board API key
const int updateInterval = 100; // Check for update after 100 cycles

int loopCounter = 0; // Counter to keep track of loop cycles

void setup() {
  board.initWiFi(ssid, pass); // Begin WiFi connection
  board.beginSend(SendApiKey, BoardApiKey); // Initialize IoT board
  board.beginOTA(ReceiveApiKey, BoardApiKey); // Enable OTA updates
}

void loop(){
  double data_0 = analogRead(ADC_IN) * 0.322;  // read voltage data
  
  double sensorValues[] = {data_0};  // sensor data array
  const char* sensorInfo[] = {"Temperature"}; // sensor info. array
  
  int sensorCount = sizeof(sensorValues)/sizeof(sensorValues[0]); // number of sensors connected 
  
  board.sendData(sensorValues, sensorInfo, sensorCount, LOW_PRE); // send over REST with delay with desired prescision

  loopCounter++;
  if (loopCounter >= updateInterval) {
    board.checkAndPerformUpdate();
    loopCounter = 0; // Reset the counter after checking for an update
  }

  delay(interval);
}

Summary

This program reads temperature data from an LM35 sensor, sends the data to the Consentium IoT cloud, and periodically checks for OTA firmware updates. It provides a robust real-time sensor data monitoring system and easy firmware updates.

Last updated