Created
September 6, 2020 14:42
-
-
Save ghtomcat/493a9077cb2edb59150334c4513b8960 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************* | |
A sketch for controlling a robot with using a web page | |
hosted on a ESP32 | |
based on: https://github.com/witnessmenow/simple-wifi-controlled-rc-car | |
********************************************************************/ | |
// Load Wi-Fi library | |
#include <WiFi.h> | |
#include <WebServer.h> | |
// load motor library | |
#include "ESP32MotorControl.h" | |
#include <ESP32Servo.h> | |
// Replace with your network credentials | |
const char* ssid = "xxxxxx"; | |
const char* password = "yyyyyy"; | |
// motor instance | |
ESP32MotorControl MotorControl = ESP32MotorControl(); | |
Servo myservo; // create servo object to control a servo | |
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33 | |
int servoPin = 25; | |
// Set web server port number to 80 | |
WebServer server(80); | |
const char *webpage = | |
#include "motorPage.h" | |
; | |
void handleRoot() { | |
server.send(200, "text/html", webpage); | |
} | |
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 setup(void){ | |
Serial.begin(115200); | |
// Attach 2 motors | |
MotorControl.attachMotors(12, 14, 27, 26); | |
ESP32PWM::allocateTimer(0); | |
ESP32PWM::allocateTimer(1); | |
ESP32PWM::allocateTimer(2); | |
ESP32PWM::allocateTimer(3); | |
myservo.setPeriodHertz(50); // standard 50 hz servo | |
myservo.attach(servoPin, 200, 2000); // attaches the servo to the servo object | |
// using default min/max of 1000us and 2000us | |
// different servos may require different min/max settings | |
// for an accurate 0 to 180 sweep | |
// Connect to Wi-Fi network with SSID and password | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
// Print local IP address and start web server | |
Serial.println(""); | |
Serial.println("WiFi connected."); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
server.begin(); | |
server.on("/", handleRoot); | |
server.on("/forward", [](){ | |
Serial.println("forward"); | |
MotorControl.motorForward(0, 100); | |
MotorControl.motorForward(1, 100); | |
myservo.write(90); | |
delay(15); | |
myservo.write(45); | |
server.send(200, "text/plain", "forward"); | |
}); | |
server.on("/driveStop", [](){ | |
Serial.println("driveStop"); | |
MotorControl.motorsStop(); | |
server.send(200, "text/plain", "driveStop"); | |
}); | |
server.on("/back", [](){ | |
Serial.println("back"); | |
MotorControl.motorReverse(0, 100); | |
MotorControl.motorReverse(1, 100); | |
server.send(200, "text/plain", "back"); | |
}); | |
server.on("/right", [](){ | |
Serial.println("right"); | |
MotorControl.motorForward(0, 100); | |
MotorControl.motorReverse(1, 100); | |
server.send(200, "text/plain", "right"); | |
}); | |
server.on("/left", [](){ | |
Serial.println("left"); | |
MotorControl.motorForward(1, 100); | |
MotorControl.motorReverse(0, 100); | |
server.send(200, "text/plain", "left"); | |
}); | |
server.on("/steerStop", [](){ | |
Serial.println("steerStop"); | |
MotorControl.motorsStop(); | |
server.send(200, "text/plain", "steerStop"); | |
}); | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
Serial.println("HTTP Server Started"); | |
} | |
void loop(void){ | |
server.handleClient(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment