#include #include const char* ssid = "Network name"; // Network name const char* password = "Network password"; // Network password WebServer server(80); //Default port for the ESP32 server relationship const int M1 = 16; //Motor 1 on pin 16 const int M2 = 17; //Motor 2 on pin 17 const int M3 = 18; //Motor 3 on pin 18 const int M4 = 19; //Motor 4 on pin 19 void Homepage() // Function that contains my server and serves as my homepage { String page = ""; // The lines in my HTML server preceded by the string function so that it is readable by the ESP32 page += ""; page += ""; page += " Server ESP32"; page += " "; page += " "; page += ""; page += ""; page += "
"; page += "

THEO ROBOT

"; page += "
"; page += "
"; page += " GO!"; page += " TURN LEFT"; page += " TURN RIGHT"; page += " STOP"; page += "
"; page += "
"; page += "

This server is hosted on an ESP32

"; page += " Created by Théo GAUTIER, AgriLab FabAcademy 2021"; page += "
"; page += ""; page += ""; server.setContentLength(page.length()); //This allows the ESP32 to know the length of the website, which allows faster communication server.send(200, "text/html", page); // Enables data to be sent } void NotFound() // Function that writes to the user "404 Not found" if a page is unknown { server.send(404, "text/plain", "404: Not found"); } void go() //Function that allows the robot to move forward, all motors are on { digitalWrite(M1, HIGH); digitalWrite(M2, HIGH); digitalWrite(M3, HIGH); digitalWrite(M4, HIGH); server.sendHeader("Location","/"); // This then links to the home page server.send(303); // Command that tells the server it is doing a redirect } void stop() //Function that allows the robot to stop, all motors are off { digitalWrite(M1, LOW); digitalWrite(M2, LOW); digitalWrite(M3, LOW); digitalWrite(M4, LOW); server.sendHeader("Location","/"); // This then links to the home page server.send(303); // Command that tells the server it is doing a redirect } void left() //Function that allows the robot to turn left by stopping motors 1 and 2 { digitalWrite(M1, LOW); digitalWrite(M2, LOW); digitalWrite(M3, HIGH); digitalWrite(M4, HIGH); server.sendHeader("Location","/"); // This then links to the home page server.send(303); // Command that tells the server it is doing a redirect } void right() // Function that allows the robot to turn right by stopping motors 3 and 4 { digitalWrite(M1, HIGH); digitalWrite(M2, HIGH); digitalWrite(M3, LOW); digitalWrite(M4, LOW); server.sendHeader("Location","/"); // This then links to the home page server.send(303); // Command that tells the server it is doing a redirect } void setup() { Serial.begin(115200); delay(1000); // Delay 1s Serial.println("\n"); // Make a line break in the serial monitor pinMode(M1,OUTPUT); // Says that M1 is an output pinMode(M2,OUTPUT); // Says that M2 is an output pinMode(M3,OUTPUT); // Says that M3 is an output pinMode(M4,OUTPUT); // Says that M4 is an output digitalWrite(M1, LOW); // Set M1 to stop digitalWrite(M2, LOW); // Set M2 to stop digitalWrite(M3, LOW); // Set M3 to stop digitalWrite(M4, LOW); // Set M4 to stop WiFi.begin(ssid, password); // Function to start the wifi connection Serial.print("Connection in progress..."); // Print on the serial monitor "Connection in progress..." while(WiFi.status() != WL_CONNECTED) // As long as the connection to the network has not been established then print a dot every second { Serial.print("."); delay(1000); } Serial.println("\n"); // Make a line break in the serial monitor Serial.println("Connection established!"); // Print on the serial monitor "Connection established!" Serial.print("Adresse IP: "); // Print on the serial monitor "Adresse IP:" Serial.println(WiFi.localIP()); // Print on the serial monitor the local IP server.on("/", Homepage); // When the server is on "/" then do the homepage function server.on("/go", go); // When the server is on "/go" then do the go function server.on("/left", left); // When the server is on "/left" then do the left function server.on("/right", right); // When the server is on "/right" then do the right function server.on("/stop", stop); // When the server is on "/stop" then do the stop function server.onNotFound(NotFound); // When the server is not found then do the NotFound function server.begin(); // Start the server Serial.println("Active web server!"); // Print on the serial monitor "Active web server!" } void loop() { server.handleClient(); // Manage the user who is connected to the server }