This week’s group assignment explores connectivity between two ESP32 boards, first using ESPNow, and then using UDP protocol over wifi.
Setting Up the Board in Arduino IDE
Before we set up the XIAO ESP32C, we have to add a link to it’s JSON file under “File >Preferences > Additional Boards Manager URLs”. The link can be found here in the Software Setup section..
Then, we check boards manager for “esp32” by “Espressif Systems”. Install it. From “Tools > Boards > esp32 > XIAO ESP32C3”, select the board. Now our board should be ready to go.
Connecting to WiFi
XIAO ESP32C comes with built in WiFi capabilites and an antenna. To make use of these, we first have to install the WiFi library in Arduino IDE. Go to Library Manager and search for WiFi, while filtering “Type: Arduino”. Install the latest version of “WiFi by Arduino”.
After this, I ran the example code below. With this code, we make the XIAO connect to a WiFi, and print the IP address in the serial monitor. We have to specifiy the ssid and password of the WiFi in the code.
#include <WiFi.h>
const char* ssid = "Fablab";
const char* password = "Fabricationlab1";
void setup() {
Serial.begin(115200);
delay(10);
//connect to wifi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// put your main code here, to run repeatedly:
}
I got the following error the first time:
Multiple libraries were found for "WiFi.h"
After I looked it up, I found out that this was an expected error and does not affect the functionality of the WiFi.
Creating an Access Point
We can also use the XIAO as an access point. I tried the sample code below, and set the ssid and password of our AP under WiFi.softAP("ssid", "password").
Then, I used my phone to connect to the access point. Since we print the number of host connections to the serial monitor, I was able to see that I had indeed connected to the AP.
Understanding how this works was the most challenging part for me. Basically, we are able to send information to the XIAO via WiFi, if we are connected to the same WiFi point. I followed the tutorial and the code below is the most basic example of how we can do this. Here is a step-by-step explanation of the code as far as I understand it:
Include the WiFi library and connect the XIAO to a WiFi of our choice.
WiFiServer server(80) specifies from which port the webserver will listen to our commands. This is 80 because webservers usually listen on port 80.
I also asked CHATGPT to clarify this line further, because I was not sure what HTTPS did exactly. Here is the response it gave me:
“(80)” is the port number that we want the server to listen on. Port 80 is the default port for HTTP traffic, so this server is most likely intended to serve web pages.
In summary, the line of code creates a WiFi server object that listens for incoming client connections on port 80, which is commonly used for web traffic. This allows the Arduino board to act as a web server and respond to HTTP requests from client devices on the network.
Connect the XIAO to the WiFi network.
With server.begin(), we start the webserver.
Check if anyone is connected to the webserver (client).
Read and print the input that is given in the terminal by the client.
Respond with a message by printing to the terminal & serial monitor.
Very short delay to allow the server to receive the data.
Repeat the loop.
To communicate with the terminal, I just opened GitBASH anywhere and wrote:
#include <WiFi.h>
const char* ssid = "Fablab";
const char* password = "Fabricationlab1";
WiFiServer server(80); //webservers usually listen on port 80
int connectionCounter = 1;
int btn = D6;
void setup() {
pinMode(btn, INPUT_PULLUP);
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin(); //start website
}
void loop() {
// do we have someone connecting?
WiFiClient client = server.available();
if (client) {
Serial.println("Client connected.");
//Reading in the input
if (client.connected()){
String incoming = ""; //creating an empty string
while (client.available()){
char c = client.read();
incoming += c;
}
//Printing the input
Serial.println(incoming);
//Responding with a message
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("Thank you for connecting with us");
//give the browser time to receive the data
delay(1);
client.stop();
Serial.println("Client disconnected");
}
}
}
Sending String Commands
We can also give commands as a “string”, which means a sequence of characters. In other words, a word. To do that, we make use of the code below.
I modified the tutorial code made by Kris, to accomodate for the specific board that I had. With typing specific commands, I can turn on and off individual LEDs, read a button state and a potentiometer state.