Week 13 - Networking and Communications

Group Assignment:

  • Send a message between two projects

  • Group Assignment Link:

  • Click HERE to view Group Assignment Page!
  • ____________


    Research into Communicating with Wireless Boards (ChatGPTv.4o, 19th June,2024)


    1

    How to get 2 Seeed Studio Xiao ESP32-C3 Boards to connect wirelessly

    (- Written by ChatGPT v4.o, 19th June, 2024)

    To get two Seeed Studio Xiao ESP32-C3 boards to communicate with each other using the Arduino IDE, you can use various communication methods such as WiFi, Bluetooth, or Serial communication. One of the simplest and most straightforward methods is using WiFi to create a basic client-server communication setup. Below, I’ll guide you through setting up basic communication using WiFi.

    Prerequisites:

    1. Arduino IDE: Make sure you have the Arduino IDE installed.
    2. ESP32 Core for Arduino: Install the ESP32 board support in the Arduino IDE. Go to `File` -> `Preferences` and add the following URL to the "Additional Board Manager URLs" field: `https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json`.

    Then go to `Tools` -> `Board` -> `Boards Manager`, search for `esp32`, and install it.
    3. Seeed Xiao ESP32-C3: Select the Seeed Xiao ESP32-C3 board from the board list in the Arduino IDE.

    Hardware Setup

    1. Board 1: This will act as the WiFi server.
    2. Board 2: This will act as the WiFi client.

    WiFi Server Code (Board 1)

    1. Open the Arduino IDE.
    2. Select the correct board and port for the first Seeed Studio Xiao ESP32-C3.
    3. Upload the following code to the first board:

    #include < WiFi.h >

    // Replace with your network credentials
    const char* ssid = "your_SSID";
    const char* password = "your_PASSWORD";

    // Set the server port
    WiFiServer server(80);

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

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

    // Start the server
    server.begin();
    Serial.println("Server started");
    Serial.print("IP Address: ");
    Serial.println(WiFi.localIP());
    }

    void loop() {
    WiFiClient client = server.available();
    if (client) {
    Serial.println("Client connected");
    while (client.connected()) {
    if (client.available()) {
    String request = client.readStringUntil('\r');
    Serial.println(request);
    client.flush();

    // Send a response
    client.println("Hello from the server!");
    client.stop();
    Serial.println("Client disconnected");
    }
    }
    }
    }

    WiFi Client Code (Board 2)

    1. Open another instance of the Arduino IDE.
    2. Select the correct board and port for the second Seeed Studio Xiao ESP32-C3.
    3. Upload the following code to the second board:

    #include < WiFi.h >

    // Replace with your network credentials
    const char* ssid = "your_SSID";
    const char* password = "your_PASSWORD";

    // Server IP address and port
    const char* serverIP = "server_IP_here";
    const uint16_t serverPort = 80;

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

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

    // Connect to the server
    Serial.print("Connecting to server at ");
    Serial.println(serverIP);
    WiFiClient client;
    if (client.connect(serverIP, serverPort)) {
    Serial.println("Connected to server");
    client.println("Hello from the client!");

    // Read the response
    String response = client.readStringUntil('\r');
    Serial.println("Response from server: " + response);

    client.stop();
    } else {
    Serial.println("Connection to server failed");
    }
    }

    void loop() {
    // Add any additional logic here if needed
    }

    Explanation:

    1. WiFi Server Code (Board 1):
    - Connects to the specified WiFi network.
    - Starts a WiFi server on port 80.
    - Waits for a client to connect and sends a "Hello from the server!" message.

    2. WiFi Client Code (Board 2):
    - Connects to the specified WiFi network.
    - Connects to the server using its IP address and port.
    - Sends a "Hello from the client!" message to the server and prints the server's response.

    Steps to Follow

    1. Upload the server code to the first Seeed Studio Xiao ESP32-C3 and note the IP address printed in the Serial Monitor.
    2. Modify the client code by replacing `server_IP_here` with the IP address of the server board obtained from the Serial Monitor.
    3. Upload the client code to the second Seeed Studio Xiao ESP32-C3.

    When both boards are running, the client board should connect to the server board, send a message, and receive a response.

    This setup demonstrates basic communication between two Seeed Studio Xiao ESP32-C3 boards using WiFi. Adjust the code as needed for more complex interactions.

    ____________


    Sending a Message Wirelessly between two ESP32-C3 Boards




    I connected the MASTER ESP32-C3 board via USB and opened up Arduino IDE. I copy and pasted the MASTER code seen above, then installed / updated the ‘esp32’ library. Once done updating, I went to Tools -> Boards -> -> and selected ‘XIAO_ESP32C3’.



    Then for Arduino IDE to recognize your board, select Tools -> Port -> COM3 (ESP32 Family Device).
    Note: Your COM Port may be labeled different.

    Note 2: Initially the ‘Port’ option wasn’t available. I fixed this by using another USB->USB-C Cable. Then the option appeared. Now open your Serial Monitor and change the Baud from 9600 -> 115200.



    Before I Uploaded the Code, I needed to add my WiFi SSID & Password to the ‘const char*’ code. IMPORTANT NOTICE: I won’t document the SSID & Password to the code as this would be doxxing the company’s information. If confused about where to get this information - log into any WiFi on your computer, then you can get your network information from ‘Control Panel’ -> ‘Network & Internet Settings -> ‘WiFi Properties’. After the code is Uploaded, you should see the following information in the Serial Monitor.



    Serial Monitor for ‘MASTER BOARD’:
    Connecting to Wi-Fi.. Connected
    Server started
    IP Address: 172.**.*.***

    IP Address hidden to protect information



    Once the first board was connected, I opened up the ‘SERVANT’ Arduino IDE code that I copied and pasted. For this code you need to add the ‘SSID’, ‘Password’ and ‘IP Address’. For the IP address, if you go into your Computer’s Control Panel -> Network & Internet -> Properties, you can find the IP address under ‘IPv4 Address’.

    If you are working on multiple computers through the same WiFi, you may notice multiple IP Addresses you can use. Use the IP Address that is mentioned in the Master Board’s Serial Monitor for the SERVANT board code.

    Now make sure to select the correct Board (XIAO_ESP32C3), Port COM, Baud Frequency (115200), and that the SERVANT contains the correct WiFi information. If everything goes well, after uploading the code to the board (and continuing USB Connection), you should be able to see in the Serial Monitor for each board that the ‘MASTER’ and “SERVANT’ are in communication. MASTER will send a message saying “Hello from the Server!” whilst the SERVANT will say ‘Hello from the Client!”.





    With that, our assignment is complete! Now you have a basis for starting your online/wireless communication between multiple boards to perform the same or separate functions in conjunction with each other.

    References:

    ESP32-C3 Product Details Getting Started with S3 ESP32-C3 ESP-NOW ESP-NOW info Kochi Week 13 Student Example Computer Networks Crash Course Getting started with ESP32-C3


    ____________


    Individual Assignment:

  • Design, build, and connect wired or wireless node(s) with network or bus addresses and local input &/or output device(s)

  • ____________

    My Plan for the Assignment:

  • Use the Seeed Studio Xiao ESP32-C3 which has an antenna for wireless connection.

  • Neil suggested starting with a Wired connection then move on to Wireless once you understand the process. He also said this week’s assignment isn't too complicated as most of the complexities here are “running under the hood”. But be prepared that there will be a lot of libraries to download.

    Need to design a new PCB - preferably with schematics available through Fab Academy. Was able to get the traces and outline design for a ‘hello.button-blink” board. I will just need to mill, solder and connect to the computer to program it. Thankfully I won’t need to Burn a Bootloader as Seeed Studio Xiao’s are pre-installed and ready - all that’s needed is to reset the board.



    My goals are not ambitious as I struggle with electronics & debugging.

    My only goals are to:
    1. Create a board with the ESP32-C3 Microcontroller and a Button.
    2. Have that button turn on a light on a different board through Wi-Fi connection.




    I will hold off on developing anything major until the Saturday Global Open Time where I can get advice from Rico, Adrian and perhaps other students. But at the very least I can do more research on Networking and Communications, catch up with tidying up my previous documentation, and using modsproject to prepare my milling files for Monday.

    For a more detailed look at the specifics of milling,
    go back to my Week 4 Documentation.



    Global Open Time Advice: After making an ESP32 Wireless board, you can use your old Quentorres to connect to it - just resolder & replace the RP2040 with another ESP32 or make a new Quentorres.
    Also ‘connecting with 2 nodes’ means connect your MASTER (ESP32) to the SERVANT (Quentorres) and ANOTHER SERVANT (Quentorres / AtTiny).

    NOTE: As you can probably guess, I don't feel comfortable labelling my boards as 'MASTER' and 'Slave' even though that is the correct terminology. So I will instead be labelling as "MASTER and SERVANT".

    This week was a busy week outside Fab Academy hours & with applying for 3 different grants that may allow funding for my flight to Mexico for Fab Academy graduation - so I'm running a bit late for this assignment.



    On Monday I began to mill an “ESP32-C3 hello.button.board” but it took a while as the speed settings were lower than I wanted. Unfortunately I couldn't do much more than this on Monday.



    On Tuesday I began to mill a second board while I soldered my components onto the board. At the very least I want to prepare the boards so that I have everything I need for when I revisit these assignments later on.



    My goal is to have the button on one board send a signal to another to turn on an LED light - then read the data through the serial monitor. Not 100% sure if this is everything I need for my assignment but it's a start.

    -----

    How to Setup Wireless Communication (ChatGPT v.4o, 19th May 2024)

    Question: Step-by-step guide on how to use a Seeed Studio Xiao ESP32-C3 to connect wirelessly to another ESP32-C3:

    1. Setup Hardware: Connect both ESP32-C3 boards to your computer via USB. Ensure you have the necessary drivers installed and that the boards are recognized by your computer.

    2. Install Required Software: Install the necessary development environment for ESP32-C3, such as the Arduino IDE or PlatformIO, and make sure to install the ESP32-C3 board support package.

    3. Open IDE: Open the Arduino IDE or PlatformIO, depending on your preference.

    4. Set Board and Port: Select the correct board (Seeed Studio Xiao ESP32-C3) from the board manager and choose the corresponding port your board is connected to.

    5. Write the Sketch: Write a sketch that configures one ESP32-C3 as a server and the other as a client. Here's a basic example:
    #include < WiFi.h >

    const char* ssid = "YourNetworkSSID";
    const char* password = "YourNetworkPassword";

    WiFiServer server(80);

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

    // Connect to Wi-Fi 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");

    // Start the server
    server.begin();
    Serial.println("Server started");

    // Print the IP address
    Serial.print("Use this URL to connect: ");
    Serial.print("http://");
    Serial.print(WiFi.localIP());
    Serial.println("/");
    }

    void loop() {
    // Check if a client has connected
    WiFiClient client = server.available();
    if (!client) {
    return;
    }

    // Wait until the client sends some data
    Serial.println("New client");
    while(!client.available()){
    delay(1);
    }

    // Read the first line of the request
    String request = client.readStringUntil('\r');
    Serial.println(request);
    client.flush();

    // Return the response
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("");
    client.println("");
    client.println("");
    client.println("

    Hello from ESP32!

    ");
    client.println("");

    delay(1);
    Serial.println("Client disconnected");
    }
    6. Upload Sketch: Upload the sketch to both ESP32-C3 boards.

    7. Test Connection: Once uploaded, open the serial monitor for both ESP32-C3 boards and note down their IP addresses. Then, use a web browser or any HTTP client to access the IP address of the server ESP32-C3. You should see the message "Hello from ESP32!" displayed in the browser, indicating a successful connection.

    That's it! You've successfully connected two ESP32-C3 boards wirelessly. You can now further develop your project by exchanging data or performing specific tasks between the two boards.
    (- Written by ChatGPT v3.5, 30th April 2024)


    -----

    Important Update & Notice

    I had to leave this assignment behind as I needed to focus on the following week’s assignment. I am now returning to complete this assignment after I have already completed my final project. I felt it necessary to get a 2nd opinion & description from ChatGPT as it had been updated to Version 4.o and may yield useful new information.

    -----

    INDIVIDUAL ASSIGNMENT PROCESS

    For our individual assignment, I need to have 2 boards communicate wirelessly with Inputs and Outputs. For this reason, I chose to Design & Produce a Seeed Studio Xiao ESP32-C3 Board with PushButton & LED components / functionality.



    Before I started working on on the Wireless communication, I embedded some code onto a single ESP32-C3 board to be absolutely certain that the board functions as intended AND that communication with the Serial Monitor can be achieved. Results on which can be seen below in the VIDEO SECTION.



    NOTE 1:: The code shown below was acquired through ChatGPTv.4o on the 28th of June 2024, then tested and adapted for my own boards and assignments. It’s important that you put in your own SSID (Your WiFi Network Name), your WiFi Password, your SERVANT’s IP Address (also known as ‘IPv4 address), and finally put in the correct pinOuts for your input & outputs. For me, my PushButton Input Device was connected to pinOut 21 and my LED Output Device was connected to pinOut 20

    NOTE 2: We need to get the IP address from the ‘SERVANT’s computer. Yet, the IPv4 Address found in your WiFi properties or found using such apps as ‘Fing’ - it’s not always correct. This was the source of my issues where the Serial Monitor would state ‘Connection to Server Failed’. To solve this - I added the code for ‘Identifying IP Address’ (provided below) and that gave me the correct IP Address (found in Serial Monitor) to make the wireless connection. Once the IP Address is obtained, I would suggest running the SERVANT code (provided below) first, so that it can grab a stable connection to the WiFi before uploading the MASTER code (provided below) on a different computer.

    NOTE 3: Remove the spaces between ‘< WiFi.h >’. They were only added to make the code visible through Visual Studio Code.

    Code for Identifying SERVANT IP Address

    #include < WiFi.h >

    // Replace with your network credentials
    const char* ssid = "yourSSID";
    const char* password = "yourPASSWORD";

    void setup() {
    // Start the Serial communication to send messages to the computer
    Serial.begin(115200);
    delay(10);

    // Connect to WiFi network
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    // Initialize WiFi
    WiFi.begin(ssid, password);

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

    // Print local IP address
    Serial.println("");
    Serial.println("WiFi connected.");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    }

    void loop() {
    // nothing here
    }


    MASTER Code for Input/Output Communication

    #include < WiFi.h >

    const char* ssid = "yourSSID"; // Replace with your SSID
    const char* password = "yourPASSWORD"; // Replace with your Wi-Fi password
    const char* host = "yourIPaddress"; // Replace with the IP address of the slave ESP32-C3
    const uint16_t port = 80; // Port number of the server

    const int buttonPin = 21; // GPIO pin connected to the pushbutton
    int buttonState = 0; // Variable for reading the pushbutton status

    WiFiClient client;

    void setup() {
    Serial.begin(115200);
    pinMode(21, INPUT_PULLUP);

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

    void loop() {
    buttonState = digitalRead(21);

    if (buttonState == LOW) {
    if (!client.connected()) {
    if (!client.connect(host, port)) {
    Serial.println("Connection to server failed");
    delay(1000);
    return;
    }
    }
    client.print("1");
    Serial.println("Button pressed, message sent");
    } else {
    if (!client.connected()) {
    if (!client.connect(host, port)) {
    Serial.println("Connection to server failed");
    delay(1000);
    return;
    }
    }
    client.print("0");
    Serial.println("Button released, message sent");
    }
    delay(100);
    }


    SERVANT Code for Input/Output Communication

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

    const char* ssid = "yourSSID"; // Replace with your SSID
    const char* password = "yourPASSWORD"; // Replace with your Wi-Fi password

    const int ledPin = 20; // GPIO pin connected to the LED

    WiFiServer server(80);

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

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

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

    void loop() {
    WiFiClient client = server.available(); // Listen for incoming clients

    if (client) {
    Serial.println("New Client.");
    while (client.connected()) {
    if (client.available()) {
    char c = client.read();
    Serial.print(c);
    if (c == '1') {
    digitalWrite(20, HIGH);
    } else if (c == '0') {
    digitalWrite(20, LOW);
    }
    }
    }
    client.stop();
    Serial.println("Client Disconnected.");
    }
    }


    Assignment Results

    If everything is working correctly once uploaded, you should notice the following
    communication when Pressing down the MASTER PushButton:

  • ‘Button Pressed / Button Released’ feedback from MASTER Serial Monitor…

  • SERVANT Serial Monitor will show a string of ‘0’s showing that the MASTER PushButton has NOT been pressed, then will change to ‘1’s whenever the MASTER PushButton is being pressed…

  • SERVANT LED will turn on / light up whenever the MASTER PushButton is being pressed.

  • With that, we now have a solid foundation for communicating wirelessly between 2 boards using Input and Output devices. Assignment Complete!



    ____________


    Videos of Results!

    Receiving Input Feedback



    Wireless Communication w/ Inputs & Outputs





    ____________


    Conclusion / Reflection

    I was very concerned about this week as the descriptions of how boards communicate wirelessly and all talk about serial codes, ssid's, bus addresses, etc, went cleanly over my head. This week was when I really felt the electronics assignments snowballing into each other. I felt I was running out of time constantly with weekly deadlines and had no time to learn new tools (because of how much documentation I had to clean up and prepare from previous weeks). My instructor was also out for this week and still needed a couple days to finish Molding & Casting documentation. I knew I wouldn't get this assignment done in time, but I wanted to have everything prepared for when I'm ready to go back.

    I chose to produce 2 ESP32-C3 PCBs that will connect wirelessly across Wi-Fi to perform a simple communication test. Milling went smoothly this time around - I felt I had finally mastered the milling process and could overcome complications in my way. It still took some more time than anticipated since our local technician wanted me to set the cutting speed low as to not damage the end mill, however I found it far too slow and took 3 hours to run 3 milling files + 1 mistake.

    Satified with the result of both my boards, I got to soldering the components onto the board and the result was some of my cleanest work so far - but unfortunately i ran out of time to proceed with the wireless connections but would get back to it closer to the final weeks.

    I had definitely overhyped in my head how difficult or problematic this wireless connection setup would be. For the Group Assignment, I managed to get both boards in wireless communication with each other without complication. I'm fascinated at where I could go from here with this interesting process, but I'm happy to lay this assignment down to rest knowing the barebone basics of wireless communication between custom made boards. There's a lot of potential here for interesting projects so I'll be sure to return to this option at some point.

    The Individual Assignment didn't go as swimmingly but got finished easily enough. ChatGPT has been amazing for finding the code you need for the right job, but mainly as a base to adapt from. The main source of trouble was the IP Address for the SERVANT board was incorrect. This was acquired through the WiFi properties and again through a 'find your network' app called 'Fing'. This was the start of several hours of debugging and panicing. But it wasn't until I ran the Arduino IDE code requesting IP information that I finally obtained the correct code. The assignment was completed in no time afterwards. I'm more than happy to let this assignment rest from here.


    ____________


    Week 13 Project Files

    Week 13 - Project Zip Folder