Last active
September 6, 2020 14:32
-
-
Save ghtomcat/5c3288cb2a016aa0b318ad8aabc7f516 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 <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include "Adafruit_LEDBackpack.h" | |
Adafruit_8x8matrix matrix = Adafruit_8x8matrix(); | |
static const uint8_t PROGMEM | |
smile_bmp[] = | |
{ B00111100, | |
B01000010, | |
B10100101, | |
B10000001, | |
B10100101, | |
B10011001, | |
B01000010, | |
B00111100 }; | |
// Replace with your network credentials | |
const char* ssid = "xxxxxx"; | |
const char* password = "yyyyyy"; | |
// motor instance | |
ESP32MotorControl MotorControl = ESP32MotorControl(); | |
// 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); | |
matrix.begin(0x70); // pass in the address | |
// 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); | |
matrix.clear(); | |
matrix.drawBitmap(0, 0, smile_bmp, 8, 8, LED_ON); | |
matrix.writeDisplay(); | |
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