ESP32 wifi library

Thread Starter

zazas321

Joined Nov 29, 2015
848
Hey. I am experimenting with ESP32 wifi Station and access point modes. I am noticing a lot of confusing things and cannot get my head arround how to work it out. I will try to explain my findings as clear as possible hopefully someone with little bit more knowledge can help me troubleshoot these issues.

1.So I have started with this example project:
https://randomnerdtutorials.com/esp32-esp8266-input-data-html-form/
Configuring my ESP32 as station mode, connecting to existing wifi and sending data from my computer to ESP32. Everything works perfectly fine!

Now I would like to add 1 feature to this example. If the entered wifi credentials are wrong or has been changed, start th ESP32 in access point mode. I can connect to this access point on my computer and type in the new wifi credentials. Save them in eeprom, and then the ESP32 will sucessfully connect to the wifi. ( I know there are Wifi manager libraries but I have tried them and could not fully understand how they work therefore I have tried to implement something very simple myself).

2. Before I try to mix station and AP modes in a single sketch, I have decided to first try the access point and see how it behaves. All I did is just changed the wifi mode and started soft AP. The full code:

Code:
/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-input-data-html-form/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/

#include <Arduino.h>

  #include <WiFi.h>
  #include <AsyncTCP.h>

#include <ESPAsyncWebServer.h>

AsyncWebServer server(80);

#define MODE_ACCESS_POINT 0
#define MODE_NORMAL       1

// REPLACE WITH YOUR NETWORK CREDENTIALS
const char* ssid = "Pick_to_light";
const char* password = "teltonika20";

const char* PARAM_INPUT_1 = "input1";
const char* PARAM_INPUT_2 = "input2";
const char* PARAM_INPUT_3 = "input3";

// HTML web page to handle 3 input fields (input1, input2, input3)
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
  <title>ESP Input Form</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  </head><body>
  <form action="/get">
    input1: <input type="text" name="input1">
    <input type="submit" value="Submit">
  </form><br>
  <form action="/get">
    input2: <input type="text" name="input2">
    <input type="submit" value="Submit">
  </form><br>
  <form action="/get">
    input3: <input type="text" name="input3">
    <input type="submit" value="Submit">
  </form>
</body></html>)rawliteral";

void notFound(AsyncWebServerRequest *request) {
  request->send(404, "text/plain", "Not found");
}
/*
void setup() {
 Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi Failed!");
    return;
  }
  Serial.println();
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
 */ 

  void setup() {
  Serial.begin(115200);
  //WiFi.persistent(false);
  WiFi.mode(WIFI_AP);
  WiFi.softAP("ESPNOW_test", "esp");
  WiFi.softAPdisconnect(false);// when called with false, creates an old access point ? HOW?
  Serial.print("MAC address of this node is ");
  Serial.println(WiFi.softAPmacAddress());
  Serial.println();
  Serial.print("IP address: ");
  Serial.println(WiFi.softAPIP());

  

  // Send web page with input fields to client
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html);
  });

  // 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;
    }
    // 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;
    }
    // GET input3 value on <ESP_IP>/get?input3=<inputMessage>
    else if (request->hasParam(PARAM_INPUT_3)) {
      inputMessage = request->getParam(PARAM_INPUT_3)->value();
      inputParam = PARAM_INPUT_3;
    }
    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() {

}

Now thats where the confusing things start. my guess is that something might be worng with the wifi persistent function?:
//WiFi.persistent(false);

After programming the device, the new wifi network appears on the list:
1605255936728.png


Clearly thats not the network that I have just created since its a different name? How does that happen? Perhaps I have made this access point some other time a while a go and it somehow saved the configuration? How can I complete wipe the wifi configuration settings? Can someone help me understand how this libraray is supposed to be used in access point mode correctly? I appreciate any help
 

Attachments

Thread Starter

zazas321

Joined Nov 29, 2015
848
UPDATE:

I have managed to create a proper access point by modifying the code as following:
Code:
  //WiFi.softAPdisconnect(false);
  //WiFi.enableAP(false);
  Serial.begin(115200);
  //WiFi.persistent(false);
  WiFi.mode(WIFI_AP);
  WiFi.softAPdisconnect (true);
  //WiFi.softAP("ESPNOW_test", "esp");
  WiFi.softAP("ESPNOW123", nullptr, 3);
  Serial.print("MAC address of this node is ");
  Serial.println(WiFi.softAPmacAddress());
  Serial.println();
  Serial.print("IP address: ");
  Serial.println(WiFi.softAPIP());
Note that I have added
WiFi.softAPdisconnect (true);
and changed the
//WiFi.softAP("ESPNOW_test", "esp");
WiFi.softAP("ESPNOW123", nullptr, 3);


However, trying to connect to the access point which is created on 192.168.4.1:
1605256808072.png



Should it not return the same page as Station mode?
 

Thread Starter

zazas321

Joined Nov 29, 2015
848
Ok. I believe it could have been a problem with my PC. It will refuse to open up a page on my PC but works fine on a smarthpone. I guess everything is as expected
 
I hate to say I am having similar problems with my ESP12F project. I have created my own custom socket on the ESP12F that a few custom applications between my PC and phone connect to it and pass data back and forth. In station mode everything works correctly, but in access point mode I have one chance to get the PC to connect or else reboot everything. The PC will connect to the access point, but the application won't connect to the port. The phone works flawless every time.
 

Thread Starter

zazas321

Joined Nov 29, 2015
848
Yeah thats weird. Works on my linux machine and android phone, but getting this error on my Window PC everytime:
1605515636000.png

It is not my Private PC therefore it may have some settings changed that would not allow to connect but I doubt thats the case?
 
Top