13.Networking and communications

This week is all about communication and networking.

Very important to understand this because I will need to have 2 boards to be communicating through their own wifi. The switch and servo are very important for my final project.

That is why I tested this with the barduino and my own board. Both have the same microcontroller: ESP32 S3 Wroom 1.

The ESP32 s3 wroom 1 have their own built in wifi and bluetooth to communicate to each other or other boards.


Software

To connect the boards over wifi, it is important to select which one will be the AccessPoint and which one will be the client.

As I am going to use arduino, it is important to have the correct libraries installed. For the servo motor i needed the ESP32Servo.h library.

I named the boards number 1 and number 2 to to make it easier to work and avoid confusion in arduino.

ESP32 board number 1 a.k.a. AP

For now I am using the barduino as the AP because I want to test my own board with the servo. The only functions that the barduino has are being AP and the connected switch.

This is the code I uploaded in arduino:

        #include < WiFi.h >

          // Define the WiFi credentials
          const char* ssid = "ESP32_AP";
          const char* password = "123456789";
          
          // Define the server port
          const int serverPort = 80;
          
          // Define the reset button pin
          const int resetButtonPin = 1;
          
          // Create a WiFiServer object
          WiFiServer server(serverPort);
          
          void setup() {
            Serial.begin(115200);
            
            // Initialize the ESP32 as a soft AP
            WiFi.softAP(ssid, password);
            
            // Print the IP address of the access point
            IPAddress ip = WiFi.softAPIP();
            Serial.print("AP IP address: ");
            Serial.println(ip);
          
            // Start the server
            server.begin();
          
            // Set the reset button pin as input
            pinMode(resetButtonPin, INPUT_PULLUP);
          }
          
          void loop() {
            // Check if the reset button is pressed
            int pinoutput = digitalRead(resetButtonPin);
             
           // if (digitalRead(resetButtonPin) == LOW) {
              // If the button is pressed, send a message to the client
              WiFiClient client = server.available();
            if (client) {
                Serial.println("Sending message to ESP32_2");
                client.println(pinoutput);
                client.stop();
                delay(5);
              }
            
            //}
          }

      

This code configures an ESP32 to act as a Wi-Fi access point and host a server on that network. It monitors a button connected to the ESP32 and waits for a client device to connect to its server. Once a client connects, the ESP32 sends the current state of the button (whether it's pressed or not) to the client. The ESP32 keeps checking the button's state and sends updates to any connected client in its main loop.

ESP32 board number 1 a.k.a. client

The second board is connected to the access point and the servo.

When the switch on the first board turns ON: the led on the second board turns on and the servo starts turning. When the switch goes back to OFF and the servo returns to the origin.

This is the code I uploaded in arduino:

        #include < WiFi.h >
        #include < ESP32Servo.h >
          
          // Define the WiFi credentials
          const char* ssid = "ESP32_AP";
          const char* password = "123456789";
          const char* serverIP = "192.168.4.1";
          const int serverPort = 80;
          
          // Define the LED pin
          const int ledPin = 21;
          const int servoPin = 1;
          
          Servo myservo;
          
          void setup() {
            Serial.begin(115200);
            
            // Connect to WiFi network
            WiFi.begin(ssid, password);
            Serial.println("Connecting to WiFi...");
            while (WiFi.status() != WL_CONNECTED) {
              delay(1000);
              Serial.print(".");
            }
            Serial.println("\nConnected to WiFi");
          
            // Set the LED pin as output
            pinMode(ledPin, OUTPUT);
            
            // Attach the servo to the servo pin
            myservo.setPeriodHertz(50); // Standard servo frequency
            myservo.attach(servoPin);
          }
          
          void loop() {
            // Create a WiFiClient object and connect to the server
            WiFiClient client;
            if (client.connect(serverIP, serverPort)) {
              Serial.println("Connected to server");
              while (client.connected()) {
                // Check if there is data available from the server
                if (client.available()) {
                  // Read the message from the server
                  String message = client.readStringUntil('\n');
                  int val = message.toInt();
                  Serial.printf("Message from server: %d\n", val);
                  
                  // Check if the message is "0"
                  if (val == 0) {
                    // Turn on the LED
                    digitalWrite(ledPin, HIGH);
                    Serial.println("LED turned on");
                    // Rotate the servo to its maximum position
                    myservo.writeMicroseconds(2400); // Maximum position (2.4 ms pulse)
                    delay(500); // Delay for the servo to reach the position
                  }
                  else {
                    // Turn off the LED
                    digitalWrite(ledPin, LOW);
                    Serial.println("LED turned off");
                    // Rotate the servo to its minimum position
                    myservo.writeMicroseconds(600); // Minimum position (0.6 ms pulse)
                    delay(500); // Delay for the servo to reach the position
                  }
                }
              }
              // Disconnect from the server
              client.stop();
              Serial.println("Disconnected from server");
            }
            // Delay before retrying
            delay(500);
          }

      

This code connects an ESP32 to a Wi-Fi network and communicates with a server at the IP address "192.168.4.1" on port 80. It controls an LED and a servo motor based on messages received from the server. If the received message is "0", it turns on the LED and moves the servo to its maximum position; otherwise, it turns off the LED and moves the servo to its minimum position. The ESP32 continually checks for server messages, processes them, and then disconnects and reconnects as needed.

Libraries

The ESP32Servo.h library: allows for the control of servo motors using an ESP32 (microcontroller that i am using), providing functionality similar to the standard Arduino Servo library but optimized for the ESP32's capabilities. Without this library, the servo does not read the code well.

The wifi.h library: enables the ESP32 to connect to Wi-Fi networks, allowing for wireless communication and internet connectivity in various applications.

Video of the boards in communication

Group assignment

week 13