ESP01 not able to set password

Hello! Greetings! and hope you are having good time!

Few days back with the help of @horace I wrote a code that worked perfectly fine on ESP32 board. Now I'm trying to get that same code working on ESP01 but ESP01 fails to do most of the tasks in the code.

The code basically does following things:

  1. Configures the ESP32 as soft AP
  2. Hosts a web server
  3. Controls a device connected to its GPIO (LED for now)
  4. has elegantOTA
  5. Resets SSID from the web page that is hosted on it
  6. But most important is it derives its password from MAC ID

now, after running it on ESP01 couple of times and trying to make some random but meaningful changes I found that I needed to correct the Pin numbers obviously so I changed it. Next thing I noticed that the SSID can't be longer than 5 characters and Password can't be longer than 9 characters. So, I made changes accordingly.

The password generation from MAC ID is something really important for me, which worked perfectly fine on ESP32 but doesn't seem to work as expected on ESP01.

Here's the code that I run on ESP32

//<script src="https://kit.fontawesome.com/6392e33924.js" crossorigin="anonymous"></script>

#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:,">
   <link rel="icon" href="https://kit.fontawesome.com/6392e33924.js" crossorigin="anonymous">
    <style>
    html {
     font-family: Arial;
     display: inline-block;
     margin: 0px auto;
     text-align: center;
    }
    h2 { font-size: 3.0rem; }
    p { font-size: 1.0rem; }
    .units { font-size: 0.8rem; }
    .labels{
      font-size: 1.0rem;
      //vertical-align:middle;
      padding-bottom: 15px;
    }
  </style>
 </head>
<body>
  <h2>Data On Tag</h2>
   <p>
  <i class="fa-solid fa-plug-circle-bolt"style="color:#059e8a;"></i>
    <span class="labels">Power Consumed</span> 
    <span id="power">%POWER%</span>
    <span class="units">Watts</span>
  </p>
 
<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();
}

setInterval(function ( ) 
{
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function()
  {
    if (this.readyState == 4 && this.status == 200)
    {
      document.getElementById("power").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/power", true);
  xhttp.send();
}, 3000 ) ;

</script>

<form action="/get">
    New SSID: <input type="text" name="input1">
    <input type="submit" value="Submit">
  </form><br>
<p> %BUTTONPLACEHOLDER%</p>
<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";

String outputState(int output){
  if(digitalRead(output)){
    return "checked";
  }
  else {
    return "";
  }
}

String PowerConsumed()
{
  float Pow = analogRead(0);
  Serial.print("Power");
  Serial.println(Pow);
  return String(Pow);
}

// Replaces placeholder with button section in your web page
String processor(const String& var)
{
  //Serial.println(var);
  if(var == "BUTTONPLACEHOLDER")
  {
    String buttons = "";
    buttons += "<h4>Device Status</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"2\" " + outputState(2) + "><span class=\"slider\"></span></label>";
    return buttons;
  } 

  else if(var == "POWER")
  {
    return PowerConsumed();
  }return String();
}

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);
  }
 );

 server.on("/power", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", PowerConsumed().c_str());
  });

  // 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);    
 }

and here's the code that's current;y running on ESP01. Which controls the LED and also shows the OTA webpage (I haven't tested it yet) but so far controlling the LED works fine. But not the password generation thing. Here's the code:

#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>

#define RELAYPIN 2

AsyncWebServer server(80);

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:,">
   <link rel="icon" href="https://kit.fontawesome.com/6392e33924.js" crossorigin="anonymous">
    <style>
    html {
     font-family: Arial;
     display: inline-block;
     margin: 0px auto;
     text-align: center;
    }
    h2 { font-size: 3.0rem; }
    p { font-size: 1.0rem; }
    .units { font-size: 0.8rem; }
    .labels{
      font-size: 1.0rem;
      //vertical-align:middle;
      padding-bottom: 15px;
    }
  </style>
 </head>
<body>
  <h2>Data On Tag</h2>
   <p>
  <i class="fa-solid fa-plug-circle-bolt"style="color:#059e8a;"></i>
    <span class="labels">Power Consumed</span> 
    <span id="power">%POWER%</span>
    <span class="units">Watts</span>
  </p>
 
<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();
}

setInterval(function ( ) 
{
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function()
  {
    if (this.readyState == 4 && this.status == 200)
    {
      document.getElementById("power").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/power", true);
  xhttp.send();
}, 3000 ) ;

</script>

<form action="/get">
    New SSID: <input type="text" name="input1">
    <input type="submit" value="Submit">
  </form><br>
<p> %BUTTONPLACEHOLDER%</p>
<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";

String outputState(int output){
  if(digitalRead(output)){
    return "checked";
  }
  else {
    return "";
  }
}

// Replaces placeholder with button section in your web page
String processor(const String& var)
{
  //Serial.println(var);
  if(var == "BUTTONPLACEHOLDER")
  {
    String buttons = "";
    buttons += "<h4>Device Status</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"2\" " + outputState(2) + "><span class=\"slider\"></span></label>";
    return buttons;
  } 
}

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);
  }
 );
/*
 server.on("/power", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", PowerConsumed().c_str());
  });

  // 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>/Device/off
  // Route to set GPIO to LOW
  server.on("/Device/off", HTTP_GET, [](AsyncWebServerRequest *request)
  {
    digitalWrite(RELAYPIN, LOW);    
    request->send_P(200, "text/html", index_html, processor);
    //detachInterrupt(CF_PIN);
   }
  );

  // Send a GET request to <ESP_IP>/Device/on
  // Route to set GPIO to HIGH
  server.on("/Device/on", HTTP_GET, [](AsyncWebServerRequest *request)
  {
    digitalWrite(RELAYPIN, HIGH);    
    request->send_P(200, "text/html", index_html, processor);
    //attachInterrupt(CF_PIN, ISR, RISING);
   }
  );

  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[1],HEX);
    int mac1 = mac[2];
     one = mac0;
     Serial.print("First:");
     Serial.println(one);
     Two = one + mac1;
     Serial.print("Second:");
     Serial.println(Two);
     Pass = one + "DOT";
     Serial.println(Pass);
 }
 
void setup()
{
  Serial.begin(115200);
  Serial.println();

  Serial.print("Setting soft-AP ... ");
  boolean result = WiFi.softAP("DOT01", "12345678T");
  if(result == true)
  {
    Serial.println("Ready");
  }
  else
  {
    Serial.println("Failed!");
  }

  pinMode(RELAYPIN, OUTPUT);
  digitalWrite(RELAYPIN, LOW);
     
    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()
{
  Serial.printf("Stations connected = %d\n", WiFi.softAPgetStationNum());
  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);
         //PowerCalc();
     }
     newStations= WiFi.softAPgetStationNum();
     if(stations != newStations){
      Serial.print("Connected stations ");
      Serial.println(stations=newStations);
     }
  delay(3000);
  
}

what I'm missing here?

Thanks in advance

porting code between system is always difficult - e.g. different data type sizes, different libraries, etc etc
I would not try a port directly from an ESP32 to an ESP-01 - initially port to something like a NodeMCU ESP-12 which is easier to use - when that works port to the ESP-01
Re: password - the ESP8266 was probably treating your ints as ASCII characters - use the String() constructor explicitly, e.g. String(mac[0])
also the ESP8266 soft AP documentation states that the password must be a minimum of 8 characters
try this version Create_Password_SetupAP() (using the ESP32 code from post #1) - it works on a NodeMCU ESP8266 and ESP32

void Create_Password_SetupAP()
 {
   // WiFi.begin();
    WiFi.mode(WIFI_AP);
    Serial.println("WiFi began");
    WiFi.softAPmacAddress(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");
     Pass = String(mac[0])+String(mac[1])+String(mac[2]) + "DOT";
     Serial.println(Pass);
    Serial.print("Connecting to ");  
    Serial.println(Old_ssid);  
    WiFi.softAP(Old_ssid, Pass.c_str());    
  Serial.println( "" );  
  Serial.println( "WiFi AP is now running with Old SSID" );  
  Serial.println( "IP address: " );  
  Serial.println( WiFi.softAPIP() ); 
 }

the serial monitor displays

Program Start *******************************WiFi began
MAC: 62:1:94:1F:7B:EE
Creating Password from MAC ID
981148DOT
Connecting to ESP8266-Data-On-Tag

WiFi AP is now running with Old SSID
IP address: 
192.168.4.1
Server started
Connected stations 1
Free heap 45288
Connected stations 0
Free heap 46304
Free heap 45992
Free heap 46304
Free heap 45992
Connected stations 1
AsyncWebServerRequest from client with IP: 192.168.4.2
Power6.00

and a webclient displays
new_ssid

the SSID is changed OK

1 Like

It worked thanks again! :smiley:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.