Hello Everyone
I have set an ESP32 as an access point to connect and control a robotic seed-sowing car. The problem is that the client side keeps disconnecting from the ESP32's Wi-Fi access point, either for a few seconds or when I press a button or input data. Currently, I'm having difficulty figuring out the problem. The ESP32 is powered by 2 18650 batteries, 3350 mAh. I also use this to power 2 servos and 2 drills. Attached is the code that I am using. One post that I saw that could be the cause is that there are to many feedback from server side which causes a voltage drop when too much request are sent and automatically reboots the ESP32, I am not sure if this is the case.
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#else
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>
#include <ESP32Servo.h>
// REPLACE WITH YOUR NETWORK CREDENTIALS
const char* ssid = "TESTSERVER";
const char* password = "12345678";
//Pinouts and Naming
const int ENA = 13;
const int IN_1 = 12;
const int IN_2 = 14;
const int IN_3 = 27;
const int IN_4 = 26;
const int ENB = 25;
const char* PARAM_INPUT_1 = "Speed";
const char* PARAM_INPUT_2 = "Distance";
const int Servo1 = 32;
const int Servo2 = 33;
const int IN_2_STEP = 21;
const int IN_3_STEP = 19;
const int IN_4_STEP = 18;
//Variables
int CarSpeed = 150; //Default 0 - 255
float distance = 0;
float spacing = 5;
float displacement = 0;
float wait = 100;
int angle = 0;
bool DriveCheck = false;
Servo servo;
Servo myservo;
// HTML web page
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<title>WINSOW ROBOT</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial; text-align: center; margin:0px auto; padding-top: 30px;}
.button {
padding: 10px 20px;
font-size: 24px;
text-align: center;
outline: none;
color: #fff;
background-color: #2f4468;
border: none;
border-radius: 5px;
box-shadow: 0 6px #999;
cursor: pointer;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
.button:hover {background-color: #1f2e45}
.button:active {
background-color: #1f2e45;
box-shadow: 0 4px #666;
transform: translateY(2px);
}
</style>
</head>
<body>
<table width="100%" border="1">
<tr>
<td id="JD">WINSOW CONTROLLER</td>
</tr>
</table>
<table width="100%" border="0" align="center">
<tr>
<td></td>
<td align="center"><button class="button" onmousedown="toggleCheckbox('F');" ontouchstart="toggleCheckbox('F');" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">FORWARD</button></td>
<td></td>
</tr>
<tr>
<td align="center"><button class="button" onmousedown="toggleCheckbox('L');" ontouchstart="toggleCheckbox('L');" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">LEFT</button></td>
<td align="center"></td>
<td align="center"><button class="button" onmousedown="toggleCheckbox('R');" ontouchstart="toggleCheckbox('R');" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">RIGHT</button></td>
</tr>
<tr>
<td></td>
<td align="center"><button class="button" onmousedown="toggleCheckbox('B');" ontouchstart="toggleCheckbox('B');" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">BACKWARD</button></td>
<td></td>
</tr>
</table>
<form action="/get">
Speed(0-255): <input type="text" name="Speed">
<input type="submit" value="Submit">
</form><br>
<form action="/get">
Seed Distancing(m): <input type="text" name="Distance">
<input type="submit" value="Submit">
</form><br>
<script>
function toggleCheckbox(x) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/" + x, true);
xhr.send();
}
</script>
</body>
</html>)rawliteral";
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
AsyncWebServer server(80);
void forward(){ //forword
digitalWrite(IN_1, HIGH); //Right Motor forword Pin
digitalWrite(IN_2, LOW); //Right Motor backword Pin
digitalWrite(IN_3, LOW); //Left Motor backword Pin
digitalWrite(IN_4, HIGH); //Left Motor forword Pin
}
void backward(){ //backword
digitalWrite(IN_1, LOW); //Right Motor forword Pin
digitalWrite(IN_2, HIGH); //Right Motor backword Pin
digitalWrite(IN_3, HIGH); //Left Motor backword Pin
digitalWrite(IN_4, LOW); //Left Motor forword Pin
}
void turnRight(){ //turnRight
digitalWrite(IN_1, HIGH); //Right Motor forword Pin
digitalWrite(IN_2, LOW); //Right Motor backword Pin
digitalWrite(IN_3, HIGH); //Left Motor backword Pin
digitalWrite(IN_4, LOW); //Left Motor forword Pin
}
void turnLeft(){ //turnLeft
digitalWrite(IN_1, LOW); //Right Motor forword Pin
digitalWrite(IN_2, HIGH); //Right Motor backword Pin
digitalWrite(IN_3, LOW); //Left Motor backword Pin
digitalWrite(IN_4, HIGH); //Left Motor forword Pin
}
void Distancer(){
displacement = displacement+(1.85857*(pow(10,-3))*CarSpeed)*(wait*(pow(10,-3)));
distance = distance+(1.85857*(pow(10,-3))*CarSpeed)*(wait*(pow(10,-3)));
Serial.println(displacement);
Serial.println(distance);
Serial.println(spacing);
}
void Drill(){
digitalWrite(IN_3_STEP,HIGH);
delay(2000);
digitalWrite(IN_2_STEP,HIGH);
digitalWrite(IN_3_STEP,LOW);
delay(4000);
digitalWrite(IN_2_STEP,LOW);
digitalWrite(IN_4_STEP,HIGH);
}
void ServoMotor(){
//if (Serial.available()) {
//int input = Serial.parseInt(); // Read integer from serial
//if (input>=0 && input<=180) {
angle = 120;
delay(20);
servo.write(angle);
myservo.write(180-angle);
Drill();
angle = 0;
servo.write(angle);
myservo.write(180-angle);
delay(2000);
digitalWrite(IN_4_STEP,LOW);
/*}else{
Serial.println("Doesn't Fit 0 to 180");
}
}*/
displacement = 0;
// Clear any leftover characters
while (Serial.available()) {
Serial.read();
}
}
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println (">> Setup");
WiFi.mode(WIFI_AP); //Only Access point
WiFi.softAP(ssid, password); //Start HOTspot removing password will disable security
IPAddress myIP = WiFi.softAPIP(); //Get IP address
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(myIP);
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(IN_3, OUTPUT);
pinMode(IN_4, OUTPUT);
pinMode(IN_2_STEP, OUTPUT);
pinMode(IN_3_STEP, OUTPUT);
pinMode(IN_4_STEP, OUTPUT);
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, LOW);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, LOW);
digitalWrite(IN_2_STEP, LOW);
digitalWrite(IN_3_STEP, LOW);
digitalWrite(IN_4_STEP, LOW);
servo.attach(Servo1);
servo.write(angle);
myservo.attach(Servo2);
myservo.write(180-angle);
// Send web page to client
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/html", index_html);
});
// Receive an HTTP GET request
server.on("/F", HTTP_GET, [] (AsyncWebServerRequest *request) {
forward();
DriveCheck = true;
request->send(200, "text/plain", "ok");
});
// Receive an HTTP GET request
server.on("/L", HTTP_GET, [] (AsyncWebServerRequest *request) {
turnLeft();
request->send(200, "text/plain", "ok");
});
// Receive an HTTP GET request
server.on("/R", HTTP_GET, [] (AsyncWebServerRequest *request) {
turnRight();
request->send(200, "text/plain", "ok");
});
// Receive an HTTP GET request
server.on("/B", HTTP_GET, [] (AsyncWebServerRequest *request) {
backward();
request->send(200, "text/plain", "ok");
});
// Receive an HTTP GET request
server.on("/off", HTTP_GET, [] (AsyncWebServerRequest *request) {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, LOW);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, LOW);
DriveCheck = false;
request->send(200, "text/plain", "ok");
});
// Send a GET request to <ESP_IP>/get?input1=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
String inputParam;
// GET input1 value on <ESP_IP>/get?input1=<inputMessage>
if (request->hasParam(PARAM_INPUT_1)) {
inputMessage = request->getParam(PARAM_INPUT_1)->value();
inputParam = PARAM_INPUT_1;
CarSpeed=inputMessage.toInt();
Serial.println(CarSpeed);
}
// GET input2 value on <ESP_IP>/get?input2=<inputMessage>
else if (request->hasParam(PARAM_INPUT_2)) {
inputMessage = request->getParam(PARAM_INPUT_2)->value();
inputParam = PARAM_INPUT_2;
spacing=inputMessage.toInt();
Serial.println(inputParam);
}
else {
inputMessage = "No message sent";
inputParam = "none";
}
Serial.println(inputMessage);
request->send(200, "text/html", "HTTP GET request sent to your ESP on input field ("
+ inputParam + ") with value: " + inputMessage +
"<br><a href=\"/\">Return to Home Page</a>");
});
server.onNotFound(notFound);
server.begin();
}
void loop() {
delay(wait);
Serial.println(CarSpeed);
analogWrite(ENA,CarSpeed);
analogWrite(ENB,CarSpeed);
if(spacing<=displacement){
delay(wait);
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, LOW);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, LOW);
ServoMotor();
}
while(DriveCheck==true){
delay(wait);
Distancer();
}
}