Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

Canonical bootstrapping approaches to user setting SSID / PSK / Method in deployment?

Open vicatcu opened this issue 11 years ago • 35 comments

The same problem every Wi-Fi connected thing ever has to solve... how to get the target network credentials into the device without compiling them into the sketch.

I've been trying to create a basic example that does this:

  1. Tries to connect to the SDK stored network configuration.
  2. If it can't connect to the SDK stored network configuration (or if the user pushes a button), reverts to AP_STA mode so that you can connect to the ESP8266 with a computer.
  3. In AP_STA mode, runs MDNS and Webserver with a page that you can go to http://esp8266.local in a browser, be presented with a form to fill out the network credentials, and click submit.
  4. Also a form submission handler page that tries to connect the STA to the network and credentials provided by the user and provides success / failure feedback to the user regarding ability to connect. On successful connection this becomes the SDK network configuration.

I've been trying to put something like this together from the examples, and it's proving a bit harder than I thought it would be. I've managed to kind of do aspects of the above by stitching together code from the library examples, but the WebServer is not very snappy to respond in the webpage, and I have a heck of a time connecting to the SoftAP (both with my Android phone and my Ubuntu 14.04 laptop, both indicating very strong signal strength). Happy to share my (currently crappy) code here.

I recognize that this is not really an issue per se, an @igrr if you prefer to move this thread elsewhere please chime in.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

vicatcu avatar Oct 01 '15 22:10 vicatcu

By the way, the web server is very snappy if I hardcode connect it to my network and access it via the STA IP address...

vicatcu avatar Oct 01 '15 22:10 vicatcu

Hi Victor, Try pfodWifiConfig library and example code detailed on http://www.forward.com.au/pfod/CheapWifiShield/ESP2866_01_WiFi_Shield/index.html Does most of what you want, but access to the config web server is via 10.1.1.1
I did not bother to add the name resolution stuff, but happy to ride on the back of your efforts when you get that running.

That example also covers how to generate the AP secure password and write it out as a QR code attach to the ESP device to be scanned to get the config AP password.

I had another user recently request a small Android app that opens a scanner to scan the password and then connects the the AP and open the config webpage, seems to work for him.

As an aside I also have a process and libraries for configuring WiFi devices that cannot be provide an AP (see http://www.forward.com.au/pfod/pfodWifiConfig/index.html)

drmpf avatar Oct 01 '15 23:10 drmpf

Did you try wifi WIFI_AP_STA mode? And try to wait for connection after begin.

pgollor avatar Oct 02 '15 10:10 pgollor

@drmpf Thanks, I will definitely have a look!

@pgollor from what I can tell the ESP8266 comes up in AP_STA mode by default. Where are you suggesting I should be waiting for connection in [the code[(http://pastie.org/10454905) that I posted? Bear in mind that before I give the ESP8266 credentials for the SDK to internalize, the STA can't possibly connect to anything, right?

vicatcu avatar Oct 02 '15 12:10 vicatcu

@igrr looking at the code from @drmpf, I seem to remember a proposal in recent history that a comparison should be made in the library to determine if the SSID and PSK provided to begin represent a change to what's already stored in the SDK, probably sometime before here in the WiFi.begin method?

vicatcu avatar Oct 02 '15 13:10 vicatcu

Yes i think it would be good to do this check inside WiFi.begin. I am planning to do some refactoring in ESP8266WiFiClass, because the code is getting rather convoluted. Especially the logic for switching between WiFi modes... I will create a separate ticket for that.

Regarding the original topic, yes, it makes sense to provide a ready-made solution for initial configuration, probably in the form of a library. Some aspects about dependencies we may have in such a library are not clear to me, like:

  • is it okay to require the user to include ESP8266WebServer library?
  • if we use the ESP8266WebServer library, who gets to initialize the server? Should it be done inside the library or the user has to do it in the sketch?
  • can we depend on the file system, or should we use simplified html pages and store them in PROGMEM?
  • is it okay to require the user to upload some html files to the file system?

These choices mostly depend on usage scenarios we want to cover. Any discussion which may help clarify these points is welcome!

igrr avatar Oct 02 '15 13:10 igrr

@igrr I think with a stable upload tool for the filesystem it is better to use it by default instead oft progmem pages.

pgollor avatar Oct 02 '15 17:10 pgollor

@igrr I think you're thinking about the right things. I'm good with the idea of the capability being encapsulated in a library, so lets explore that idea.

  • Since the user may want to include a Server in their sketch in a more general sense than for initial configuration, I think it makes sense for the Server to be passed to class from the sketch somehow (e.g. a setServer() method).
  • Following the same logic from the previous bullet point, I think it should probably be left to the user sketch to initialize the server. Ideally there would be adequate reflection possible so that the library could complain if it is passed a Server object that is not in the expected state. A solid example with good inline comments about the importance of sequence of events would go a long way.
  • SPIFFS seems really cool and versatile. Uploading one or more files as a pre-requisite for the library seems like a pretty reasonable expectation. Possibly it would be good to have a minimal implementation in PROGMEM if the required files are not found in SPIFFS. I think might be pretty important that an example work "out-of-the-box".

I guess that's my opinions. Interested in others opinions.

vicatcu avatar Oct 02 '15 17:10 vicatcu

@drmpf I was able to try your pfodWifiConfig sketch but I had a similar experience to my own attempt, i.e. the server didn't respond (reliably/quickly) to the Configure button being pressed, and usually the "/config" page never loads (I've got it to load once). Sometimes I have to connect to the SoftAP a few times, and sometimes it drops me after connecting as well. Could it be just some undiscovered issues in the staging release (server performance on softAP interface), or is your experience similar in general?

vicatcu avatar Oct 02 '15 19:10 vicatcu

Been scouring the web... found this forum post that I'll need to give a try. I'll give it a whirl tomorrow and report back.

vicatcu avatar Oct 02 '15 23:10 vicatcu

@igrr

is it okay to require the user to upload some html files to the file system?

From my point of view NO, May be OK for a commercial board setup, but for novices, I need the simplest process possible. I.e. load this sketch and run.

Since the user may want to include a Server in their sketch in a more general sense than for initial configuration, I think it makes sense for the Server to be passed to class from the sketch somehow (e.g. a setServer() method).

I don't think this is necessary or desirable. When starting in config mode none of the user's normal code will be run so the setting up of the server can and should be done completely within the supporting library. Not that my example code does this but.. if we are going to provide a standard library that is how I think it should run.

@vicatcu, I use the pfodWifiConfig regularly without any problems at all. I connect from my Android a few feet away. Always works for me. I am using package esp8266-1.6.5-947-g39819f0, which is not the latest. This suggests to me your Wifi network is flaky. Perhaps you could try changing Channels for the ESP? I was never sure how you are supposed to set the channel. You could also try running WireShark on your pc and connect it to your ESP AP and see what is happening. Had quick look at the link you posted. pfodWifiConfig has a more complete URL decoder which handles more then just spaces. Also see following comments.

@igrr and @vicatcu As a general point. Just setting the ssid and password (and security WPA2 etc) is usually not enough. If the user is going to run the ESP as a server then they at least want to set the port it will listen on and probably the IP address so they can find it. If the user is going to run the ESP as a client then they will want to set the host and port to connect to and perhaps the username and password. (Currently working with a user on this type of client config.)
Setting up to run as a client is not completely straight forward because of the various types of client to server connections, but at least by storing the server host, port, username and password for the client connection from the config then the sketch can just retrieve them and plug them into the appropriate spots.

So for a general config library I think we need to allow for these setting as well. Have a look at http://www.forward.com.au/pfod/pfodWifiConfig/pfodWifiConfig.html for a definition of what I think needs to be configured and saved for various uses, Server, Client, ClientLogin

Also since I wrote that I have found you need to have a connection timeout setting. Essential for running as a Server and probably very useful when running as a Client to ensure the WiFi connection is cleaned up properly when there are network errors, drop outs, router power outages etc. http://www.forward.com.au/pfod/CheapWifiShield/ESP2866_01_WiFi_Shield/index.html and the supporting library include configuring the connection timeout.

drmpf avatar Oct 03 '15 01:10 drmpf

@vicatu I posted the onboarding code in http://www.esp8266.com/viewtopic.php?p=17694. There is a slightly (by two days) more up to date version at https://github.com/jeffryr/ESP8266-Sketches. AFAIK it still works but it's been quite a few months and there may be a more elegant solution.

jeffryr avatar Oct 03 '15 02:10 jeffryr

This is a loosely related problem I have been thinking about for a while.

When I have a few ESP's I am working on at the same time, to give them all a generic name causes issues. I recently created a whole lot of boards for controlling robot arms and gave them away at a hackathon. The default AP SSID was eeArm on 50 boards and this resulted in people inadvertently setting up each-others boards. This caused huge confusion.

Does anyone know of a predictable way to create a unique SSIDs? Maybe based on MAC address? Would be cool to create, say a six character unique string. The alternative is burning unique defaults to each, but that seems like a lot of work!

chrisfraser avatar Oct 03 '15 11:10 chrisfraser

Yes MAC address would be an good alternative in that situation.

uint8_t* softAPmacAddress(uint8_t* mac);
    String softAPmacAddress(void);

in ESP8266WiFi.h looks like what you want. I do burn each one with a unique password which I also use for pfodSecurity.

drmpf avatar Oct 03 '15 11:10 drmpf

a simple webpage with some security allowing posting the a configuration file into flash memory is the best option; in my opinion; note the SECRUITY is needed SSL/HTTPS server would be great, but is the unit capable? some RSA public/private key encryption using javascript client side encryption to secure login details and configuration data.

grahamehorner avatar Oct 03 '15 12:10 grahamehorner

BTW, I'm working on a simple webserver that has a configuration page (username/password) which allows the upload of a config.json file this has a setting WiFi mode and SSID together with a secured password encrypted using a public/private key.

After initial configuration of the WiFi SSID/password the webserver restart the WiFi using the chosen SSID/password and enabled OTA update features.

The idea here is that initial configuration can be automated as the SSID is broadcast and is of a known form, initially with no/a known password.

grahamehorner avatar Oct 03 '15 12:10 grahamehorner

a simple webpage with some security allowing posting the a configuration file into flash

I don't think any extra security is needed. The ESP configuration AP should be protected by a 'secret' key, in my example that key is unique per device. So only users with access to the secret key can connect to the ESP AP and load the real network ssid and password. Others cannot eavesdrop on this connection without the 'secret' key.

drmpf avatar Oct 03 '15 13:10 drmpf

I am ignoring OTA updates and how they may be handled. While OTA is a great feature, I don't think is it necessary for the general user wanting to get their device setup on the nextwork. More for developers making code changes.

drmpf avatar Oct 03 '15 13:10 drmpf

Reporting back. This code from the forum, crashes, but thanks @jeffryr for the github link, that doesn't crash. I still get aweful performance from the SoftAP though. Here's a ping report:

PING 192.168.4.1 (192.168.4.1) 56(84) bytes of data.
From 192.168.4.2 icmp_seq=1 Destination Host Unreachable
From 192.168.4.2 icmp_seq=2 Destination Host Unreachable
64 bytes from 192.168.4.2: icmp_seq=3 ttl=255 time=2000 ms
64 bytes from 192.168.4.2: icmp_seq=4 ttl=255 time=1000 ms
64 bytes from 192.168.4.2: icmp_seq=5 ttl=255 time=8.80 ms
64 bytes from 192.168.4.2: icmp_seq=24 ttl=255 time=3.64 ms
64 bytes from 192.168.4.2: icmp_seq=28 ttl=255 time=29.3 ms
64 bytes from 192.168.4.2: icmp_seq=47 ttl=255 time=4.05 ms

Also when I go to the server page and punch in my SSID and password (which loads eventually), when I submit the form it never results never loads, eventually resulting in "Error code: ERR_CONNECTION_TIMED_OUT" in the browser. Meanwhile in the serial monitor, sometime after pressing submit, I get:

Waiting for Wifi to connect...
66666666663
Great Success!

Soft WDT reset

ctx: cont 
sp: 3ffec630 end: 3ffec900 offset: 01b0

>>>stack>>>
3ffec7e0:  3fff4460 0000000f 0000000b 3fff4398  
3ffec7f0:  0000000f 0000000c 3ffec8b0 4020abaa  
3ffec800:  3fff4098 00000006 3ffec850 3ffeabfc  
3ffec810:  3ffeb3c8 3fff3ff8 3fff3ff8 402095e2  
3ffec820:  3ffeb3c8 3fff3ff8 3ffec850 4020643a  
3ffec830:  0000000f 3fffdc20 3fff3ff8 4020646a  
3ffec840:  3fff4cb0 0000000f 3ffeabc8 402060d8  
3ffec850:  3fff4378 0000000f 00000006 0000002f  
3ffec860:  00000006 3fff4460 0000000f 00000003  
3ffec870:  3fff5240 0000007f 00000000 3ffeb8b4  
3ffec880:  40206444 00000001 402013f4 0000000f  
3ffec890:  402010ae 00000001 00000001 3ffec92c  
3ffec8a0:  3fffdc20 000003e5 3ffeabc8 402061ed  
3ffec8b0:  3ffe96c8 00000000 000003e8 0006a248  
3ffec8c0:  3ffeabe0 3fff4d60 00000016 40101a2d  
3ffec8d0:  40201415 00000000 3ffec924 402028e0  
3ffec8e0:  00000000 00000000 00000000 40201472  
3ffec8f0:  00000000 00000000 3ffeb8e0 40100398  
<<<stack<<<
```
On a reset it does connect to my network, so that's a plus...

vicatcu avatar Oct 04 '15 18:10 vicatcu

Clearly something the in the code is not yielding (yield() or delay()). It is possible that it is a library bug, BUT.. most likely a problem in the uploaded Arduino sketch. You need to start cutting out code until it goes way and then put the last line back in to reproduce the error. (but I didn't nee to tell you that)

I did not have that issue with http://www.forward.com.au/pfod/CheapWifiShield/ESP2866_01_WiFi_Shield/index.html after adding some yield() calls.

You might also look at https://github.com/jeelabs/esp-link/tree/v1.0.1 for another set of code

drmpf avatar Oct 04 '15 23:10 drmpf