Home About Final

Week 13: Interface and Application Programming

Assignment

  • Write an application that interfaces a user with an input &/or output device that you made
  • I used this week to work on my final project: Anemoia Device focusing on the following goal:

    Create a web interface that could control 4 pumps over a local network or Internet (WiFi) connection

    Interfacing with Pumps:

    In the previous week, I had created a board for my final project, the Anemoia Device. I ordered some pumps and wired everything up. I found however that my board did not work as expected:

    After debugging, I redesigned the board and created a new one; the Anemoia PCB v 0.5. I soldered components to the board, wired everything up and this time it did work as expected.

    Next, I built on a previous program I used to test a single pump to test the pumps out. I then wrote an application that would do the following:

                    
                        int pumpState[numPumps] = {0, 0, 0, 0};
    
                        // Function to toggle the pump state
                        void togglePump(int pump) {
                          if (pumpState[pump] == 0) {
                            digitalWrite(pumpPins[pump], HIGH); // Turn pump ON
                            pumpState[pump] = 1;
                          } else {
                            digitalWrite(pumpPins[pump], LOW); // Turn pump OFF
                            pumpState[pump] = 0;
                          }
                        }                    
                    
                

    And a function to handle toggle requests

                    
                        void handleToggle() {
                            if (server.hasArg("pump")) {
                              int pump = server.arg("pump").toInt();
                              if (pump >= 0 && pump < numPumps) {
                                togglePump(pump);
                                server.send(200, "text/plain", "Pump " + String(pump) + " toggled!");
                              } else {
                                server.send(400, "text/plain", "Invalid pump ID!");
                              }
                            } else {
                              server.send(400, "text/plain", "Missing pump ID!");
                            }
                          }
                        
                

    With that all setup, I then tested my interface with the pumps. I faced a few issues with reliable WiFi connection with the XIAO ESP32S3 which I will have to look into more deeply for a final presentation of the project. Typically unplugging and plugging back in works and at worst having to hit the reset button. However, when everything is assembled this will become trickier.

    After managing to connect reliably to WiFi, the toggle buttons worked as expected after a few tries. The main issue was loose wiring as currently I’m working with simple cables that are pretty easy to dislodge particularly with the setup I have.

    An end-to-end demo and the code used for this week in full are below:

                    
                        #include  
                            #include 
                            // Wi-Fi credentials
                            const char* ssid = "####";       
                            const char* password = "#######; 
                            
                            const int pumpPins[] = {2, 3, 4, 5}; //relay modules
                            const int numPumps = 4;
                            
                            WebServer server(80);
                            
                            // web interface
                            const char webpage[] = R"rawliteral(
                            
                            
                            
                            )rawliteral";
                            
                            // get state of each pump (O=off)
                            int pumpState[numPumps] = {0, 0, 0, 0};
                            
                            // function to toggle the pump state
                            void togglePump(int pump) {
                              if (pumpState[pump] == 0) {
                                digitalWrite(pumpPins[pump], HIGH); // Turn pump ON
                                pumpState[pump] = 1;
                              } else {
                                digitalWrite(pumpPins[pump], LOW); // Turn pump OFF
                                pumpState[pump] = 0;
                              }
                            }
                            
                            // Handle the root page (web interface)
                            void handleRoot() {
                              server.send(200, "text/html", webpage);
                            }
                            
                            // function for toggle requests
                            void handleToggle() {
                              if (server.hasArg("pump")) {
                                int pump = server.arg("pump").toInt();
                                if (pump >= 0 && pump < numPumps) {
                                  togglePump(pump);
                                  server.send(200, "text/plain", "Pump " + String(pump) + " toggled!");
                                } else {
                                  server.send(400, "text/plain", "Invalid pump ID!");
                                }
                              } else {
                                server.send(400, "text/plain", "Missing pump ID!");
                              }
                            }
                            
                            void setup() {
                              Serial.begin(115200);
                            
                              // Initialize pumps
                              for (int i = 0; i < numPumps; i++) {
                                pinMode(pumpPins[i], OUTPUT);
                                digitalWrite(pumpPins[i], LOW); // all pumps off initially
                              }
                            
                              // connect to Wi-Fi
                              Serial.print("Connecting to Wi-Fi");
                              WiFi.begin(ssid, password);
                              while (WiFi.status() != WL_CONNECTED) {
                                delay(500);
                                Serial.print(".");
                              }
                              Serial.println("\nWi-Fi connected.");
                              Serial.println("IP Address: " + WiFi.localIP().toString());
                            
                              // web server routes
                              server.on("/", handleRoot);
                              server.on("/toggle", handleToggle);
                            
                              // web server
                              server.begin();
                              Serial.println("Web server started.");
                            }
                            
                            void loop() {
                              server.handleClient();
                            }