Last active
September 6, 2020 14:32
-
-
Save ghtomcat/42b8e549abe77db4070d2fb08e6651c5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <SPI.h> | |
#include <Adafruit_LIS3DH.h> | |
#include <Adafruit_Sensor.h> | |
Adafruit_LIS3DH lis = Adafruit_LIS3DH(); | |
// 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); | |
if (! lis.begin(0x18)) { | |
Serial.println("Couldnt start"); | |
while (1) yield(); | |
} | |
Serial.println("LIS3DH found!"); | |
// 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); | |
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(); | |
lis.read(); | |
if (lis.x > 1000) { | |
MotorControl.motorsStop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment