/* * Copyright (C) 2021 - Cuautli Garcia - All Rights Reserved * * FabAcademy * * Version 1.0 * Autor: Cuautli Garcia - cuautli.garciaa@gmail.com * */ //Required libraries #include // Wifi credentials const char* ssid = "HOME_2.4G"; const char* password = "5GaRcIaS"; //Set the web server port number to 80 WiFiServer server(80); //String to store the end of the URL String cadenarecibida; //Variables to save the state of the motors String motor1 = "off"; String motor2 = "off"; //GPIO pins const int outA11 = 2; const int outA12 = 15; const int outB11 = 0; const int outB12 = 4; const int motorsOn = 12; void setup() { Serial.begin(115200); // Initialize pins pinMode(outA11, OUTPUT); pinMode(outA12, OUTPUT); pinMode(outB11, OUTPUT); pinMode(outB12, OUTPUT); pinMode(motorsOn, OUTPUT); // Set outputs to LOW digitalWrite(outA11, LOW); digitalWrite(outA12, LOW); digitalWrite(outB11, LOW); digitalWrite(outB12, LOW); digitalWrite(motorsOn, LOW); //Stablishing connectio to the given WiFi Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } //Print local IP address of the ESP32 Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); //Start web server server.begin(); } void loop(){ WiFiClient cliente = server.available(); // Listen for incoming clients if (cliente) { //If there is a client Serial.println("Nuevo Usuario"); //Print "New user" while (cliente.connected()) { //While the cliente is connected stay in the loop if (cliente.available()) { //If the client sends information char c = cliente.read(); //Save the informacion in "c" if(cadenarecibida.length() < 100) //If the lecture length is less than 100 characters cadenarecibida += c; //Add the gotten info to the previous string if (c == '\n') { //If there is a line break cliente.println("HTTP/1.1 200 OK"); //Server title cliente.println("Content-Type: text/html"); //File type that the server will use cliente.println(); //Print a white space cliente.println(""); //File definition (HTML) cliente.println(""); //Start the HTML file cliente.println(""); //Header cliente.println("Using a Web Server with an ESP32");//print the header name cliente.println("
"); //Document at the center cliente.println(""); //Close the header cliente.println(""); //Start the body if the file cliente.println("

Controlling a Dual H-Bridge with an ESP32

");//Print the title cliente.println("
"); //Print a horizontal line cliente.println("

Motor 1

"); //Print a subtitle //Create a hiperlink with the button and its name, it will add a text in the page address cliente.println(""); cliente.println("    ");//Print a tab cliente.println(""); cliente.println("    ");//Print a tab cliente.println(""); cliente.println("
"); //Print a line break cliente.println("
"); //Print a line break cliente.println("

Motor 2

"); //Print a second subtitle cliente.println(""); cliente.println("    ");//Print a tab cliente.println(""); cliente.println("    ");//Print a tab cliente.println(""); //style=width:100px;height:45px cliente.println("
"); //Print a line break cliente.println(""); //Close the body cliente.println(""); //Close the HTML file delay(1); //Wait a millisecond for a new entrance cliente.stop(); //Close the connection digitalWrite(motorsOn, LOW); //Reset the Dual H-Bridge if (cadenarecibida.indexOf("?Off1")>0){ //Turn off the motor 1 digitalWrite(outA11, LOW); digitalWrite(outA12, LOW); motor1 = "off";} if (cadenarecibida.indexOf("?Off2")>0){ //Turn off the motor 2 digitalWrite(outB11, LOW); digitalWrite(outB12, LOW); motor2 = "off";} if (cadenarecibida.indexOf("?For1")>0){ //Forward direction motor 1 digitalWrite(outA11, HIGH); digitalWrite(outA12, LOW); motor1 = "forward";} if (cadenarecibida.indexOf("?For2")>0){ //Forward direction motor 2 digitalWrite(outB11, HIGH); digitalWrite(outB12, LOW); motor2 = "forward";} if (cadenarecibida.indexOf("?Back1")>0){ //Backward direction motor 1 digitalWrite(outA11, LOW); digitalWrite(outA12, HIGH); motor1 = "backward";} if (cadenarecibida.indexOf("?Back2")>0){ //Backward direction motor 2 digitalWrite(outB11, LOW); digitalWrite(outB12, HIGH); motor2 = "backward";} Serial.println("Motor 1 State: " + motor1 ); Serial.println("Motor 2 State: " + motor2 ); cadenarecibida=""; //Clean the received string } } } Serial.println("Usuario Desconectado"); //Print it to the terminal } }