/*************************************************** Autor: Ingmar Stapel Website: www.custom-build-robots.com Date: 2020-04-21 Description: This program will enable you robot car to be controlled via WIFI and a small HTML control center. Java Script added by John and Paul Apfelbaum, July 2020 Enables Key commanding (WASD) and robot stops when key or button is released. Website: www.linuxslate.com ****************************************************/ #include #include #include #define pwm_A 15 #define pwm_B 12 #define dir_A 14 #define dir_B 13 #define batt_sense 0 #define client_name "Robot Control" #define dBm 17.5 #define lowCellVolts 3.7 #define battCells 3 WiFiEventHandler disconnectedEventHandler; // Set the WIFI SSID and password // SSID of your WIFI const char* ssid = "AP SSID"; //password of your WIFI const char* password = "AP Password"; // Set the default speed int motor_speed = 1024; // Start the web server ESP8266WebServer server(80); void setup() { // set the serial speed Serial.begin(115200); pinMode(LED_BUILTIN, OUTPUT); pinMode(pwm_A, OUTPUT); // PMW A pinMode(pwm_B, OUTPUT); // PMW B pinMode(dir_A, OUTPUT); // DIR A pinMode(dir_B, OUTPUT); // DIR B pinMode(batt_sense,INPUT); //Battery Monitor Voltage Divider Pin WiFi.persistent(false); WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_STA); WiFi.hostname(client_name); // connect to your local WIFI Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); // connecting to wifi WiFi.begin(ssid, password); WiFi.setOutputPower(dBm); bool setPhyMode (WiFiPhyMode_t WIFI_PHY_MODE_11N); // while wifi not connected while (WiFi.status() != WL_CONNECTED) { pinMode(LED_BUILTIN, LOW); delay(250); pinMode(LED_BUILTIN, HIGH); delay(250); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Print the IP address Serial.println(WiFi.localIP()); disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) { Serial.println("Station disconnected, Motors stopped"); analogWrite(pwm_A, 0); analogWrite(pwm_B, 0); }); server.on("/", sendPage); server.on("/car", handleCar); server.on("/inline", []() { server.send(200, "text/plain", "this works as well"); }); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); } void sendPage() { String message = ""; // Send main page which will be displayed as control center message += " Telepresence Robot"; message += "

Telepresence Robot Control

"; message += ""; message += "
"; message += " "; message += ""; message += ""; message += ""; message += "

"; message += ""; message += "

"; message += "

"; message += "

"; message += "

"; message += "

"; message += "

"; message += "

"; message += "

"; message += "
"; message += "
"; message += ""; message += ""; message += ""; message += ""; message += "STOP"; message += "
"; message += ""; server.send(200, "text/html", message); } // handle each request and detect which button was pressed. void handleCar() { String message = ""; //Read RSSI to later send to the control page long rssi = WiFi.RSSI(); //Read and convert battery voltage int sensorValue = analogRead(A0); //read the A0 pin value float volts = sensorValue * (5.00 / 1023.00) * 4.37; //convert the value to a true voltage. //changes voltage color to red when battery is low String voltsColor = "black"; if (volts < lowCellVolts * battCells) { voltsColor = "red"; } int BtnValue = 0; for (uint8_t i = 0; i < server.args(); i++) { if (server.argName(i) == "a") { String s = server.arg(i); BtnValue = s.toInt(); } Serial.println(server.argName(i) + ": " + server.arg(i) + "\n"); } switch (BtnValue) { case 1: //do nothing, the speed of the left motor will be displayed. break; case 2: // accelerate forward analogWrite(pwm_A, motor_speed); analogWrite(pwm_B, motor_speed); digitalWrite(dir_A, LOW); digitalWrite(dir_B, HIGH); Serial.println("Foward"); break; case 3:// do nothing, the speed of the right motor will be displayed. break; case 4:// turn left analogWrite(pwm_A, motor_speed); analogWrite(pwm_B, motor_speed); digitalWrite(dir_A, HIGH); digitalWrite(dir_B, HIGH); Serial.println("Turn Left"); break; case 5: // stop the left and right motor analogWrite(pwm_A, 0); analogWrite(pwm_B, 0); Serial.println("Stop"); break; case 6:// right analogWrite(pwm_A, motor_speed); analogWrite(pwm_B, motor_speed); digitalWrite(dir_A, LOW); digitalWrite(dir_B, LOW); Serial.println("Turn Right"); break; case 7:// do nothing and display nothing break; case 8:// backward analogWrite(pwm_A, motor_speed); analogWrite(pwm_B, motor_speed); digitalWrite(dir_A, HIGH); digitalWrite(dir_B, LOW); Serial.println("Reverse"); break; case 9:// do nothing and display nothing break; } //stuff that goes in the rssi iframe message += " ESP8266 NodeMCU robot car"; message += ""; message += "Signal: "; message += rssi; message += " dB        Battery: "; message += "" + volts + " V"; message += ""; server.send(200, "text/html", message); } // if something went wrong.... void handleNotFound() { String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET) ? "GET" : "POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); } void loop() { server.handleClient(); }