Ive got the code working and some data sent to Adafruit IO:
https://learn.adafruit.com/adafruit-io- ... e-sketches
Problem is, I sometimes I take my IOT temperature sensor elsewhere so would like it to automatically try to connect to other wifi networks (including my phone's hotspot).
In the example code, it just gives one wifi to try. Is it possible to specify more than one?
Thanks
Adafruit IO trying more than one wifi network
Moderators: adafruit_support_bill, adafruit
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
- Xcross100
- Posts: 14
- Joined: Wed Jun 29, 2016 4:57 pm
- brubell
- Learn User Page
- Posts: 2017
- Joined: Fri Jul 17, 2015 10:33 pm
Re: Adafruit IO trying more than one wifi network
Not currently possible - you'd need to add it in manually
- Xcross100
- Posts: 14
- Joined: Wed Jun 29, 2016 4:57 pm
Re: Adafruit IO trying more than one wifi network
Ok thanks.
By adding manually, do you mean having a different sketch for each possible Wi-Fi network?
Would it not be possible to have a ‘try this password, if it doesn’t connect, try this password etc etc, until if it never connects then to give up’??
By adding manually, do you mean having a different sketch for each possible Wi-Fi network?
Would it not be possible to have a ‘try this password, if it doesn’t connect, try this password etc etc, until if it never connects then to give up’??
- brubell
- Learn User Page
- Posts: 2017
- Joined: Fri Jul 17, 2015 10:33 pm
Re: Adafruit IO trying more than one wifi network
It would be possible but would need to be integrated into the sketch during the WiFi connection initialization in setup().By adding manually, do you mean having a different sketch for each possible Wi-Fi network?
Would it not be possible to have a ‘try this password, if it doesn’t connect, try this password etc etc, until if it never connects then to give up’??
- Xcross100
- Posts: 14
- Joined: Wed Jun 29, 2016 4:57 pm
Re: Adafruit IO trying more than one wifi network
That’s great, I was hoping it would be possible. My challenges is now how to do it, does anyone have any sample code??
- hyde00001
- Posts: 13
- Joined: Fri Jan 20, 2023 4:16 am
Re: Adafruit IO trying more than one wifi network
I had a similar issue, my solution (I imagine there are more elegant ones, but this seems to work for me) was to insert multiple WiFi SSID and password into the settings.toml file each numbered with a _N extension. The order doesn't matter, but the process I've implemented is more efficient if the most likely to be used WiFi details have the lowest values of N. Here is an example of the text to insert into settings.toml
In my CircuitPython code I have this:
This code just works through the list of WiFi details in settings.toml until it connects to one. It doesn't do anything clever like connect to the strongest signal. If it doesn't connect to any of the WiFi detail entered in settings.toml it goes through the list again until it does. Thus subsequent code won''t get executed until a WiFI is found. You might not want this behaviour. A clever person would probably only fetch the details from settings.toml once, but this works simply and quickly.
You didn't ask for this, but thought you'd be interested, in my version I have another block code after the first section that counts the number of WiFi details and before the second block that tries to connect to them in turn; this extra block of code checks to see if there are io.adafruit login details in the settings.toml: if they are missing or there are no WiFi details, then it just stops execution of the code:
Hope that helps (first time I've been presumptious enough to offer a solution to any code problem, rather than beg for help, so please go gently on me if there are obvious or more elegant solutions...)
Code: Select all
CIRCUITPY_WIFI_SSID_1="NameOfWiFiAtHome"
CIRCUITPY_WIFI_PASSWORD_1="AppropriatePassword"
CIRCUITPY_WIFI_SSID_2="NameOfWiFiAtFriendsHouse"
CIRCUITPY_WIFI_PASSWORD_2="AppropriatePassword"
CIRCUITPY_WIFI_SSID_3="NameOfWiFiSomeWhereElse"
CIRCUITPY_WIFI_PASSWORD_3="AppropriatePassword"
CIRCUITPY_WIFI_SSID_4="NameOfWiFiCreatedByMyPhone"
CIRCUITPY_WIFI_PASSWORD_4="AppropriatePassword"
CIRCUITPY_WIFI_SSID_5="NameOfWiFiCreatedByAnotherPhone"
CIRCUITPY_WIFI_PASSWORD_5="AppropriatePassword"
Code: Select all
# settings.toml contains multiple wifi SSID and password details
# in a series of entries in the format of:
# CIRCUITPY_WIFI_SSID_N & CIRCUITPY_WIFI_PASSWORD_N
# where N is an integer that starts at 1 and can have any incremental value
# First, lets count the number of SSID & password values in settings.toml
# limitations: assumes the entries are unique & the first skipped number stops the count
# returns wifiSSIDCount which is an integer that contains the maximum valid value of N
wifiSSIDCount = 0
noError = True
while noError :
wifiSSIDCount += 1
WiFi_ssid = (os.getenv("CIRCUITPY_WIFI_SSID_" + str(wifiSSIDCount)))
WiFi_password = (os.getenv("CIRCUITPY_WIFI_PASSWORD_"+ str(wifiSSIDCount)))
if WiFi_ssid==None or WiFi_password==None:
wifiSSIDCount -= 1
noError = False
print ("Details for", wifiSSIDCount,"WiFI options found in settings.toml")
# Second, lets try connecting to the WiFi details found in settings.toml
# uses wifiSSIDCount determined earlier
# assumes the values of N used in settings.toml start at 1 and go up increments of 1
count = 0
noWiFiYet = True
while noWiFiYet :
count += 1
WiFi_ssid = (os.getenv("CIRCUITPY_WIFI_SSID_" + str(count)))
WiFi_password = (os.getenv("CIRCUITPY_WIFI_PASSWORD_"+ str(count)))
print ("Attempting to connect to WiFi:", WiFi_ssid)
try:
wifi.radio.connect(WiFi_ssid, WiFi_password)
print("Connected to", WiFi_ssid, "WiFi")
noWiFiYet = False
except:
print ("Failed to connect to", WiFi_ssid, "WiFI, trying another SSID")
if count == wifiSSIDCount:
count = 0
You didn't ask for this, but thought you'd be interested, in my version I have another block code after the first section that counts the number of WiFi details and before the second block that tries to connect to them in turn; this extra block of code checks to see if there are io.adafruit login details in the settings.toml: if they are missing or there are no WiFi details, then it just stops execution of the code:
Code: Select all
# settings.toml contains io.Adafruit login and password details
# can we acces them?
aio_username = os.getenv("ADAFRUIT_IO_USERNAME")
aio_key = os.getenv("ADAFRUIT_IO_KEY")
if (wifiSSIDCount==0) or (aio_username == None) or (aio_key == None):
print ("Couldn't access WiFI and or io.Adafruit details in settings.toml")
sys.exit()
else:
print ("io.Adafruit login details found in settings.toml")
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.