Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Data streaming is a crucial aspect of modern IoT applications, enabling real-time monitoring and control of various devices. In this article, we will explore how to set up a data streaming system using Arduino and the ESP8266 Wi-Fi module. This setup allows you to send sensor data to a web server in real-time, making it accessible from anywhere with an internet connection. This project is particularly useful for applications like environmental monitoring, smart agriculture, and home automation.
Project: In this project, we will create a real-time data streaming system using an Arduino Uno, an ESP8266 Wi-Fi module, and a DHT11 temperature and humidity sensor. The objective is to read the temperature and humidity data from the DHT11 sensor and send it to a web server using the ESP8266 module. The data will be updated in real-time on a web page, allowing for remote monitoring.
Components List:
Examples:
// Include necessary libraries
#include <ESP8266WiFi.h>
#include <DHT.h>
// Define constants
#define DHTPIN 2 // Pin where the DHT11 is connected
#define DHTTYPE DHT11 // DHT 11
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Wi-Fi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Server address
const char* server = "your_server_address";
// Initialize WiFi client
WiFiClient client;
void setup() {
// Start serial communication
Serial.begin(115200);
// Initialize DHT sensor
dht.begin();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.print(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" connected");
}
void loop() {
// Read temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again)
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print data to serial monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
// Send data to server
if (client.connect(server, 80)) {
String postStr = "humidity=";
postStr += String(h);
postStr += "&temperature=";
postStr += String(t);
postStr += "\r\n";
client.print("POST /data HTTP/1.1\n");
client.print("Host: ");
client.print(server);
client.print("\n");
client.print("Connection: close\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.println("Data sent to server");
}
client.stop();
// Wait 10 seconds before sending next data
delay(10000);
}
Code Explanation: