Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
ThingSpeak is an IoT analytics platform service that allows you to aggregate, visualize, and analyze live data streams in the cloud. By integrating ThingSpeak with Arduino, you can create powerful projects that monitor and control various parameters in real-time. This integration is particularly useful for applications such as environmental monitoring, smart agriculture, and home automation. In this article, we'll walk through the process of setting up ThingSpeak with Arduino, focusing on how to send sensor data to ThingSpeak for real-time monitoring.
Project: In this project, we will create a system that monitors temperature and humidity using a DHT11 sensor connected to an Arduino. The data will be sent to ThingSpeak, where it can be visualized in real-time. The objectives of this project are:
Components List:
Examples:
#include "DHT.h" // Include the DHT library
#define DHTPIN 2 // Define the pin where the data pin of the DHT11 is connected
#define DHTTYPE DHT11 // Define the type of DHT sensor
DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor
void setup() {
Serial.begin(9600); // Start the serial communication
dht.begin(); // Start the DHT sensor
}
void loop() {
delay(2000); // Wait a few seconds between measurements
float humidity = dht.readHumidity(); // Read humidity
float temperature = dht.readTemperature(); // Read temperature
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
}
#include "DHT.h"
#include <WiFi.h>
#include <HTTPClient.h>
#define DHTPIN 2
#define DHTTYPE DHT11
const char* ssid = "your_SSID"; // Replace with your WiFi SSID
const char* password = "your_PASSWORD"; // Replace with your WiFi Password
const char* server = "http://api.thingspeak.com/update";
const char* apiKey = "your_API_KEY"; // Replace with your ThingSpeak API Key
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = server;
url += "?api_key=";
url += apiKey;
url += "&field1=";
url += String(temperature);
url += "&field2=";
url += String(humidity);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Error in WiFi connection");
}
}
This code will read temperature and humidity data from the DHT11 sensor and send it to ThingSpeak every 2 seconds. Ensure you replace "your_SSID"
, "your_PASSWORD"
, and "your_API_KEY"
with your actual WiFi credentials and ThingSpeak API key.