Skip to content

14. Networking and communications

In this week I am using the Barduino's touch sensor to send the values to the ESP32 board I have built, through WiFi.

Barduino

In the design of the Barduino, there are capacitive touch pins with capacitive pads on the board. Pins 3, 4, 5, and 6.

Barduino Pinout

The Barduino uses an ESP32-S3-Wroom-1 which has a WiFi antenna embedded.

ESP32

In the board I designed earlier, I am also using an ESP32-S3-Wroom-1 with the WiFi.

ESP32 Pinout

IP Address

To begin connecting them, I first needed to know what the IP Address for my ESP32 is. To do that I connected the board to my laptop and ran the code below on Arduino IDE.

#include <WiFi.h>

const char* ssid = "WiFi Name";
const char* password = "WiFi Password";

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

  // Connect to Wi-Fi
  Serial.println();
  Serial.print("Connecting to WiFi...");
  WiFi.begin(ssid, password);

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

  // Print connection status
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println();
    Serial.println("WiFi connected");
    Serial.print("ESP32 IP address: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println();
    Serial.println("WiFi connection failed");
  }
}

void loop() {
  // Nothing to do in the loop
}

In ssid and password, write in the WiFi name and password. Once you upload it, change the serial port baud to 115200 as it says in the setup.

The IP Address will be printed. Mine was 192, 168, 0, 19

Sending

The Barduino will be sending the touch data and the ESP32 will be receiving and displaying it.

To send the data, I used this code.

#include <WiFi.h>

const char* ssid = "WiFi Name";
const char* password = "WiFi Password";
const IPAddress serverIP(192, 168, 0, 19);

void setup() {
  Serial.begin(9600);
  delay(100);

  Serial.println();
  Serial.println("Connecting to WiFi");

  WiFi.begin(ssid, password);

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

  Serial.println();
  Serial.println("WiFi connected");
}

void loop() {
  int touchValue = analogRead(4); // Read touch sensor value from pin 4

  // Create a TCP client connection to the ESP32 server
  WiFiClient client;
  if (client.connect(serverIP, 80)) {
    Serial.println("Connected to ESP32 server");

    // Send touch sensor value to ESP32
    client.print("GET /data?value=");
    client.print(touchValue);
    client.println(" HTTP/1.1");
    client.println("Host: 192.168.1.100"); // IP address of the ESP32
    client.println("Connection: close");
    client.println();
    delay(100); // Delay for stability

    // Check if the request was successful
    while (client.available()) {
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }

    client.stop(); // Close the connection
  } else {
    Serial.println("Connection to ESP32 server failed");
  }

  delay(1000); // Delay between sending data
}

Receiving

On the receiving end, the code is.

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

const char* ssid = "WiFi Name";
const char* password = "WiFi Password";
WebServer server(80);

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

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("WiFi connected");
  Serial.println("IP address: " + WiFi.localIP().toString());

  // Start the web server
  server.begin();

  // Route for handling the "/data" URL
  server.on("/data", HTTP_GET, []() {
    if (server.hasArg("value")) {
      String value = server.arg("value");
      Serial.println("Received touch sensor value: " + value);
    }
    server.send(200, "text/plain", "Data received successfully");
  });
}

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

Video of Values

RGB LED

Looking at the received values, I edited the code to turn the LED on only when it is above 300.

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

#define LED_PIN    38 // Pin connected to the NeoPixel LED
#define NUM_LEDS   1 // Number of NeoPixel LEDs

const char* ssid = "sercommBB2408";
const char* password = "CH9JKY5XHJDJGK9S";
WebServer server(80);

Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

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

  // Initialize NeoPixel LED
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("WiFi connected");
  Serial.println("IP address: " + WiFi.localIP().toString());

  // Start the web server
  server.begin();

  // Route for handling the "/data" URL
  server.on("/data", HTTP_GET, []() {
    if (server.hasArg("value")) {
      int touchValue = server.arg("value").toInt();
      Serial.println("Received touch sensor value: " + String(touchValue));

      // Map touchValue to RGB color
      int red, green, blue;
      if (touchValue < 300) {
        red = green = blue = 0; // LED stays off
      } else if (touchValue < 600) {
        red = 0;
        green = 255;
        blue = 0; // Green
      } else if (touchValue < 1000) {
        red = 0;
        green = 0;
        blue = 255; // Blue
      } else if (touchValue < 4000) {
        red = 255;
        green = 165;
        blue = 0; // Orange
      } else {
        // If touchValue exceeds 4000, set LED to white or another color
        red = green = blue = 255;
      }

      // Set NeoPixel LED color
      strip.setPixelColor(0, strip.Color(red, green, blue));
      strip.show();
    }
    server.send(200, "text/plain", "Data received successfully");
  });
}

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