Tag: PAM_WiFiConnect

Update for library PAM_WiFiConnect

Update for library PAM_WiFiConnect

I recently had a short power outage. Nothing major but a feature request for the connect library did come out of that. When the power came back on, my ESP with my temperature logger was back on faster than my wifi which meant that after the wifiConnect nothing else happened. Ok, I could have coded that in the program but than I realized that if you have some sort of IoT device, it does not make sense to return to the setup function from the wifiConnect if you have no wifi. A need was born.

The result? A function called waitWiFiConnect which does what it’s name implies; it waits for a wifi connection. It does this by turning on the internal led, GPIO2, try to connect via wifiConnect. When it returns from that function it checks whether there is a connection. If there is a connection the led is turned off and the function ends. If not, the led blinks shortly after which we wait for 1 minute before trying again. It will only return to the caller after it has a wifi connection.

github-mark-32px Available on GitHub with a lot of comment in the code. Direct download.

Library PAM_WiFiConnect

Library PAM_WiFiConnect

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.

github-mark-32px 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.