# Method to configure an Embedded CPU for WiFi network w/o a physical user interface

Let's say you have a device you built that uses something like an Arduino with a Wifi shield. The intent of this device is to connect to your wifi network, and then connect to the internet and do something (use your imagination).

If the device was so simple that it did not have any buttons or a screen on it, how would one initially configure it to connect to a particular WiFi network?

(Obviously directly inputting the wifi network parameters into the source code for your project is out of the question).

Yesterday something happened that got me thinking. I purchased a WiFi enabled Honeywell Thermostat for our house. It connects to your WiFi network so you can control the thermostat using your phone from anywhere in the world.

The method used to connect the thermostat to the wifi was interesting though. When you first hooked up the thermostat, it would create it's own wifi network/server called "Thermostat_EKDD94" or something. You use your phone or laptop to connect to this network. Then visit 192.168.1.1 in the browser, in which you see a list of available wifi networks to connect to. You pick the network, input the password, and then the thermostat disconnects you and connects to the network you picked. At this point, the thermostat's own wifi network/server goes away for good (unless you do a hard reset on the thermostat at a later time).

And just like that, your thermostat is connected to a wifi network of your choosing, all without having to actually touch the device. It was a very interesting approach. Is this something that is possible with an Arduino? Can it act like a server and then client? Maybe at the same time?

Any thoughts to this approach or if there is a better approach?

Any WiFi module that supports ad-hoc mode should be able to do this. I've used the WiFly module before, and I know it has this capability. Note that the module can only be in ad-hoc or client mode at any given time; not both at once.

You can use that module directly with your Arduino (assuming Uno or compatible footprint) via this shield. There is also a standalone shield that might be more space-saving. Of course, other modules are available that are up to the task but these are the ones to come mind.

Here is how I would set up this specifically. In response to your comment, I've modified the approach to fix the problem of what happens if the WiFi association fails.

1. Arduino starts up and sets WiFly to ad-hoc mode.
2. You connect to the WiFly's network and navigate to the WiFly's "web server".
3. You view the list of available networks, enter connection parameters, and click "Submit." * see note below
4. The WiFly's response page contains a piece of JavaScript code that fetches a special page with Ajax after some fixed time interval (maybe 2 or 3 minutes). Maybe call the address http://[WiFly address]:80/status.
5. While the aforementioned timer is ticking, the WiFly has turned off ad-hoc mode and turned on client mode (your laptop will have disconnected from the WiFly, of course). The WiFly attempts to connect to the network you specified. If successful, the WiFly's work is done. If the connection fails, it re-enables ad-hoc mode which your laptop should automatically reconnect to.
6. The timer we set in #4 hits 0. The page attempts to asynchronously fetch that special page we mentioned. If successful, we know that the WiFly failed to connect to our network and thus has re-enabled ad-hoc mode. If the request fails, we know that the WiFly connected successfully to the network.
7. In the former case, with JavaScript you can display the appropriate message to the user, and maybe even navigate to the WiFly's "home page" to start the process over.

Special considerations:

• You might need to play around with the timeout for step #4.
• Your laptop/device will need to be configured to automatically connect to the WiFly's ad-hoc network and even prefer that network over others.

* Interesting side note:

You might be wondering how the module can scan for networks while in ad-hoc mode. According to the manual:

The WiFly module supports adhoc and infrastructure networking modes. However, it does not support both the modes simultaneously. Scanning for wireless networks is a function of infrastructure mode. To do this, the WiFly module has to disable adhoc mode and scan.

With the release of firmware version 2.22 and later, it is possible to scan from adhoc mode. Issuing the scan command temporarily disables adhoc while the module is scanning. The adhoc is restored automatically when the scan is complete. If you are connected to the module over telnet, the result of the scan command is sent over telnet and then adhoc is restored.

If your requirements allowed a separate user interface (perhaps an application on your laptop), you could have used this feature to your advantage and avoided crafting an HTTP server altogether. The application could have connected to the WiFly's ad-hoc network, opened a Telnet session, and then automated the Telnet session to configure the module.

# Alternate approach

If cost is not an issue, you could use two WiFly modules - one to provide a constant ad-hoc network and the other to act as the WiFi client.

• One thing I would wonder about: Lets say user connects to the AP, chooses his wifi network, puts in the Wifi password, but it's incorrect. The device is going to have to turn off the AP, then try to connect to the Wifi network and see that it fails, but now it can't relay this feedback to the user because the AP is gone and the user is probably automatically reconnected back to their home network. See the potential problem? – Jake Wilson Feb 8 '13 at 18:42
• @Jakobud: Great question - I've edited my answer to (hopefully) provide a solution. – Chris Laplante Feb 8 '13 at 21:31

How about having the device behave as a USB mass storage device. Here is the use case:

1. User plugs in the device into PC.
2. User navigates to the storage volume exposed by the device.
3. User edits parameters in a text file, and saves.
4. User restarts device.

Instead of 3, the device's filesystem could have some setup.html file in the root which the user fires up in the browser. The user then interacts with a JavaScript-ed UI to create the configuration. (How, you might wonder? But HTML5 does evidently allow JavaScript in the browser to interact with the local file system).

That is a very elegant way to solve this task provided your wifi hardware supports access point mode.

Another way of doing that could be via serial interface - you can use the arduino command line library http://www.freaklabs.org/index.php/Tutorials/Software/Tutorial-Using-CmdArduino.html to implement some serial commands which allow entry of SSID and password. User connects the device to an USB port, opens a serial terminal software and configures the device via command line using commands you implemented. You can save SSID and password in eeprom of arduino if needed.

Not the most user friendly way and requires you to leave usb port accessable, but the easiest to implement I would guess.

• Thanks for the input, but the serial thing would be out of the question. If I wanted to make a product to sell on shelves that a normal non-computery person would purchase, I would never expect them to jump thru those hoops. – Jake Wilson Feb 8 '13 at 18:33