Skip to content

Week 11 - Networking and Communication

Header

Published on: May 24, 2025


This week is about transmitting signals over a network and allowing multiple devices to communicate with each other. I used my development board I produced for input week to carry out the test as well as a WS2812B LED ring and my laptop.


LED Control via Command Line

First, I connected the NEO Pixel Ring to my dev board and wrote a sketch to control the colors and brightness of the LED ring via command line.

I downloaded and installed the Arduino library to control the Neopixel.

Then I wrote a sketch that accepts the commands as words in the command line and controls the colors and brightness of the ring. I also programmed a rainbow mode and a mode in which the ring rotates.

After this first functional test of the DevBoard in combination with the Neopixel Ring was completed, I then took care of the implementation of the WiFi connectivity.


Controlling the LED Via UDP

My next goal was to be able to control the LED ring via UDP over the WLAN. So I extended my original sketch so that the dev board connects to the WiFi and can receive messages via UDP.

To do this, I had to install and import the WiFi.h and WiFiUDP.h libraries in the Arduino IDE.

Arduino Library Import

In the setup of the program, WiFi.begin(ssid, password) is used to establish a connection. The password and the name of the WiFi are previously created as variables in the code.

WiFi.begin(...) starts the connection process. The while loop checks every 500 ms whether the connection is already established. If not: It waits and continues to check.

void setup() {

  Serial.begin(115200);
  Serial.println("\nConnecting to WiFi...");

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

  Serial.println("\n WiFi connected!");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  udp.begin(localPort);
  Serial.print("UDP listening on port ");
  Serial.println(localPort);
}

Then the local IP is written to the serial monitor and with udp.begin(localPort) the ESP32 opens a local UDP port (here 4210) and is ready to receive incoming UDP packets.

Since UDP is connectionless, unlike TCP, it does not require a connection to be established and therefore no handshake. The packets are simply sent and, if possible, received. The protocol has no control mechanisms to check whether the packets arrive. If this is required, it must be implemented in the program.

In the loop(), udp.parsePacket() is later used to check whether a new UDP packet has arrived and the number of bytes received is saved. Then a char array with the variable name incoming is created, which contains 255 characters and the message is stored in the array with udp.read(incoming, 255). The variable len contains the actual number of characters read and if (len > 0) incoming[len] = '\0'; inserts a null terminator at the last position of the array so that it is a valid C string. With processCommand(String(incoming)); the array is first converted into a string and then passed to the processCommand method.

void processCommand(String input) {
  input.trim();
  input.toLowerCase();

  if (input.startsWith("brightness")) {
    int value = input.substring(10).toInt();
    value = constrain(value, 0, 100);
    currentBrightness = map(value, 0, 100, 0, 255);
    ring.setBrightness(currentBrightness);
    applyColor(lastColor);
    Serial.print("Brightness set to ");
    Serial.println(value);
  }
  else if (input == "on") {
    applyColor(lastColor);
  }
  else if (input == "off") {
    applyColor("off");
  }
  else if (input == "turn") {
    turnClockwise(lastColor);
    applyColor(lastColor);
  }
  else if (input == "rainbow") {
    rainbow(50);
  }
  else {
    applyColor(input);
  }
}

At first I had problems connecting the ESP32 to the WiFi. Without the supplied antenna, the DevBoard could not connect to the WiFi. I initially assumed that the board also had an integrated antenna, like the classic ESP32, and therefore had to search for the error for a while.

However, after connecting the antenna, the devboard connected to the WiFi and I was able to send the commands via UDP.

UDP Connection


Sending Messages with PowerShell via UDP

In order to be able to transmit a corresponding UDP message from the laptop, I first used Windows PowerShell to specify the IP and port of the recipient with a "Send-UDP" function and created a client and endpoint. [string]$message is the variable name of the parameter that is to be sent as a message by the function. The message is then sent with udpClient.Send to the remote endpoint (the ESP32).

function Send-UDP {
    param (
        [string]$ip = "192.168.2.147",
        [int]$port = 4210,
        [string]$message
    )
    $udpClient = New-Object System.Net.Sockets.UdpClient
    $remoteEndpoint = New-Object System.Net.IPEndPoint ([System.Net.IPAddress]::Parse($ip), $port)
    $bytes = [System.Text.Encoding]::ASCII.GetBytes($message)
    $udpClient.Send($bytes, $bytes.Length, $remoteEndpoint)
    $udpClient.Close()
}

The function can then be used in PowerShell with Send-UDP. The parameter for the message, the desired string to be transmitted, is transferred with -message.

Send-UDP -message "purple"
Send-UDP -message "brightness 40"
Send-UDP -message "turn"

LED Ring Purple

LED Ring Result


Group Assignment

For the group assignment we sent a message between Julian's and my dev board. You can find the detailed description on the group documentation page.


Downloads