Hello! Greetings!
Currently I'm trying work with Elegant OTA for OTA with my ESP32 project. But the code uploading doesn't proceeds beyond 90%. Does it takes longer to upload the code successfully?
Here's the code that I'm using:
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#else
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
AsyncWebServer server(80);
const char* Old_ssid = "ESP32-Data-On-Tag";// This is the SSID that ESP32 will broadcast
const char* password;
char ssid[64]={0};
void Reset_SSID(char* ssid);
const char* PARAM_INPUT1 = "input1";
const char* PARAM_INPUT_1 = "output";
const char* PARAM_INPUT_2 = "state";
const char* LED_ON = "ON";
const char* LED_OFF = "OFF";
String DeviceState = "off";
byte mac[6];
String one, Two, Pass;
// HTML web page to handle 3 input fields (input1, input2, input3)
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<title>ESP Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
</head>
<body>
<h2>ESP Web Server</h2>
%BUTTONPLACEHOLDER%
<script>
function toggleCheckbox(element)
{
var xhr = new XMLHttpRequest();
if(element.checked){ xhr.open("GET", "/update?output="+element.id+"&state=1", true); }
else { xhr.open("GET", "/update?output="+element.id+"&state=0", true); }
xhr.send();
}
</script>
<form action="/get">
inputString: <input type="text" name="input1">
<input type="submit" value="Submit">
</form><br>
<p><a href="/Device/on"><button>Turn ON</button></a></p>
<p><a href="/Device/off"><button>Turn OFF</button></a></p>
</body>
</html>
)rawliteral";
// Replaces placeholder with button section in your web page
String processor(const String& var)
{
//Serial.println(var);
if(var == "BUTTONPLACEHOLDER"){
String buttons = "";
buttons += "<h4>Output - GPIO 2</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"2\" " + outputState(2) + "><span class=\"slider\"></span></label>";
return buttons;
} return String();
}
String outputState(int output){
if(digitalRead(output)){
return "checked";
}
else {
return "";
}
}
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
void setupServer(void)
{
// Send web page with input fields to client
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{
Serial.print("AsyncWebServerRequest from client with IP: ");
Serial.println(request->client()->remoteIP());
request->send_P(200, "text/html", index_html, processor);
}
);
// 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_INPUT1))
{
inputMessage = request->getParam(PARAM_INPUT1)->value();
inputParam = PARAM_INPUT1;
}
Serial.println(inputMessage);
//ssid = char(inputMessage);
inputMessage.toCharArray(ssid,64);
Serial.print("New SSID is:");
Serial.println(ssid);
// Reset_SSID(ssid); // <<<<<<<<<<<<<<<<<<<<< moved into loop()
}
);
/*
// Send a GET request to <ESP_IP>/update?output=<inputMessage1>&state=<inputMessage2>
server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request)
{
String inputMessage1;
String inputMessage2;
// GET input1 value on <ESP_IP>/update?output=<inputMessage1>&state=<inputMessage2>
if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT_2))
{
inputMessage1 = request->getParam(PARAM_INPUT_1)->value();
inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
digitalWrite(inputMessage1.toInt(), inputMessage2.toInt());
}
else
{
inputMessage1 = "No message sent";
inputMessage2 = "No message sent";
}
Serial.print("GPIO: ");
Serial.print(inputMessage1);
Serial.print(" - Set to: ");
Serial.println(inputMessage2);
request->send(200, "text/plain", "OK");
}
);
*/
// Send a GET request to <ESP_IP>/Device/off
// Route to set GPIO to LOW
server.on("/Device/off", HTTP_GET, [](AsyncWebServerRequest *request)
{
digitalWrite(2, LOW);
request->send_P(200, "text/html", index_html, processor);
}
);
// Send a GET request to <ESP_IP>/Device/on
// Route to set GPIO to HIGH
server.on("/Device/on", HTTP_GET, [](AsyncWebServerRequest *request)
{
digitalWrite(2, HIGH);
request->send_P(200, "text/html", index_html, processor);
}
);
server.onNotFound(notFound);
// Start our ESP32 server
server.begin();
}
void Create_Password_SetupAP()
{
WiFi.begin();
Serial.println("WiFi began");
WiFi.macAddress(mac);
Serial.print("MAC: ");
Serial.print(mac[0],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.println(mac[5],HEX);
Serial.println("Creating Password from MAC ID");
int mac0 = mac[0];
int mac1 = mac[1];
one = mac0;
Two = one + mac1;
Pass = Two + "DOT";
Serial.println(Pass);
password = Pass.c_str();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.softAP(Old_ssid, password);
Serial.println( "" );
Serial.println( "WiFi AP is now running with Old SSID" );
Serial.println( "IP address: " );
Serial.println( WiFi.softAPIP() );
/*
WiFi.begin();
Serial.println("WiFi began");
Serial.print("Connecting to ");
Serial.println(Old_ssid);
// WiFi.softAP(Old_ssid, password);
WiFi.softAP(Old_ssid);
Serial.print( "WiFi AP is now running IP address: " );
Serial.println( WiFi.softAPIP() ); */
}
void Reset_SSID(char* ssid)
{
//server.end();
// delay(500);
WiFi.softAPdisconnect (true);
delay(500);
// WiFi.disconnect(); // **************** added
// delay(500);
Serial.print("Connecting to New SSID: ");
Serial.println(ssid);
WiFi.softAP(ssid);//, password);
Serial.println( "WiFi AP is now running with New SSID" );
Serial.print( "IP address: " );
Serial.println( WiFi.softAPIP() );
// server.begin();
ssid[0]=0;
}
void setup()
{
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
Serial.begin(115200);
delay(1000);
Serial.println();
Serial.print("Program Start *******************************");
Create_Password_SetupAP();
setupServer();
server.onNotFound(notFound);
// Start our ESP32 server
AsyncElegantOTA.begin(&server); // Start ElegantOTA
server.begin();
Serial.println("Server started");
}
void loop()
{
static long startTime = millis(), heap=0, freeHeap=0, stations=0, newStations=0;
freeHeap=ESP.getFreeHeap();
if((millis()-startTime>30000) && (heap != freeHeap)) {
startTime=millis();
Serial.print("Free heap ");
Serial.println(heap=freeHeap);
}
newStations= WiFi.softAPgetStationNum();
if(stations != newStations){
Serial.print("Connected stations ");
Serial.println(stations=newStations);
}
if(Serial.available()) {
Serial.readBytesUntil('\n', ssid, 64);
Reset_SSID(ssid);
stations=0;
}
if(ssid[0]!=0)
Reset_SSID(ssid);
}
Thanks in advance