Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Real-Time Data Streaming with Arduino and ESP8266

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:

  1. Arduino Uno - 1
  2. ESP8266 Wi-Fi Module - 1
  3. DHT11 Temperature and Humidity Sensor - 1
  4. Breadboard - 1
  5. Jumper Wires - Several
  6. 10k Ohm Resistor - 1 (for pull-up)

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:

  1. Include Libraries: The necessary libraries for the ESP8266 Wi-Fi module and the DHT sensor are included.
  2. Define Constants: The pin for the DHT11 sensor and the type of sensor are defined.
  3. Initialize DHT Sensor: The DHT sensor is initialized with the specified pin and type.
  4. Wi-Fi Credentials: The SSID and password for the Wi-Fi network are defined.
  5. Server Address: The address of the server to which data will be sent is defined.
  6. Setup Function: In the setup function, serial communication is started, the DHT sensor is initialized, and the ESP8266 module is connected to the Wi-Fi network.
  7. Loop Function: In the loop function, the temperature and humidity are read from the DHT11 sensor. If the readings are valid, they are printed to the serial monitor and sent to the server. The loop waits for 10 seconds before repeating.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.