更新 web

This commit is contained in:
Luthics 2022-11-12 21:34:51 +08:00
parent 6de4a33e5c
commit 0c6d9e0b23
7 changed files with 144 additions and 3 deletions

View File

@ -1,5 +1,6 @@
{
"files.associations": {
"*.tcc": "cpp"
"*.tcc": "cpp",
"typeinfo": "cpp"
}
}

8
include/web.h Normal file
View File

@ -0,0 +1,8 @@
#include <AsyncTCP.h>
#include <DNSServer.h>
#include <WiFi.h>
#include "ESPAsyncWebServer.h"
void web_init();
void web_loop();
void ref_ret(AsyncWebServerRequest* request, String html);

18
index.html Normal file
View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>Servo</title>
</head>
<body>
<form action='servo' method='post'>
<button name='deg' value='0'>0</button>
<button name='deg' value='90'>90</button>
<button name='deg' value='180'>180</button>
</form>
<form action='servo' method='post'>
<input name='deg' />
<button name='submit' value='submit'>设置</button>
</form>
</body>

View File

@ -15,4 +15,5 @@ framework = arduino
lib_deps =
madhephaestus/ESP32Servo@^0.12.0
h2zero/NimBLE-Arduino@^1.4.1
ottowinter/ESPAsyncWebServer-esphome@^3.0.0
monitor_speed = 115200

View File

@ -1,15 +1,18 @@
#include <Arduino.h>
#include <servo.h>
#include <bt.h>
#include <web.h>
void setup() {
Serial.begin(115200);
servo_init();
bt_init();
web_init();
}
void loop() {
servo_loop();
bt_loop();
web_loop();
delay(100);
}

View File

@ -6,12 +6,11 @@ Servo servo[8];
ESP32PWM pwm;
int servoPin[8] = {13, 12, 14, 26, 25, 33, 32, 15};
int pos[8] = {0};
int minUs = 500;
int maxUs = 2500;
int pos = 0;
void servo_init() {
for (int i = 0; i < 8; i++) {
ESP32PWM::allocateTimer(i);
@ -21,6 +20,7 @@ void servo_init() {
servo[i].attach(servoPin[i], minUs, maxUs);
}
pwm.attachPin(27, 10000);
servo_deg(8, 0);
}
void servo_loop() {
@ -62,10 +62,12 @@ void servo_deg(int servoid, int deg) {
if (servoid >= 8) {
for (int i = 0; i < 8; i++) {
servo[i].write(deg);
pos[i] = deg;
Serial.println(deg);
}
} else {
servo[servoid].write(deg);
pos[servoid] = deg;
Serial.println(deg);
}
}

108
src/web.cpp Normal file
View File

@ -0,0 +1,108 @@
#include <Arduino.h>
#include <servo.h>
#include <web.h>
#include <AsyncTCP.h>
#include <DNSServer.h>
#include <WiFi.h>
#include "ESPAsyncWebServer.h"
AsyncWebServer server(80);
const char* ssid = "Luthics' WLAN";
const char* password = "qwerasdf";
const char* PARAM_MESSAGE = "foo";
void notFound(AsyncWebServerRequest* request) {
request->send(404, "text/plain", "Not found");
}
void web_init() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
return;
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
String mp =
"<!DOCTYPE html>\
<html>\
<head>\
<meta charset=\"utf-8\">\
<title>Servo</title>\
</head>\
<body>\
<form action='servo' method='post'>\
<button name='deg' value='0'>0</button>\
<button name='deg' value='90'>90</button>\
<button name='deg' value='180'>180</button>\
</form>\
<form action='servo' method='post'>\
<input name='deg' />\
<button name='submit' value='submit'></button>\
</form>\
</body>";
request->send(200, "text/html", mp);
});
// Send a GET request to <IP>/get?message=<message>
// server.on("/get", HTTP_GET, [](AsyncWebServerRequest* request) {
// String message;
// if (request->hasParam(PARAM_MESSAGE)) {
// message = request->getParam(PARAM_MESSAGE)->value();
// } else {
// message = "No message sent";
// }
// request->send(200, "text/plain", "Hello, GET: " + message);
// });
// Send a POST request to <IP>/post with a form field message set to <message>
// server.on("/post", HTTP_POST, [](AsyncWebServerRequest* request) {
// String message;
// if (request->hasParam(PARAM_MESSAGE, true)) {
// message = request->getParam(PARAM_MESSAGE, true)->value();
// } else {
// message = "No message sent";
// }
// request->send(200, "text/plain", "Hello, POST: " + message);
// });
server.on("/servo", HTTP_POST, [](AsyncWebServerRequest* request) {
int deg;
int servoid;
if (request->hasParam("deg", true)) {
deg = request->getParam("deg", true)->value().toInt();
if (request->hasParam("id", true)) {
servoid = request->getParam("id", true)->value().toInt();
} else {
servoid = 8;
}
servo_deg(servoid, deg);
ref_ret(
request,
"<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" "
"content=\"1; "
"url='http://192.168.6.9'\" /></head><body><h1>Success. Back after 1 "
"sencond.</h1></body></html>");
} else {
notFound(request);
}
});
server.onNotFound(notFound);
server.begin();
}
void web_loop() {}
void ref_ret(AsyncWebServerRequest* request, String html) {
request->send(200, "text/html", html);
}