# 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 firmware updates for Over-The-Air (OTA).

## Dependencies

The code relies on the `ConsentiumThings.h` library, facilitating communication with the Consentium IoT platform and enabling 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 loop cycles.

## Code Overview

### 1. Initialization

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

```cpp
#define FIRMWARE_VERSION "0.1"

ConsentiumThingsDalton board(FIRMWARE_VERSION);
```

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

```cpp
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.

```cpp
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).

```cpp
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.

```cpp
vector <double> sensorValues[] = {data_0};  // sensor data array
const char* sensorInfo[] = {"Temperature"};  // sensor information array
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.

```cpp
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).

```cpp
delay(interval);
```

## 5. Final code

```cpp
#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
  
  vector <double> sensorValues[] = {data_0};  // sensor data array
  const char* sensorInfo[] = {"Temperature"}; // sensor info. array
  
  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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.consentiumiot.com/tutorials/sending-sensor-data/sending-lm35-sensor-data-to-consentium-iot-cloud.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
