Home About Final

Week 12: Networking and Comunications

Assignment

  • design, build, and connect wired or wireless node(s) with network or bus addresses and local input &/or output device(s)
  • I used this week to work on my final project: Anemoia Device

    How could I get inputs from a GPT to control outputs from a pump across two microcontrollers

    Sending Signals:

    While you can use Wi-Fi, you can also turn the XIAO into a hotspot itself using something like:

                    
                #include <WiFi.h>
                #include <HTTPClient.h>
                
                // Wi-Fi credentials
                const char* ssid = "ESP32_Test";
                const char* password = "12345678";
                    
                

    The above is the built-in ESP32_Test SSID, which worked well for me initially. Trying to create my own network was less successful this week.

    From there I created a server which was located at (could find this using ifconfig in the terminal):

                    
                // Server URL (replace with your server or endpoint)
                const char* serverURL = "http://192.168.4.1:80/";
                        
                

    With that setup, I could write some code for a potentiometer to send values across this network. It reads the value from the potentiometer, with a threshold value to turn something ON or OFF (1, 0) and send that value over the network.

                    
                    void loop() {
                        // Reading potentiometer value
                        int potValue = analogRead(potPin);
                        Serial.print("Potentiometer Value: ");
                        Serial.println(potValue);
                      
                        // Determine pump state based on potentiometer value
                        String pumpState = (potValue >= pumpThreshold) ? "1" : "0"; // "1" = ON, "0" = OFF
                      
                        // Send the pump state over Wi-Fi
                        if (WiFi.status() == WL_CONNECTED) { // Check Wi-Fi connection
                          HTTPClient http;
                      
                          // Construct the GET request URL with the pump state
                          String url = String(serverURL) + "?state=" + pumpState;
                          http.begin(url);
                      
                          Serial.print("HTTP URL: ");
                          Serial.println(url);
                      
                          // Send HTTP GET request
                          int httpResponseCode = http.GET();
                      
                          // Print the HTTP response code
                          Serial.print("HTTP Response code: ");
                          Serial.println(httpResponseCode);
                      
                          // Close the connection
                          http.end();
                        } else {
                          Serial.println("Wi-Fi not connected");
                        }
                      
                        delay(500); // Delay before the next read
                      } 
                       
                

    Receiving Values

    Next, on the other side of things, I needed to receive values on the XIAO that is controlling my pumps.

    Connecting on the same network, and setting the pump to a pin, we read the values being sent across the network. If a ‘1’ is received, the pump is turned ON; otherwise, it turns off. For now, that is all I needed to do.

                    
                #include <WiFi.h>
                #include <WebServer.h>
                
                // Wi-Fi credentials
                const char* ssid = "ESP32_Test";
                const char* password = "12345678";
                
                // Create a web server on port 80
                WebServer server(80);
                
                // Pin where the pump is connected
                const int pumpPin = 2;
                
                // Function to handle pump control
                void handleInput() {
                    // Read the "state" parameter from the HTTP request
                    String stateStr = server.arg("state");
                    int state = stateStr.toInt(); // Convert the state to an integer
                
                    // Ensure state is either 0 or 1
                    state = clamp(state, 0, 1);
                
                    // Set the pump state
                    digitalWrite(pumpPin, state == 1 ? HIGH : LOW);
                    Serial.println(state == 1 ? "Pump ON" : "Pump OFF");
                
                    // Respond to the client with the current pump state
                    if (state == 1) {
                        server.send(200, "text/plain", "Pump ON");
                    } else {
                        server.send(200, "text/plain", "Pump OFF");
                    }
                }
                
                // Clamp function to limit values between min and max
                template <typename T>
                T clamp(T value, T minVal, T maxVal) {
                    return value < minVal ? minVal : (value > maxVal ? maxVal : value);
                }
                
                void setup() {
                    // Initialize the pump pin as an output
                    pinMode(pumpPin, OUTPUT);
                    digitalWrite(pumpPin, LOW); // Ensure pump is initially off
                
                    // Start serial communication
                    Serial.begin(115200);
                
                    // Create the Wi-Fi hotspot
                    WiFi.softAP(ssid, password);
                    Serial.print("Hotspot IP Address: ");
                    Serial.println(WiFi.softAPIP());
                
                    // Set up the route to handle pump control
                    server.on("/", handleInput);
                
                    // Start the web server
                    server.begin();
                    Serial.println("Server started!");
                }
                
                void loop() {
                    // Handle client requests
                    server.handleClient();
                }
                        
                

    Group Assignment

    For the group assignment, I connected this logic to Jessica’s lights instead of a pump, synchronized to the soundtrack of *Sandstorm*.