Skip to content

Week11 | Networking and Communications

Overview

Group Assignment

In the group assignment, we tried to measured power consumption of a OLED using four power meter.

Individual assignment

For the Networking and communication Week assignment requirements:

The goal is to design, build, and connect at least two embedded nodes (node-to-node communication). Each node should have: • its own network or bus address • at least one local input and/or output device • and communicate directly with another node

The connection medium is flexible šŸ‘ — it can be: • wired (UART, I2C, SPI, CAN, etc.) • wireless (WiFi, BLE, ESP-NOW, LoRa, RFID, etc.) • mesh systems (Meshtastic, MeshCore, etc.) • internet-based protocols like MQTT, HTTP, Matter ( only if communication is directly between devices )

Controlled LCD via Wifi and BLE

For this week individual assignment I started with controlling a LCD with my phone thru Wifi and Bluetooth.

Hardwares and software and Application

  • My board that I made during week08.
  • I changed XIAO RP2040 with ESP32-C3
  • USB C cable
  • LCD
  • Arduino IDE
  • BLE Scanner Android App

I did wiring same as I did for output device, week10, and connected my board to my laptop with USB c cable.

I opened the Arduino IDE, started with installing BLE library:

BLE Code:

Wifi code

Connected PCB board to Arduino Uno

In the second setup, the aim was to control a OLED display connected to the Arduino Uno board via wifi with my phone thru the PCB board which I made output device, week10:

My phone --> Wifi --> ESP32-C3 --> Serial UART --> Arduino --> I2C --> OLED.

Connected PCB Board to Arduino Uno

In the second setup, my goal was to control an OLED display connected to an Arduino Uno using my phone via Wi-Fi through a PCB board that I made in the output device, week10.

The communication chain I made:

Phone → WiFi → ESP32-C3 → UART Serial → Arduino Uno → I2C → OLED Display.

In this setup, the ESP32-C3 acts as a Wi-Fi access point and receives messages from the phone. The ESP32-C3 then forwards the message through UART serial communication to the Arduino Uno. Finally, the Arduino processes the received message and displays it on the OLED screen using the I²C interface.

Wiring

GND (ESP32-C3) --> GND(Arduino) TX (pin D6 in ESP32-C3) --> RX (pin 2 in Arduino)

OLED connected to Uno

GND --> GND VCC --> VCC SDA --> SDA SCL --> SCL

Programming

For programming I used Arduino IDE. Both microcontroller needed its own code. For The coding I asked Chatgpt:

ChatGpt prompt:
I want to use esp32-c3 and send a message to arduino uno r3 and display it on oled. one more thing I want to send the message from my phone via wifi to ESP32 and... modify this code:
(I paste the code that I used for the first setup)

ESP32-C3

#include <WiFi.h>
#include <WebServer.h>

WebServer server(80);

const char* apName = "ESP32_Uno";
const char* apPassword = "12345678";

// Hardware UART1 on default pins: TX = D6 (GPIO21), RX = D7 (GPIO20)
#define ESP_TX_PIN D6   // Connect this to Arduino Uno pin 2
#define ESP_RX_PIN D7   // Connect this to Arduino Uno pin 3 (optional for now)

String lastMessage = "";

void sendToUno(String msg) {
  Serial.print("Sending to Arduino: ");
  Serial.println(msg);

  Serial1.println(msg);   // Send with newline
}

void handleRoot() {
  String page = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>ESP32 Message Sender</title>
  <style>
    body{font-family:Arial;margin:30px;}
    input,button{font-size:20px;padding:10px;width:100%;}
    button{margin-top:10px;}
  </style>
</head>
<body>
  <h2>Send message to Arduino OLED</h2>
  <form action="/send">
    <input type="text" name="msg" placeholder="Type message here">
    <button type="submit">Send Message</button>
  </form>
</body>
</html>
)rawliteral";

  server.send(200, "text/html", page);
}

void handleSend() {
  if (server.hasArg("msg")) {
    lastMessage = server.arg("msg");
    sendToUno(lastMessage);

    String response = "<html><body>";
    response += "<h2>Message sent:</h2>";
    response += "<p><strong>" + lastMessage + "</strong></p>";
    response += "<br><a href='/'>← Back to Home</a>";
    response += "</body></html>";

    server.send(200, "text/html", response);
  } else {
    server.send(400, "text/plain", "No message received");
  }
}

void setup() {
  Serial.begin(115200);                    // USB Serial Monitor (debug)

  // Correct initialization for Xiao ESP32-C3
  Serial1.begin(9600, SERIAL_8N1, ESP_RX_PIN, ESP_TX_PIN);

  WiFi.mode(WIFI_AP);
  WiFi.softAP(apName, apPassword);

  IPAddress IP = WiFi.softAPIP();

  Serial.println("\n=== WiFi AP Started ===");
  Serial.print("Connect your phone to: ");
  Serial.println(apName);
  Serial.print("Open browser at: http://");
  Serial.println(IP);

  server.on("/", handleRoot);
  server.on("/send", handleSend);

  server.begin();
  Serial.println("Web server started");
}

void loop() {
  server.handleClient();
}

Arduino Uno

#include <Wire.h>
#include <SoftwareSerial.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

SoftwareSerial espSerial(2, 3); // RX (pin 2), TX (pin 3)

String message = "Waiting...";

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

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("OLED failed");
    while (true);
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);

  display.setCursor(0, 0);
  display.println("System Ready");
  display.setCursor(0, 20);
  display.println("Waiting for message");
  display.display();

  Serial.println("Uno ready - waiting for ESP32...");
}

void loop() {
  if (espSerial.available()) {
    message = espSerial.readStringUntil('\n');
    message.trim();

    Serial.print("Received from ESP32: [");
    Serial.print(message);
    Serial.println("]");

    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("Phone -> WiFi -> ESP32");

    display.setCursor(0, 15);
    display.println("Message received:");

    display.setCursor(0, 30);
    display.println(message);

    display.display();
  }
}

I got this error:

The issue was that the UART serial connection was still connected before programming the microcontroller. It must be disconnected before uploading the code. After successfully programming the board, I reconnected the UART serial port and everything worked correctly.

Then I connected to the ESP32-Uno WiFi:

And opened the address 192.168.4.1 in the browser:

The display was ready to receive a message:

Here is the message received from my phone:

I then sent another message while standing about 10 meters away from the setup: