The past few days I've been doing more arduino-work. In between dying of sleep-exhaustion.
One thing that always annoyed me was that I had to hard-code my WiFi credentials in my projects, with code like this:
// // Connect to the SCOTLAND network // WiFi.mode(WIFI_STA); WiFi.hostname("tram-clock"); WiFi.begin("SCOTLAND", "highlander1"); // // Attempt to connect - TODO: Timeout on failure // while (WiFi.status() != WL_CONNECTED) delay(500); // // Now we're connected show the local IP address. // lcd.print("WiFi connected "); lcd.print(WiFi.localIP());
Whilst looking at another project I found a great solution though. There is a library called WiFiManager which behaves perfectly:
- If you've stored connection details it will connect to the local WiFI network using those, automatically.
- If you've not saved previous connection details it will instead configure the device to work as an Access Point
- You can then connect to that access point and see a list of local WiFi networks.
- Choose the appropriate one from the list, enter your password, and these details are saved for the future.
- The device will then reset, join the network via your saved choices and acquire an IP via DHCP as you'd expect.
The code for this is beautifully simple:
// // Connect to WiFI with saved credentials, if any. // // Otherwise work as an access-point, named TRAM-TIMES, and // let the user fill out their details. // WiFiManager wifiManager; wifiManager.autoConnect("TRAM-TIMES");
This means my current project, which continues to revolve around tram-times, is so very much more user-friendly. It is a product you could package and take to a friends house, not a project you have to recompile to tweak.
For that reason, user-niceness, I reworked the on-board HTTP status-page to use bootstrap, be themed, and look nicer. Other than being housed in a horrid case the project actually looks like a product. Not one I'd buy, but neither one I'm ashamed of sharing.