After working with the Arduino IDE for some time, I decided it is time to write some convenient libraries that will help me quickly do some of the tasks that occur often and could do with a convenient wrapper.
This library is aimed at easily connecting to wifi. I know that there are other libraries that support this but mine supports me better and might support you better too.
The library needs one other library, PAM_Tools.
Available on GitHub with a lot of comment in the code. Direct download.
Let’s look at the functions:
void wifiConnect (String ssid, String password)
This function will connect you to the specified SSID using the specified password. It is different from the standard connect to wifi and just waiting on a connected in two ways:
- It will break trying to connect after 5 runs of 10 seconds waiting on a connection
- It will disconnect from wifi and try again in every run.
In my experience some of the connections might not work in the first try (especially hotspots on a mobile phone) and by stopping the connection and starting again I noticed that most connections will succeed within 15 seconds.
I also wanted the wifi connection not to run forever but break off at some point so you could do some error handling.
void wifiConnect (String ssid)
This instance of the function will get the password for the specified SSID from the file system, through one of the functions in PAM_Tools, and than uses wifiConnect to connect to it.
Passwords are stored in a file on the file system in the directory wifi where the full name of the file will be /wifi/ssid.txt and this file will contain one line and that is the password for that ssid. There are two advantages to do this:
- If you share your code and you forget to change your ssid to Your-SSID at least you will not have given away your password
- If you have multiple ssid’s that you can connect to, which is what wifiConnect does if you do not supply any SSID, you do not have to have all SSIDs and passwords in your file.
void wifiConnect (bool debug)
This instance of the function in the library is probably the most powerful/convenient. What it does is scan the available networks and check for each one found whether there is a associated password stored in the file system. If so, it will try to connect.
You can list preferred networks by storing SSIDs in a numbered file in the same directory. So if there is a file called 1.txt in the wifi directory, it will open that file and check whether that SSID is available. If not it will try 2.txt etc. If none of them are available or there is no 1.txt then it will try to use any of it’s other credentials. If there is still no connection, it will try the SSID in 0.txt which could be a hidden SSID. I found this function works great if you have a mobile IoT device. Even with fixed devices I now use it and put my mobile hotspot as the first to check (1.txt). It usually isn’t on, but if I want to isolate the IoT, for example for testing, I can turn on my hotspot, reset it and it will connect to my hotspot.
If you want to see which networks are found and how it decides to connect to it, you can call it with a parameter true after which it prints out what it does on Serial.
void wifiConnect ()
This instance of the function in the library will call wifiConnect(false) and as a result try to automatically connect to a wifi network as described above.