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 Communication with Arduino and ESP8266

In the world of IoT (Internet of Things), real-time communication is crucial for various applications, such as home automation, remote monitoring, and instant data logging. Real-time communication allows devices to send and receive data almost instantaneously, which is essential for time-sensitive applications. This article explores how to achieve real-time communication using Arduino and the ESP8266 Wi-Fi module. We will discuss the importance of real-time communication and provide a step-by-step guide to setting up a project that demonstrates this capability.

Project: In this project, we will create a real-time communication system using an Arduino Uno and an ESP8266 Wi-Fi module. The objective is to send sensor data from the Arduino to a web server in real-time and display the data on a web page. This project will help you understand the basics of real-time communication and how to implement it using Arduino and ESP8266.

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. USB Cable for Arduino - 1
  7. 10k Ohm Resistor - 1

Examples:

  1. Setting Up the ESP8266:
#include <SoftwareSerial.h>

// Create a software serial port for the ESP8266
SoftwareSerial esp8266(2, 3); // RX, TX

void setup() {
  Serial.begin(9600); // Initialize the Serial Monitor
  esp8266.begin(115200); // Initialize the ESP8266 baud rate
  sendCommand("AT", 5, "OK"); // Test the connection
  sendCommand("AT+CWMODE=1", 5, "OK"); // Set the ESP8266 to station mode
  sendCommand("AT+CWJAP=\"yourSSID\",\"yourPASSWORD\"", 20, "OK"); // Connect to Wi-Fi
}

void loop() {
  // Main loop
}

void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print("Sending: ");
  Serial.println(command);
  esp8266.println(command);
  long int time = millis();
  while ((time + maxTime) > millis()) {
    while (esp8266.available()) {
      String response = esp8266.readString();
      Serial.print(response);
      if (response.indexOf(readReplay) != -1) {
        Serial.println("Command executed successfully");
        return;
      }
    }
  }
  Serial.println("Timeout");
}
  1. Sending Sensor Data to a Web Server:
#include <SoftwareSerial.h>
#include <DHT.h>

#define DHTPIN 4 // Pin where the DHT11 is connected
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);
SoftwareSerial esp8266(2, 3); // RX, TX

void setup() {
  Serial.begin(9600);
  esp8266.begin(115200);
  dht.begin();
  sendCommand("AT", 5, "OK");
  sendCommand("AT+CWMODE=1", 5, "OK");
  sendCommand("AT+CWJAP=\"yourSSID\",\"yourPASSWORD\"", 20, "OK");
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  String data = "temperature=" + String(t) + "&humidity=" + String(h);
  sendData(data);
  delay(2000);
}

void sendData(String data) {
  sendCommand("AT+CIPSTART=\"TCP\",\"yourserver.com\",80", 10, "OK");
  String httpRequest = "POST /data HTTP/1.1\r\nHost: yourserver.com\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: " + String(data.length()) + "\r\n\r\n" + data;
  sendCommand("AT+CIPSEND=" + String(httpRequest.length()), 5, ">");
  esp8266.print(httpRequest);
  delay(5000);
  sendCommand("AT+CIPCLOSE", 5, "OK");
}

void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print("Sending: ");
  Serial.println(command);
  esp8266.println(command);
  long int time = millis();
  while ((time + maxTime) > millis()) {
    while (esp8266.available()) {
      String response = esp8266.readString();
      Serial.print(response);
      if (response.indexOf(readReplay) != -1) {
        Serial.println("Command executed successfully");
        return;
      }
    }
  }
  Serial.println("Timeout");
}

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.