Receiving Sensor Data from Consentium IoT Cloud and Controlling LED
This documentation provides the steps and code required to receive data from the Consentium IoT cloud using the ConsentiumThings library. The code checks whether the first data pair (assumed to be temperature data) exceeds a threshold of 30°C and turns on an LED if the condition is met. This is intended for ESP8266, ESP32, or Raspberry Pi Pico W compatible boards.
Dependencies
The code utilizes the ConsentiumThings.h library to handle communication with the Consentium IoT platform. Additionally, it uses GPIO pins to control an LED.
Features
Connect to a WiFi network for cloud communication.
Receive sensor data from the Consentium IoT platform.
Parse and display the received data on the serial monitor.
Control an LED connected to a specific GPIO pin based on sensor data (temperature in this case).
Hardware Setup
LED: Connect the anode (positive leg) of an LED to GPIO 16 on an ESP8266, GPIO 23 on an ESP32, or GPIO 25 on a Raspberry Pi Pico W.
Resistor: Use a 220Ω resistor in series with the LED to limit current.
GPIO Pin Mapping for LED:
ESP8266: GPIO 16
ESP32: GPIO 23
Raspberry Pi Pico W: GPIO 25
Constants
WiFi Credentials: Set your WiFi SSID and password.
Receive Interval: The loop runs every 7 seconds (7000 ms) to retrieve data from the server.
API Keys: Required to authenticate the connection to the Consentium IoT cloud.
Code Overview
1. Initialization
A ConsentiumThings the object is created to manage communication with the Consentium IoT platform.
An LED is connected to the designated GPIO pin (D15 in the example) to indicate whether the temperature exceeds the threshold.
#defineLED_PIN15 // Set the pin to which the LED is connected
2. Setup
The setup() function establishes a WiFi connection, configures the IoT board to receive data, and initializes the LED pin as an output.
voidsetup() {pinMode(LED_PIN, OUTPUT); // Set the LED pin as an outputdigitalWrite(LED_PIN, LOW); // Turn off the LED initiallyboard.initWiFi(ssid, pass); // Begin WiFi connectionboard.beginReceive(ReceiveApiKey, BoardApiKey); // Initialize board to receive data from Consentium IoT cloud}
3. Main Loop
The loop() function receives sensor data from the cloud, displays it on the serial monitor, and checks if the 0th data pair (assumed to be temperature) exceeds 30°C. If so, it turns on the LED connected to D15; otherwise, the LED remains off.
a. Receiving Data
The sensor data is fetched from the Consentium IoT platform using the receiveData() function.
auto dataPairs =board.receiveData(); // Receive sensor data from server
b. Parsing Data
The received data is stored in pairs (value and info), which are iterated through and printed on the serial monitor.
for(size_t i =0; i <dataPairs.size(); i++){double data =dataPairs[i].first; String info =dataPairs[i].second.c_str();Serial.print("Data: ");Serial.print(data);Serial.print(" \t");Serial.print("Info: ");Serial.println(info);}
c. Testing Temperature and Controlling LED
If the temperature (0th data pair) exceeds 30°C, the LED connected to GPIO D15 is turned on.
// Test the 0th data pair (temperature) against 30°Cif (dataPairs.size() >0) {double temperature =dataPairs[0].first; // Assume 0th data pair is temperatureif (temperature >30) {digitalWrite(LED_PIN, HIGH); // Turn on LED if temperature > 30°C } else {digitalWrite(LED_PIN, LOW); // Turn off LED if temperature <= 30°C }}
4. Loop Control
The loop repeats every 7000 milliseconds (7 seconds), allowing for periodic data retrieval.
delay(interval);
Full Code
/*************************************************** This is Consentium's IoT library ----> https://consentiuminc.online/ Check out the links above for our tutorials and product diagrams. This Consentium's IoT library works only for ESP8266/ESP32/Raspberry Pi Pico W compatible Edge boards. Connect a LED to GPIO 16 for ESP 8266, GPIO 23 for ESP 32 and GPIO 25 for Raspberry Pi Pico W to indicate the REST events. Written by Debjyoti Chowdhury for Consentium. MIT license, all text above must be included in any redistribution ****************************************************/#include<ConsentiumThings.h>ConsentiumThings board; // Create ConsentiumThings objectconstchar*ssid ="YOUR_WIFI_SSID"; // Add WiFi SSIDconstchar*pass ="YOUR_WIFI_PASSWORD"; // Add WiFi passwordconstexprint interval =7000; // Wait for 7 secondsconstchar*ReceiveApiKey ="YOUR_API_KEY"; // Receive API keyconstchar*BoardApiKey ="YOUR_BOARD_API_KEY"; // Board API key#defineLED_PIN15 // Set the pin to which the LED is connected (D15)voidsetup() {pinMode(LED_PIN, OUTPUT); // Set the LED pin as an outputdigitalWrite(LED_PIN, LOW); // Turn off the LED initiallyboard.initWiFi(ssid, pass); // Begin WiFi connectionboard.beginReceive(ReceiveApiKey, BoardApiKey); // Initialize board to receive data from Consentium IoT cloud}voidloop() { // Get sensor data from serverauto dataPairs =board.receiveData(); // Print sensor data from serverfor(size_t i =0; i <dataPairs.size(); i++){double data =dataPairs[i].first; String info =dataPairs[i].second.c_str();Serial.print("Data: ");Serial.print(data);Serial.print(" \t");Serial.print("Info: ");Serial.println(info); }Serial.println(); // Test the 0th data pair (temperature) against 30°Cif (dataPairs.size() >0) {double temperature =dataPairs[0].first; // Assume 0th data pair is temperatureif (temperature >30) {digitalWrite(LED_PIN, HIGH); // Turn on LED if temperature > 30°C } else {digitalWrite(LED_PIN, LOW); // Turn off LED if temperature <= 30°C } }delay(interval); // Wait for the next cycle}
Summary
WiFi connection is initialized to receive sensor data.
LED control is based on the temperature threshold.
The system loops every 7 seconds, receiving and processing data.