Skip to content

Week14. Embedded Networking and Communications

Group assignment:

  • Send a message between two projects
  • Document your work to the group work page and reflect on your individual page what you learned.

Individual assignment

  • design, build and connect wired or wireless node(s) with network or bus addresses and a local interface

Group assignment can be viewed at this link. - week14 Group assignment

Group assignment:

I first learned about the ways of communication through some content. At the same time, I made some comparisons between wireless communication and wired communication and learned some things.

Learn about Wireless Communication:

1. Bluetooth:
- Uses radio signals for short-range communication (up to 30 feet).
- Commonly used for connecting devices like headphones and keyboards to smartphones.
2. WiFi:
- Utilizes a wireless electronic network through routers. - Provides internet access with varying speeds and security concerns in public spaces.
3. Cellular:
- Uses cellular networks for reliable and fast communication, popular in mobile phones.
- Supports technologies like Cat-M1 and NB-IoT for IoT applications over long distances.
4. Infrared (IR):
- Sends data using invisible light waves.
- Commonly used in TV remotes, requiring direct line-of-sight between transmitter and receiver.
5. Microwave:
- Provides secure communication with direct line-of-sight between antennas.
- Suitable for short-distance transmission without needing external networks.

Learn about Wired Communication:

1. Wired Asynchronous Serial Communication (RS-232):
- Sends data character-by-character with start and stop bits.
- Supports full duplex communication over short distances (up to 15 meters).
2. Wired Synchronous Serial Communication (SPI, I2C):
- Transmits data in blocks with synchronized clocks.
- SPI offers high-speed, full duplex communication for peripherals on boards.
- I2C supports multi-master and multi-slave architectures, ideal for integrated circuits and sensors.

Key Points & Summary:

  • Wireless Networking Protocols use radio waves and include error correction for stability.
  • Wi-Fi supports high-speed, full duplex communication over a range of meters to hundreds of meters.
  • Bluetooth offers short-range, medium-speed connections for devices like headphones and keyboards.
  • Zigbee is low-speed but suitable for IoT applications in smart homes and industrial settings.

This summary highlights various communication methods’ characteristics, ranging from short-range, low-power options like Bluetooth and IR to high-speed, long-range options like Wi-Fi and cellular networks. Each method has specific applications and considerations for use based on distance, data rate, and environmental factors.

Learn from Group assignment

Based on group assignment experience with the XIAO ESP32C3 boards and the HTTP communication setup, here are some key learning experiences and a summary:

Learning Experiences:

  1. Setting up WiFi Communication:
  2. Configuring the ESP32C3 boards to connect to a WiFi network (AndroidAP26ab) and establishing communication using sockets and HTTP protocol (socket in MicroPython, WiFi.h in Arduino).
  3. HTTP Server Setup (Dion - Server):
  4. Developed an HTTP server on one ESP32C3 board (Dion) to serve temperature and humidity data.
  5. Utilized the DHT library to read sensor data and responded to client requests with formatted data.
  6. HTTP Client Setup (Matthew - Client):
  7. Configured another ESP32C3 board (Matthew) as an HTTP client to request data from the server (Dion).
  8. Implemented socket programming in MicroPython to connect to the server and receive data periodically.
  9. Error Handling and Debugging:
  10. Dealt with potential errors such as failed sensor readings (isnan checks) and network connectivity issues (try-except blocks).
  11. Integration and Testing:
  12. Integrated hardware (ESP32C3, DHT11 sensor) and software (Arduino IDE, Thonny for MicroPython) components.
  13. Tested the end-to-end communication flow between the server and client boards, ensuring data transmission and reception reliability.

Summary:

  • Networking and Communication Protocols: Explored the implementation of WiFi networking using HTTP protocol for exchanging sensor data between two ESP32C3 boards.
  • Practical Application: Applied theoretical knowledge of networking principles to real-world IoT projects, focusing on data transmission and client-server architecture.
  • Future Considerations: Consider optimizing code for efficiency (e.g., handling larger data sets, implementing security measures), exploring alternative communication protocols (like MQTT for IoT), and expanding project scope (adding more sensors or devices).

This project not only enhances my technical skills in IoT and networking but also prepares for collaborative project environments, emphasizing communication protocols and practical implementation in real-world applications.

Individual assignment:

I will use the XIAO ESP32C3 as main controller to get the data then pubish the data to web page via WiFi.

Step1: Get know about XIAOESP32C3

Parameter Descriptions

Parameter Description
Processor ESP32-C3 SoC
RISC-V single-core 32-bit chip processor with a four-stage pipeline that operates at up to 160 MHz
Wireless Complete 2.4GHz Wi-Fi subsystem
Bluetooth 5.0 / Bluetooth mesh
On-chip Memory 400KB SRAM & 4MB Flash
Interface 1x UART, 1x IIC, 1x SPI, 11x GPIO (PWM), 4x ADC
1x Reset button, 1x Boot button
Dimensions 21 x 17.5 mm
Power Circuit operating voltage: 3.3V @ 200mA
Charging current: 350mA / 100mA
Input voltage (VIN): 5V
Deep Sleep Power Consumption Deep Sleep Model: > 44 μA
Wi-Fi Enabled Power Consumption Active Model: < 75 mA
Modem-sleep Model: < 25 mA
Light-sleep Model: < 4 mA
BLE Enabled Power Consumption Modem-sleep Model: < 27 mA
Light-sleep Model: < 10 mA
Working Temperature -40°C ~ 85°C

Step2: Set and test the WiFi function on XIAO ESP32C3

This code is designed to scan for available WiFi networks using an ESP32 microcontroller. The code initializes the WiFi in station mode, disconnects from any previously connected access points, and then continuously scans for nearby WiFi networks, printing the results to the serial monitor.
This code is written for an ESP32 microcontroller to scan and display nearby WiFi networks.
The setup function initializes the serial communication and sets up the WiFi in station mode. It ensures any previous connections are terminated to avoid interference.
The loop function performs the actual scanning of WiFi networks. It starts by indicating the beginning of the scan process, then uses the WiFi.scanNetworks() function to detect available networks. The results are printed to the serial monitor, including the SSID (network name), RSSI (signal strength), and encryption status. The program waits for 5 seconds before scanning again, making it easy to monitor changes in the network environment.

    #include "WiFi.h"

    // The setup function is called once when the microcontroller starts up
    void setup()
    {
        // Initialize the serial communication at a baud rate of 115200
        Serial.begin(115200);

        // Set the WiFi mode to station mode and disconnect from any previously connected access point
        WiFi.mode(WIFI_STA);
        WiFi.disconnect();
        delay(100);

        // Print a message indicating that the setup is completed
        Serial.println("Setup done");
    }

    // The loop function is called repeatedly after the setup function
    void loop()
    {
        // Print a message indicating that the WiFi scan is starting
        Serial.println("scan start");

        // Call the scanNetworks function of the WiFi library to scan for available networks
        // It will return the number of networks found
        int n = WiFi.scanNetworks();
        // Print a message indicating that the scan is completed
        Serial.println("scan done");
        // If no networks were found
        if (n == 0) {
            // Print a message indicating that no networks were found
            Serial.println("no networks found");
        // If one or more networks were found
        } else {
            // Print the number of networks found
            Serial.print(n);
            Serial.println(" networks found");
            // Loop through each network found
            for (int i = 0; i < n; ++i) {
                // Print the index of the current network
                Serial.print(i + 1);
                Serial.print(": ");
                // Print the SSID (Service Set Identifier) of the current network
                Serial.print(WiFi.SSID(i));
                Serial.print(" (");
                // Print the RSSI (Received Signal Strength Indication) of the current network
                Serial.print(WiFi.RSSI(i));
                Serial.print(")");
                // Print an asterisk if the network is encrypted, otherwise print a space
                Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
                delay(10);
            }
        }
        // Print a blank line for better formatting
        Serial.println("");

        // Wait for 5 seconds before scanning again
        delay(5000);
    }

Step3: Config the WiFi with local name and passward

In this step we need to set up XIAO ESP32C3 to connect to my local WiFi. In this part I need to make some changes, mainly to change the name and password of the WiFi network.
But when I entered it, I found that the wifi connection stayed connected and no successful connection was displayed. So I checked and found several issues.
1. I forgot to install the XIAO ESP32C3 antenna when connecting to wifi, causing the motherboard to be unable to obtain the signal.
2. I entered the wrong wifi name and password when connecting to wifi.
3. The most important thing is that after I checked the wiki page of XIAO ESP32C3, I found that this product is only equipped with 2.4G wireless network connection function, which means that if my wifi network is 5G, I cannot connect to it.
After final investigation, I finally successfully connected to the wifi network, and the results are as follows.

Step4. Webpage inteaction with ESP32C3 through Wifi

Workflow and Experience:

  • Setup: I connect my ESP32 to my computer and upload the provided code using the Arduino IDE.
  • WiFi Connection: After uploading the code, I open the Serial Monitor to see my ESP32 trying to connect to my WiFi network.
  • Successful Connection: Once connected, the Serial Monitor shows the IP address assigned to my ESP32.
  • Accessing the Webpage: I open a web browser and type in the IP address from the Serial Monitor. This brings up a simple webpage showing the distance measurements from the ultrasonic sensor.
  • Real-time Data: Every time I refresh the webpage, I see the latest distance measurements.

Code Summary and Explanation:

Here’s the combined code for connecting my ESP32 to WiFi, reading ultrasonic sensor data, and displaying it on a webpage:

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

// Replace with your network credentials
const char* ssid = "SEEED-MKT";
const char* password = "edgemaker2023";

Ultrasonic ultrasonic(5);
WebServer server(80); // Create a web server on port 80

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  while (!Serial); // Wait for Serial Monitor to open

  // Initialize WiFi
  WiFi.begin(ssid, password);
  Serial.println("Connecting to WiFi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("Connected to WiFi");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // Define the web server route
  server.on("/", handleRoot);

  // Start the web server
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  // Handle client requests
  server.handleClient();
}

void handleRoot() {
  long RangeInInches = ultrasonic.MeasureInInches();
  long RangeInCentimeters = ultrasonic.MeasureInCentimeters();

  // Print sensor data to Serial Monitor
  Serial.print("Distance to obstacle: ");
  Serial.print(RangeInInches);
  Serial.print(" inch, ");
  Serial.print(RangeInCentimeters);
  Serial.println(" cm");

  String html = "<html><body>";
  html += "<h1>Ultrasonic Sensor Data</h1>";
  html += "<p>Distance to obstacle: " + String(RangeInInches) + " inch</p>";
  html += "<p>Distance to obstacle: " + String(RangeInCentimeters) + " cm</p>";
  html += "</body></html>";

  // Send the response to the client
  server.send(200, "text/html", html);
}

Key Points:

  • WiFi Connection: I connect my ESP32 to WiFi with my credentials and see the connection status and IP address in the Serial Monitor.
  • Web Server: My ESP32 runs a simple web server on port 80, serving an HTML page with sensor data.
  • Ultrasonic Sensor Data: The sensor measures the distance, and this data is displayed on the webpage.
  • HTTP Response: The HTML content is dynamically created and sent to the client each time the page is accessed.
  • Serial Monitor Output: The sensor data is printed to the Serial Monitor for real-time monitoring.
    With this code, I can easily monitor the distance measured by my ultrasonic sensor through a web interface using my ESP32’s WiFi capabilities.

Here is the hero Shot: