WEEK 10 : Network and Communications

So starting off lets recap, for the past two weeks of inputs and outputs I tried to test different proximity sensors, motors and designs in order to make the running coffee table actually move. see below the experiments!

.

As you can see here the table was working on a smaller scale however when I scaled up last week for out put devices it stopped working because the table could not really carry its weight with the motor in place.. I realized then it was time to pivot and think a little more like an engineer rather than a designer and embed the movement within the legs..

Okay! so onto this week! I went ahead and re designed the leg with two servo motors in order to have two degrees of freedom and be able to control the joints of the leg I found some super helpful youtube tutorials on flexure design and compliant links here is one of them which was very helpful to start understanding how moving fixtures would work. see below the modelling of one of the legs, then I connected it to my board and with the help of my good friend chat gpt linked both of the servos to create a leg like movement.

.

.


                  #include Servo.h

                    Servo myservo;   // Create servo object to control the first servo
                    Servo myservo2;  // Create another servo object to control the second servo
                    
                    void setup() {
                      myservo.attach(26);  // Attaches the first servo on GPIO pin 26 to the servo object
                      myservo2.attach(27); // Attaches the second servo on GPIO pin 27 to the servo object
                    }
                    
                    void loop() {
                      // Synchronize the movement of both servos with different ranges
                      for (int pos = 0; pos <= 180; pos += 1) { // Goes from 0 degrees to 180 degrees for myservo
                        myservo.write(pos);               // Move the first servo to position 'pos'
                        myservo2.write(map(pos, 0, 180, 45, 135)); // Move the second servo to a mapped position
                        delay(5);                         // Waits 5ms for the servos to reach the position
                      }
                      for (int pos = 180; pos >= 0; pos -= 1) { // Goes from 180 degrees to 0 degrees for myservo
                        myservo.write(pos);               // Move the first servo to position 'pos'
                        myservo2.write(map(pos, 0, 180, 45, 135)); // Move the second servo to a mapped position
                        delay(5);                         // Waits 5ms for the servos to reach the position
                      }
                    }
                        

            

Then in order to fulfill this weeks assignment I wanted to create a board using the xiao esp32c3 in order to be able to create an interface to control the leg from my computer and control both of the servos movements, in theory it would be great if I could eventually built some sort of remote controll so I can just take the table for walks :D. But anyways, This started as a new adventure since I have been only using the rp2040 throughout the whole semester. I started off by building a new breakout board where I could connect the servos, the proximity sensor an LED and the xiao.

.

After soddering the board and its components I tested the connection with the web server I used my laptop for this and my phones hot spot to connect both the computer and the esp32c3. I was surprised how straight forward it was to host the web server with the ip provided in the serial monitor.

.

Sooo ideally I wanted to connect the proximity sensor to the web server and host the readings on the web page but I usually work from my desktop and I have used the proximity sensor many times for my previous weeks but this week since it was my first time using my laptop when I downloaded the VL53L1X library the directory could not find it. So I decided to create a web server where I would only host the servo motors controls so I could move the leg however I wanted to. I started by creating control for each servo. See below

Then I decided to link the movements of both servos.

Here is the full code to host two stepper motors on a web server from an esp32c3! and two red buttons with arrows!


                          #include 
                            #include 
                            #include 
                            
                            // Network credentials
                            const char* ssid = "iPhone";       // Hotspot name
                            const char* password = "mateoferfer";  // Hotspot password
                            
                            WebServer server(80); // HTTP server on port 80
                            
                            Servo servo1;
                            Servo servo2; // Second servo object
                            int servoPosition = 90; // Initial position for both servos
                            const int servo1Pin = 3;  // GPIO 3 is connected to servo 1
                            const int servo2Pin = 2;  // GPIO 2 is connected to servo 2
                            const int stepSize = 3;  // Step size for slower movement
                            
                            void setup() {
                              Serial.begin(115200);
                              servo1.attach(servo1Pin);  // Attaches servo1 on GPIO 3 to the servo object
                              servo2.attach(servo2Pin);  // Attaches servo2 on GPIO 2 to the servo object
                              servo1.write(servoPosition);  // Initialize both servos to the middle position (90 degrees)
                              servo2.write(servoPosition);
                            
                              // Connect to Wi-Fi
                              WiFi.begin(ssid, password);
                              while (WiFi.status() != WL_CONNECTED) {
                                delay(1000);
                                Serial.println("Connecting to WiFi...");
                              }
                            
                              // Print the IP address
                              Serial.println("Connected to WiFi");
                              Serial.println("IP address: ");
                              Serial.println(WiFi.localIP());
                            
                              // Route for root / web page
                              server.on("/", handleRoot);
                            
                              // Routes to handle servo movement
                              server.on("/move", []() {
                                String direction = server.arg("direction");
                                if (direction == "left") {
                                  servoPosition = max(0, servoPosition - stepSize); // Decrease position
                                } else if (direction == "right") {
                                  servoPosition = min(180, servoPosition + stepSize); // Increase position
                                }
                                servo1.write(servoPosition);
                                servo2.write(servoPosition);
                                server.send(204, "text/plain", ""); // No content response
                              });
                            
                              // Start server
                              server.begin();
                            }
                            
                            void loop() {
                              server.handleClient(); // Handle client requests
                            }
                            
                            void handleRoot() {
                              String html = R"====(
                              html
                                head
                                  title>ESP32 Linked Servo Control
                                  style>
                                    button {
                                      font-size: 24px;
                                      padding: 15px;
                                      margin: 5px;
                                      border: none;
                                      border-radius: 50%;
                                      color: white;
                                      background-color: red;
                                    }
                                    button:active {
                                      background-color: darkred;
                                    }
                                  /style>
                                /head>
                                body>
                                  h1>ESP32 Linked Servo Control
                                  p>
                                    button onmousedown="moveServo('left')" onmouseup="stopMove()" ontouchstart="moveServo('left')" ontouchend="stopMove()">←
                                    button onmousedown="moveServo('right')" onmouseup="stopMove()" ontouchstart="moveServo('right')" ontouchend="stopMove()">→
                                  /p>
                                  script>
                                    var timer;
                                    function moveServo(direction) {
                                      timer = setInterval(function() {
                                        fetch('/move?direction=' + direction);
                                      }, 40); // Increased interval for slower movement
                                    }
                                    function stopMove() {
                                      clearInterval(timer);
                                    }
                                  /script>
                                /body>
                              /html>
                              )====";
                              server.send(200, "text/html", html);
                            }