Simple IOT Project
This project demonstrates how to Publish Sensor readings to ThingSpeak using ESP32 and BME280
Components Required:
Installing the Libraries:
About THINGSPEAK :-
ThingSpeak is an IoT (Internet of Things) analytics platform service that allows users to collect, store, analyze, and visualize data from IoT devices. Developed by MathWorks, it provides a convenient environment to build IoT projects without needing extensive backend development.
Key Features of ThingSpeak
Common Use Cases
ThingSpeak’ s simplicity, combined with its powerful analytics capabilities, makes it a popular choice for beginners and advanced users looking to experiment with IoT projects.
Creating a ThingSpeak Account:
Creating New Channel:
After your account is ready, sign in, open the “Channels” tab and select “My Channels“.
Customizing Chart:
API Key :
ESP32 Publish Sensor Readings to ThingSpeak – Code :
#include <WiFi.h>
#include "ThingSpeak.h"
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
const char* ssid = "REPLACE_WITH_YOUR_SSID"; // your network SSID (name)
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // your network password
WiFiClient client;
unsigned long myChannelNumber = X;
const char * myWriteAPIKey = "XXXXXXXXXXXXXXXX";
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
// Variable to hold temperature readings
float temperatureC;
//uncomment if you want to get temperature in Fahrenheit
//float temperatureF;
// Create a sensor object
Adafruit_BME280 bme; //BME280 connect to ESP32 I2C (GPIO 21 = SDA, GPIO 22 = SCL)
void initBME(){
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
}
void setup() {
Serial.begin(115200); //Initialize serial
initBME();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nConnected.");
}
// Get a new temperature reading
temperatureC = bme.readTemperature();
Serial.print("Temperature (ºC): ");
Serial.println(temperatureC);
//uncomment if you want to get temperature in Fahrenheit
/*temperatureF = 1.8 * bme.readTemperature() + 32;
Serial.print("Temperature (ºF): ");
Serial.println(temperatureF);*/
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
int x = ThingSpeak.writeField(myChannelNumber, 1, temperatureC, myWriteAPIKey);
//uncomment if you want to get temperature in Fahrenheit
//int x = ThingSpeak.writeField(myChannelNumber, 1, temperatureF, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
lastTime = millis();
}
}
HOMEWORK / ASSIGNMENT: -
Refer the below mentioned project blog and try to implement the same functionality hands on
https://iotdesignpro.com/projects/iot-wireless-weather-station-using-arduino-esp8266-thingspeak
Refer these labs for the components: -
IOT LAB IN PG BLOCK , IOT LAB IN EIE Department , WEEKENDS PROJECT LAB (P-015), THINKERING LAB (A-201).