Created
June 29, 2021 17:13
-
-
Save FreeFly19/8f824b923d1292626eaae997cf318c42 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
#include <WiFi.h> | |
#include <WiFiAP.h> | |
#include <DNSServer.h> | |
#include <ESPAsyncWebServer.h> | |
#include <ArduinoJson.h> | |
#include <AsyncJson.h> | |
#define index_html "<!doctype html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>Document</title></head><body><div style=\"display: flex; justify-content: space-between; padding-top: 100px;\"><div style=\"display: flex; width: 200px;transform: rotate(-90deg);\"><input type=\"range\" min=\"-1\" max=\"1\" step=\"0.1\" value=\"0\" id=\"throttle\"></div><div style=\"display: flex; width: 200px;\"><input type=\"range\" min=\"-1\" max=\"1\" step=\"1\" value=\"0\" id=\"steering-wheel\"></div></div><script>const throttleEl = document.getElementById('throttle');const steeringWheelEl = document.getElementById('steering-wheel');function sendState() {console.log(throttleEl.value, steeringWheelEl.value);fetch('/api/state', {method: \"POST\", headers: {'Content-Type': 'application/json'}, body: JSON.stringify({throttle: +throttleEl.value, steeringWheel: +steeringWheelEl.value})}).then(res => console.log(res));}throttleEl.oninput = () => setTimeout(sendState, 0);steeringWheelEl.oninput = () => setTimeout(sendState, 0);</script></body></html>" | |
const int LED_PIN = 22; | |
const int LEFT_PIN = 19; | |
const int RIGHT_PIN = 23; | |
const int FORWARD_PIN = 18; | |
const int BACKWARD_PIN = 5; | |
IPAddress apIP(192, 168, 0, 1); | |
AsyncWebServer server(80); | |
DNSServer dnsServer; | |
float throttleState = 0; | |
float steeringWheelState = 0; | |
void setup() { | |
Serial.begin(9600); | |
WiFi.disconnect(); | |
WiFi.mode(WIFI_OFF); | |
WiFi.mode(WIFI_AP); | |
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); | |
WiFi.softAP("WiFiCar"); | |
dnsServer.start(53, "*", apIP); | |
pinMode(LED_PIN, OUTPUT); | |
ledcSetup(0, 50, 8); | |
ledcAttachPin(FORWARD_PIN, 0); | |
ledcSetup(1, 50, 8); | |
ledcAttachPin(BACKWARD_PIN, 1); | |
// pinMode(FORWARD_PIN, OUTPUT); | |
// pinMode(BACKWARD_PIN, OUTPUT); | |
pinMode(LEFT_PIN, OUTPUT); | |
pinMode(RIGHT_PIN, OUTPUT); | |
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ | |
request->send(200, "text/html", index_html); | |
}); | |
AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/api/state", [](AsyncWebServerRequest *request, JsonVariant &json) { | |
StaticJsonDocument<128> jsonObj = json.as<JsonObject>(); | |
throttleState = jsonObj["throttle"].as<float>(); | |
steeringWheelState = jsonObj["steeringWheel"].as<float>(); | |
//Serial.println(throttleState); | |
request->send(200, "application/json", "{}"); | |
}); | |
server.addHandler(handler); | |
server.begin(); | |
} | |
void loop() { | |
dnsServer.processNextRequest(); | |
digitalWrite(LED_PIN, !digitalRead(LED_PIN)); | |
if (throttleState < 0) { | |
ledcWrite(0, (int)(throttleState * 255)); | |
ledcWrite(1, 0); | |
} else if (throttleState > 0) { | |
ledcWrite(1, (int)(-throttleState * 255)); | |
ledcWrite(0, 0); | |
} else { | |
ledcWrite(0, 0); | |
ledcWrite(1, 0); | |
} | |
if (steeringWheelState > 0) { | |
digitalWrite(LEFT_PIN, HIGH); | |
digitalWrite(RIGHT_PIN, LOW); | |
} else if (steeringWheelState < 0) { | |
digitalWrite(LEFT_PIN, LOW); | |
digitalWrite(RIGHT_PIN, HIGH); | |
} else { | |
digitalWrite(LEFT_PIN, LOW); | |
digitalWrite(RIGHT_PIN, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment