//esp32_wrover_ftmc004_wifi
//Thanks to mukujii.sakura.ne.jp/esp1.html
//Thanks to www.freenove.com
//Thanks to https://akizukidenshi.com/download/ds/akizuki/ft-mc-002_004_appendix.pdf
#include <WiFi.h>
#include <ESP32Servo.h>
Servo RightServo;
Servo LeftServo;
Servo WiperServo;
int pos=0;
int RightPin=12;
int LeftPin=14;
int intv=150;
int WiperPin=18;
int HazardPin=13;
const char* ssid = "";
const char* password = "";
const char html[] =
"<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>\
<style>input {font-size:16pt;margin:16px;width:160px;}\
div {font-size:32pt;color:blue;text-align:center;width:600px;border:medium solid black;}</style>\
<title>EPS32 WiFi_Car</title></head>\
<body><div><p>4WD Car Controller</p>\
<form method='get'>\
<input type='submit' name='le' value='Left' />\
<input type='submit' name='fo' value='Forward' />\
<input type='submit' name='ri' value='Right' /><br>\
<input type='submit' name='hz' value='Hazard' />\
<input type='submit' name='st' value='Stop' /> \
<input type='submit' name='wp' value='Wiper' /><br>\
<input type='submit' name='lb' value='LeftBack' />\
<input type='submit' name='ba' value='Back' />\
<input type='submit' name='rb' value='RightBack' /><br><br>\
</form></div></body></html>";
WiFiServer server(80);
void ServoSet()
{
RightServo.attach(RightPin,1000,2000);
LeftServo.attach(LeftPin,1000,2000);
}
void Forward()
{
ServoSet();
for (pos = 10; pos <= 170; pos += 1)
{
RightServo.write(pos);
LeftServo.write(pos);
}
}
void MotorStop()
{
RightServo.detach();
LeftServo.detach();
}
void Back()
{
ServoSet();
for (pos = 170; pos >= 10; pos -= 1)
{
RightServo.write(pos);
LeftServo.write(pos);
}
}
void Right()
{
ServoSet();
RightServo.detach();
for (pos = 10; pos <= 170; pos += 1)
{
LeftServo.write(pos);
}
delay(intv);
MotorStop();
}
void Left()
{
ServoSet();
LeftServo.detach();
for (pos = 10; pos <= 170; pos += 1)
{
RightServo.write(pos);
}
delay(intv);
MotorStop();
}
void RightBack()
{
ServoSet();
RightServo.detach();
for (pos = 170; pos >= 10; pos -= 1)
{
LeftServo.write(pos);
}
delay(intv);
MotorStop();
}
void LeftBack()
{
ServoSet();
LeftServo.detach();
for (pos = 170; pos >= 10; pos -= 1)
{
RightServo.write(pos);
}
delay(intv);
MotorStop();
}
void Hazard()
{
for (int i=1;i<=5;i +=1)
{
digitalWrite(HazardPin,HIGH);
delay(300);
digitalWrite(HazardPin,LOW);
delay(300);
}
}
void Wiper()
{
for (int i=1;i<=5;i +=1)
{
for (pos = 20; pos <= 160; pos += 10)
{
WiperServo.write(pos);
delay(15);
}
for (pos = 160; pos >= 20; pos -= 10)
{
WiperServo.write(pos);
delay(15);
}
}
}
void setup()
{
pinMode(HazardPin,OUTPUT);
Serial.begin(115200);
RightServo.attach(RightPin,1000,2000);
LeftServo.attach(LeftPin,1000,2000);
WiperServo.attach(WiperPin,1000,2000);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available();
if (client) {
Serial.println("New Client.");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print(html);
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
if (currentLine.endsWith("GET /?fo")) {
Forward();
}
if (currentLine.endsWith("GET /?le")) {
Left();
}
if (currentLine.endsWith("GET /?ri")) {
Right();
}
if (currentLine.endsWith("GET /?ba")) {
Back();
}
if (currentLine.endsWith("GET /?lb")) {
LeftBack();
}
if (currentLine.endsWith("GET /?rb")) {
RightBack();
}
if (currentLine.endsWith("GET /?st")) {
MotorStop();
}
if (currentLine.endsWith("GET /?hz")) {
Hazard();
}
if (currentLine.endsWith("GET /?wp")) {
Wiper();
}
}
}
client.stop();
Serial.println("Client Disconnected.");
}
}