Week 11: Networking and Communication

Overview

This week, I built on Output Week by designing a microcontroller system where a button on one ESP32 board communicates with another ESP32 board to control a linear actuator. The goal is to integrate this system into my final project for a voice-activated or facial-recognition-enabled door lock.

The primary challenge was establishing a stable Wi-Fi connection between the two ESP32 devices. Some issues encountered included weak Wi-Fi signals, incorrect IP configurations, and inconsistent connections. To address these:

Process

Below are the steps I followed:

  1. Designed and tested a new PCB board for the ESP32 microcontroller to ensure proper connections for the button and linear actuator (see schematics below).
  2. Implemented server-client communication using the ESP32's Wi-Fi capabilities.
  3. Tested the system by pressing the button on the client board, which triggered the actuator on the server board.
Switch Controller PCB Switch Controller Schematic

Server Code (ESP32 with Linear Actuator)

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

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

WebServer server(80);
const int actuatorPin = 9;

void handleExtend() {
  digitalWrite(actuatorPin, HIGH);
  server.send(200, "text/plain", "Actuator extended");
  delay(1000);
  digitalWrite(actuatorPin, LOW);
}

void setup() {
  Serial.begin(115200);
  pinMode(actuatorPin, OUTPUT);
  digitalWrite(actuatorPin, LOW);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  server.on("/extend", handleExtend);
  server.begin();
}

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

Client Code (ESP32 with Button and LED)

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* serverIP = "SERVER_IP_ADDRESS";
const int buttonPin = 20;
const int ledPin = 9;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    digitalWrite(ledPin, HIGH);
    delay(100);
    if (WiFi.status() == WL_CONNECTED) {
      HTTPClient http;
      String url = String("http://") + serverIP + "/extend";
      http.begin(url);
      int httpResponseCode = http.GET();
      http.end();
    }
    delay(1000);
    digitalWrite(ledPin, LOW);
  }
}
    

Videos & Final Results

Final Working Setup