week14. Networking and communications

Group assignment:

1. Send a message between two projects 2. 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.

Part1 Research

Wired Asynchronous Serial Communication

RS-232:

Wired Synchronous Serial Communication

SPI (Serial Peripheral Interface):

I2C (Inter-Integrated Circuit):

Wireless Networking Protocol

Wi-Fi:

Bluetooth:

Zigbee:

Part2 Group Assigment My Part

Our group used XIAO ESP32C3 board connecting to the DHT11 temperature and humidity sensor and one XIAO ESP32C3 board connecting a LED.

One ESP32C3 is sending data over WiFi and another ESP32 device receiving this data on the same WiFi network, applying HTTP protocol, to complete the sending and receiving.

What I have learned from our group assigment

Sensor Interface and Data Acquisition

WiFi Network Configuration and Communication

HTTP Protocol Application

Embedded System Programming

System Integration and Debugging

Part3 Individual Assigment

This week's assignment is to implement the communication of the chip. I chose to create a web page to complete the communication between the computer web page and ESP32C3.

IMPORTANT! ESP32C3 can only work on 2.4G wifi.

Step1. Scan the Wifi Networks Available.


    #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);
    }
  
Description of image This code is for an ESP32C3 to scan for available WiFi networks. In the `setup` function, it initializes the serial communication and sets the WiFi mode to station and disconnects from any previous connection. The `loop` function, it starts by indicating that a scan is beginning. Then it performs the scan and determines the number of networks found. If no networks are found, it prints a corresponding message. If networks are found, it prints details such as the SSID, RSSI, and whether the network is encrypted for each network. A blank line is printed for formatting, and then it waits for 5 seconds before scanning again.

Step2. ESP32C3 Connected to my iPhone's wifi



#include <WiFi.h>

// The Wi-Fi network SSID (Service Set Identifier)
const char* ssid     = "iPhone";
// The Wi-Fi network password
const char* password = "12345678";   

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

  // Print some blank lines for better readability
  Serial.println();
  Serial.println();
  // Print the SSID that the device is trying to connect to
  Serial.print("Connecting to ");
  Serial.println(ssid);

  // Start the Wi-Fi connection process with the provided SSID and password
  WiFi.begin(ssid, password);

  // Keep looping until the Wi-Fi connection is established
  while (WiFi.status()!= WL_CONNECTED) {
      // Wait for 500 milliseconds
      delay(500);
      // Print a dot to indicate that the device is still trying to connect
      Serial.print(".");
  }

  // Print a new line for better formatting
  Serial.println("");
  // Print a message indicating that the Wi-Fi connection is successful
  Serial.println("WiFi connected");
  // Print the local IP address assigned to the device
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}  

// The loop function runs repeatedly after the setup function
void loop()
{
}

Description of image Explaination of the code In the `setup` function, it initializes serial communication, attempts to connect to a specified Wi-Fi network using the provided SSID and password. It keeps indicating the connection status with dots until the connection is established and then prints a success message along with the local IP address. The `loop` function is empty in this case, meaning no specific actions are performed repeatedly.

Step3. ESP32C3 Connect to my Home wifi

3.1 First, I changed the wifi name and password to my home wifi, but it didn't work. Description of image 3.2 Then I found that ESP32C3 can only connect to 2.4G wifi. Description of image 3.3 Then I changed one of my home wifi to 2.4G . Description of image 3.4 Now I can succesffly connect ESP32C3 to my home wifi . Description of image

Step4. Webpage inteaction with ESP32C3 through Wifi

4.1 I changed the code to include a simple html page and it could control the LED on the ESP32C3. The code is as below:

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

// Define the Wi-Fi network SSID (Service Set Identifier)
const char* ssid = "Katherine_Home";  // Replace with your Wi-Fi network name
// Define the Wi-Fi network password
const char* password = "NiuNiu876519";  // Replace with your Wi-Fi password

// Define the pin where the LED is connected
#define LED_PIN D1  

// Create a WebServer object and listen on port 80
WebServer server(80);  

// Function to handle the root URL ("/")
void handleRoot() {
  // Create an HTML string
  String html = "<html><head><title>ESP32 LED Control</title></head><body>";
  html += "<h1>Xiao ESP32 LED Control</h1>";
  html += "<p><a href=\"/on\">Turn LED ON</a></p>";
  html += "<p><a href=\"/off\">Turn LED OFF</a></p>";
  html += "</body></html>";
  // Send the HTML response with status code 200 and content type "text/html"
  server.send(200, "text/html", html);
}

// Function to handle the "/on" URL
void handleOn() {
  // Set the LED pin to HIGH (turn the LED on)
  digitalWrite(LED_PIN, HIGH);
  // Send a plain text response with status code 200
  server.send(200, "text/plain", "LED is ON");
}

// Function to handle the "/off" URL
void handleOff() {
  // Set the LED pin to LOW (turn the LED off)
  digitalWrite(LED_PIN, LOW);
  // Send a plain text response with status code 200
  server.send(200, "text/plain", "LED is OFF");
}

// The setup function is called once when the microcontroller starts
void setup() {
  // Set the LED pin as an output
  pinMode(LED_PIN, OUTPUT);

  // Initialize the serial communication at a baud rate of 115200
  Serial.begin(115200);

  // Start the connection process to the Wi-Fi network
  WiFi.begin(ssid, password);

  // Keep looping until the Wi-Fi connection is established
  while (WiFi.status()!= WL_CONNECTED) {
    // Wait for 1 second
    delay(1000);
    // Print a message indicating that the device is connecting to the Wi-Fi network
    Serial.println("Connecting to WiFi...");
    Serial.println(ssid);
  }

  // Print a message indicating that the Wi-Fi connection is successful
  Serial.println("Connected to WiFi");
  Serial.println(ssid);
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Set up handlers for the different URLs
  server.on("/", handleRoot);
  server.on("/on", handleOn);
  server.on("/off", handleOff);

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

// The loop function is called repeatedly after the setup function
void loop() {
  // Handle client requests
  server.handleClient();
}

This code is for an ESP32 to control an LED through a web server. In the `setup` function, it configures the LED pin as an output, initializes serial communication, attempts to connect to the specified Wi-Fi network, and sets up handlers for different URLs. Once connected to Wi-Fi, it starts the web server. In the `loop` function, it handles client requests. The `handleRoot` function sends an HTML page with links to turn the LED on or off. The `handleOn` and `handleOff` functions respectively turn the LED on or off and send corresponding responses.

Use my computer's IP to open the webpage and it works!

  KK Rocks!