Skip to content

Networking and Communications


๐Ÿ“ก UART Communication (Arduino Uno โ†” ESP8266-12E)


๐Ÿ”น Description

In this part of the project, I implemented UART communication between Arduino Uno and ESP8266-12E.

The goal was:

  • Send data between devices
  • Control LED using serial commands
  • Test communication protocol

๐Ÿ”Œ Hardware Connection

UART Wiring:

  • Arduino TX โ†’ ESP RX (through voltage divider)
  • Arduino RX โ† ESP TX
  • GND โ†” GND

โš ๏ธ Voltage Divider

  • 1kฮฉ + 2kฮฉ resistors used
  • Converts 5V โ†’ 3.3V

๐Ÿ’ก LED Connection (ESP8266)

  • GPIO2 โ†’ resistor (220ฮฉ) โ†’ LED โ†’ GND

๐ŸŽฅ Video Demonstration


๐Ÿ’ป Code


๐Ÿ”น Arduino Code (UART Bridge)

#include <SoftwareSerial.h>

SoftwareSerial esp(2, 3); // RX, TX

void setup() {
  Serial.begin(9600);
  esp.begin(9600);
}

void loop() {
  // PC โ†’ ESP
  if (Serial.available()) {
    esp.write(Serial.read());
  }

  // ESP โ†’ PC
  if (esp.available()) {
    Serial.write(esp.read());
  }
}

๐Ÿ”น ESP8266 Code (LED Control via UART)

#define LED_PIN 2

String command = "";

void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();
    command += c;

    if (c == '\n') {
      command.trim();

      if (command == "ON") {
        digitalWrite(LED_PIN, HIGH);
        Serial.println("LED ON");
      } 
      else if (command == "OFF") {
        digitalWrite(LED_PIN, LOW);
        Serial.println("LED OFF");
      }

      command = "";
    }
  }
}

๐Ÿงช Testing

Steps:

  1. Open Serial Monitor

  2. Set:

  3. Baud rate: 9600

  4. Both NL & CR

  5. Send commands:

ON
OFF

โœ… Result

  • LED turns ON when "ON" is sent
  • LED turns OFF when "OFF" is sent
  • UART communication works correctly

Got it ๐Ÿ‘ โ€” here is the Web Server with LEDs section fully in English, ready for your report:


๐ŸŒ ESP8266 Web Server (LED Control)


๐Ÿ”น Description

In this part of the project, I created a web server using the ESP8266-12E module. The web server allows controlling two LEDs through a web browser using Wi-Fi.

The system uses the HTTP protocol to send commands from the client (browser) to the ESP8266.


๐Ÿ”Œ Hardware Connection

LED Connections:

  • GPIO5 (D1) โ†’ LED โ†’ 220ฮฉ resistor โ†’ GND
  • GPIO4 (D2) โ†’ LED โ†’ 220ฮฉ resistor โ†’ GND

Power Supply:

  • ESP8266 โ†’ 3.3V external power supply
  • GND โ†’ common ground

โš ๏ธ The Arduino 3.3V pin is not sufficient for stable ESP8266 operation.


๐Ÿ“ธ Hardware Setup (Hero Shot)

  • Photo of ESP8266 with LEDs

  • Wiring setup
  • Working LEDs


๐ŸŽฅ Video Demonstration

  • Opening IP address in browser
  • Clicking ON/OFF buttons
  • LEDs turning ON/OFF

๐Ÿ’ป Code (ESP8266 Web Server)

#include <ESP8266WiFi.h>

// Replace with your WiFi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

WiFiServer server(80);

String header;

String output5State = "off";
String output4State = "off";

const int output5 = 5;
const int output4 = 4;

void setup() {
  Serial.begin(115200);

  pinMode(output5, OUTPUT);
  pinMode(output4, OUTPUT);

  digitalWrite(output5, LOW);
  digitalWrite(output4, LOW);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\nWiFi connected");
  Serial.println(WiFi.localIP());

  server.begin();
}

void loop() {
  WiFiClient client = server.available();

  if (client) {
    String currentLine = "";

    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        header += c;

        if (c == '\n') {
          if (currentLine.length() == 0) {

            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            // LED control
            if (header.indexOf("GET /5/on") >= 0) {
              digitalWrite(output5, HIGH);
              output5State = "on";
            } else if (header.indexOf("GET /5/off") >= 0) {
              digitalWrite(output5, LOW);
              output5State = "off";
            }

            if (header.indexOf("GET /4/on") >= 0) {
              digitalWrite(output4, HIGH);
              output4State = "on";
            } else if (header.indexOf("GET /4/off") >= 0) {
              digitalWrite(output4, LOW);
              output4State = "off";
            }

            // HTML page
            client.println("<html><body>");
            client.println("<h1>ESP8266 Web Server</h1>");

            client.println("<p>LED1</p>");
            client.println("<a href=\"/5/on\">ON</a>");
            client.println("<a href=\"/5/off\">OFF</a>");

            client.println("<p>LED2</p>");
            client.println("<a href=\"/4/on\">ON</a>");
            client.println("<a href=\"/4/off\">OFF</a>");

            client.println("</body></html>");

            client.println();
            break;
          } else {
            currentLine = "";
          }
        } else if (c != '\r') {
          currentLine += c;
        }
      }
    }

    header = "";
    client.stop();
  }
}

๐Ÿงช Testing

Steps:

  1. Upload code to ESP8266
  2. Open Serial Monitor (115200 baud)

  3. Copy the IP address

  4. Paste it into a web browser

Example:

http://192.168.1.100

โœ… Result

  • Web page loads successfully
  • Buttons control LEDs
  • LED states update correctly