Streaming real-time temperature data from an ESP32 to ThingSpeak involves setting up the ESP32 with a temperature sensor and then using the ThingSpeak API to send the data to your ThingSpeak channel. Below is a step-by-step tutorial on how to achieve this:
Â
Â
#include <WiFi.h>
#include <DHT.h>
#include <ThingSpeak.h>
#define WIFI_SSID “your_wifi_ssid”
#define WIFI_PASS “your_wifi_password”
#define DHTPIN 4 // DHT22 data pin
#define DHTTYPE DHT22 // DHT sensor type
DHT dht(DHTPIN, DHTTYPE);
unsigned long previousMillis = 0;
const long interval = 60000; // Send data every 60 seconds
const char *thingSpeakApiKey = “your_thingspeak_api_key”;
const unsigned long channelID = your_channel_id;
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi…”);
}
Serial.println(“Connected to WiFi”);
ThingSpeak.begin(thingSpeakApiKey);
dht.begin();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis – previousMillis >= interval) {
previousMillis = currentMillis;
float temperature = dht.readTemperature();
if (!isnan(temperature)) {
ThingSpeak.writeField(channelID, 1, temperature, thingSpeakApiKey);
Serial.print(“Temperature: “);
Serial.println(temperature);
} else {
Serial.println(“Failed to read temperature from sensor”);
}
}
}
Replace your_wifi_ssid
, your_wifi_password
, your_thingspeak_api_key
, and your_channel_id
with your actual values.
Â
Upload the code to your ESP32 using the Arduino IDE.
Monitor the Serial Monitor in the Arduino IDE to see the temperature readings and the status of the data upload.
Visit your ThingSpeak channel to see the real-time temperature data being plotted.
That’s it! You’ve successfully set up an ESP32 to stream real-time temperature data to ThingSpeak. This tutorial provides a basic example; you can enhance it by adding error handling, more sensor types, and additional fields to your ThingSpeak channel for various data readings.
It will take just few seconds to claim 7% Discount, After Submitting check Email for Coupon code.
Leave a Reply
You must be logged in to post a comment.