adafruit / Adafruit_IO_Arduino

Arduino library to access Adafruit IO from WiFi, cellular, and ethernet modules.
Other
209 stars 108 forks source link

Allow SSID determination at runtime #102

Closed sellensr closed 4 years ago

sellensr commented 4 years ago

The current configuration assumes you know the SSID and password at compile time, for inclusion in the config.h file as #defined string constants so you can create e.g.

AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);

Once compiled, there's no clearly supported way to connect with a different SSID. The scenario I would like to support is a device that could run in different locations by detecting and initializing different wifi networks, much as computers and phones do.

As best I can see, it should be possible to have multiple const char * string pointers and choose which one to use at runtime, provided _ssid and _pass are set before _connect is called. I had that approach working on a previous release using

io = new AdafruitIO_WiFi(IO_USERNAME, IO_KEY, wlan_ssid[ssidIndex], wlan_pass[ssidIndex]);

where the arrays were char * but it seems to have broken along the way somewhere -- not sure if it relates to the strings being variables or something else. I'm imagining a code sequence like io.resetSSID(ssid,pass); io.connect(); //do things here io.disconnect(); // https://github.com/adafruit/Adafruit_IO_Arduino/issues/101 to enable a device to talk to AIO, then go talk to something different, maybe with a different ssid, and then come back. io.resetSSID() might just need to check if AIO is disconnected and change the values of _ssid and _pass, or is there something more complicated lurking in the background?

sellensr commented 4 years ago

I think I have a simple solution that doesn't alter current functionality. Test strlen(_ssid) in the _connect() function and only perform the wifi connection functionality if the length is non-zero. Any sketch that set the SSID to "" would have to look after its own wifi connections. Adding a WiFi.disconnect() before the WiFi.begin() would provide a fresh start. Any thoughts?

brentru commented 4 years ago

Fixed in PR #103 and released in v3.3.1 (https://github.com/adafruit/Adafruit_IO_Arduino/releases/tag/3.3.1).