Hi everyone, I'm using an esp32 as an AP, it have two modes, a config mode and AP mode, in config mode I can access a webpage where I can specify the ssid and password to be used during AP mode, I use Preferences.h to store ssid and password, the problem is that when I try to pass the Strings ssid and password to the WiFi.softAP function I get an error:
error: no matching function for call to 'WiFiClass::softAP(String&, String&)'
(I'm not an expert programmer but) inside the library WiFi, checking the content of WiFiAP.h I notice it is waiting for const char* values:
class WiFiAPClass
{
// ----------------------------------------------------------------------------------------------
// ----------------------------------------- AP function ----------------------------------------
// ----------------------------------------------------------------------------------------------
public:
bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4);
What modification must I do in the code to get it working? or how can the strings be converted to the type of data WiFi.softAP is waiting for? I will appreciate any help .
Circuit:
a button connected to pin 19, if pressed during boot, it will enter in configuration mode, if not pressed it will go to AP mode.
an amber led in pin 18 to indicate ESP is in AP Mode
a blue led in pin 5 to indicate ESP is in Config mode
My code
#include <WiFi.h>
#include <WebServer.h>
#include <Preferences.h>
#define Buttonpin 19
#define Blueled 5
#define Amberled 18
const char *ssidConf = "confignetwork"; // SSID for configuration mode
const char *pwConf = "12345678"; // password for configuration mode
IPAddress ip(192, 168, 4, 1);
IPAddress netmask(255, 255, 255, 0);
String message;
WebServer servidor(80);
//-----------HTML CODE FOR CONFIG PAGE---------------
String page = "<!DOCTYPE html>"
"<html>"
"<head>"
"<title>CONFIG PAGE</title>"
"<meta charset='UTF-8'>"
"</head>"
"<body>"
"</form>"
"<form action='save_conf' method='get'>"
"SSID:
"
"<input class='input1' name='ssid' type='text'>
"
"PASSWORD:
"
"<input class='input1' name='password' type='password'>
"
"<input class='boton' type='submit' value='SAVE'/>
"
"</form>"
"<a href='scan'><button class='boton'>SCAN</button></a>
";
String endofpage = "</body>"
"</html>";
//-------------------CONFIGURATION PAGE--------------------
void pageconf() {
servidor.send(200, "text/html", page + message + endofpage);
}
//---------------------------SCAN----------------------------
void scan() {
int n = WiFi.scanNetworks(); //returns the number of found networks
Serial.println("scan finished");
if (n == 0) { //if no network is found
Serial.println("No networks found");
message = "No networks found";
}
else
{
Serial.print(n);
Serial.println(" Discovered networks");
message = "";
for (int i = 0; i < n; ++i)
{
// Following code adds to "message" string the information of discovered networks
message = (message) + "<p>" + String(i + 1) + ": " + WiFi.SSID(i) + " (" + WiFi.RSSI(i) + ") Ch: " + WiFi.channel(i) + " Enc: " + WiFi.encryptionType(i) + " </p>\r\n";
//WiFi.encryptionType 5:WEP 2:WPA/PSK 4:WPA2/PSK 7:open network 8:WPA/WPA2/PSK
delay(10);
}
Serial.println(message);
pageconf();
}
}
//---------------------SAVE CONFIGURATION-------------------------
//wireless
void guardar_conf() {
Preferences preferences;
preferences.begin("WifiCred", false);
preferences.putString("ssid", servidor.arg("ssid"));
preferences.putString("password", servidor.arg("password"));
preferences.end();
message = "Saved...";
pageconf();
}
void setup() {
// put your setup code here, to run once:
//---------------CONFIGURATION MODE-------------------------------
if ( digitalRead(Buttonpin) == HIGH ) {
// if (debug) COM[DEBUG_COM]->println("You have entered to configuration mode");
//AP mode (phone connects directly to ESP) (no router)
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(ip, ip, netmask); // configure ip address for softAP
WiFi.softAP(ssidConf, pwConf); // configure ssid and password for softAP
digitalWrite(Blueled, HIGH);
digitalWrite(Amberled, LOW);
servidor.on("/", pageconf); //this is the configuration page
servidor.on("/save_conf", guardar_conf); //Save configuration in the eeprom
servidor.on("/scan", scan); //Seach for available networks
servidor.begin();
while (true) {
servidor.handleClient();
}
}
//--------------------ACCESS POINT MODE-------------------------------
if ( digitalRead(Buttonpin) == LOW ) {
Preferences preferences;
preferences.begin("WifiCred", false);
String wnet = preferences.getString("ssid","");
String passwd = preferences.getString("password","");
Serial.println (preferences.getString("ssid"));
Serial.println (preferences.getString("password"));
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(ip, ip, netmask); // configure ip address for softAP
WiFi.softAP(wnet, passwd); // configure ssid and password for softAP
digitalWrite(Blueled, LOW);
digitalWrite(Amberled, HIGH);
preferences.end();
}
}
void loop() {
// put your main code here, to run repeatedly:
}