Hayder Ismael
Hayder Ismael

Reputation: 11

ESP32: How to change WiFi SSID using access point with Arduino IOT cloud running

I have been trying to use WiFi.h, WebServer.h and EEPROM.h together with ArduinoIoTCloud.h and Arduino_ConnectionHandler.h to be able to change the WiFi credentials if required without changing the sketch and reuploading it. The problem is when moving;

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS)

inside void setup() wouldn't work whether with constant char of the credentials or after modifying it to read from the EEPROM, like

WiFiConnectionHandler ArduinoIoTPreferredConnection(esid.c_str(), epass.c_str());

Please, is there any way to change the WiFi credentials from a web browser?

The Code

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char DEVICE_LOGIN_NAME[]  = "********************";

const char SSID[]               = "SECRET_SSID";    // Network SSID (name)
const char PASS[]               = "SECRET_OPTIONAL_PASS";    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = "SECRET_DEVICE_KEY";    // Secret device password

void onTemperatureChange();

float temperature;

void initProperties()
{
  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(temperature, READWRITE, ON_CHANGE, onTemperatureChange);
}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

void setup() {
  Serial.begin(9600);
  delay(1500); 
  initProperties();

  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update(); 
}


void onTemperatureChange()  {
}

So, when I add the code for the access point to write and read from the EEPROM, I then, o my knowledge, need to move this line inside the void setup.

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

For example



void setup() {

  Serial.begin(9600);
  delay(1500); 
// Code for reading from EEPROM and the eeprom_SSID and eeprom_SSID_PASS will then pass to SSID and PASS Chars
//
  initProperties();
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

After that, the ESP will be pushed to restart as the IoT library doesn't find the SSID and PASS.

I have tried to do so with Blynk and many other libraries, and it worked; I could enter the credentail by activating a server and creating an access point to do so, and then blynk read from that and connected perfectly. The problem is that I have to use the Arduino IoT cloud, not Blynk. Thanks for your support.

Upvotes: 1

Views: 638

Answers (1)

Luca
Luca

Reputation: 11

I had the same problem recently on an Arduino Nano 33 IOT. I solved as follows:

  • Declare the instance of WiFiConnectionHandler as a global pointer, outside the setup function (not initialized! because, at this stage, we do not know the credentials of the WiFi network we want to connect to!)
  • Initialize the instance of the instance of WiFiConnectionHandler inside setup, only after having read the credentials from the EEPROM

In my case, it looks like this:

// declaration (outside setup)
WiFiConnectionHandler * iot_connector;

// setup function
void setup() {
 // read from EEPROM credentials of WiFi network to connect to
 // ssid = readFromEEPROM()
 // password = readFromEEPROM()
 
 // initialize the WiFiConnectionHandler pointer
 iot_connector = new WiFiConnectionHandler(ssid, password);
 ArduinoCloud.begin(*iot_connector);

 // setup OTA callback
 ArduinoCloud.onOTARequestCb(wifiOTARequestCallback);

 // setup debug info
 setDebugMessageLevel(DBG_VERBOSE);
 ArduinoCloud.printDebugInfo();
}

Upvotes: 1

Related Questions