17

I'm in the process of building an IoT device using ESP8266. The device will eventually contain a couple of motors, and I would like to control these motors using MQTT. I would like to make the device as cheap as possible, so I would like to avoid things like displays and keyboards.

So, when the device is turned it is supposed to connect to the local WiFi, and then to an MQTT broker. But how does it know about the local SSID to connect to, and what about username and password? Since the device has no display or keyboard, there is no way to input these things. And how does the user know if the device was able to connect or not? For troubleshooting, it would be nice if the device at least had some way to indicate what the problem might be.

The solution I have thought of is to have one button and one LED on the device. The button would be marked "config" or similar. When the button is pressed, the device will start operating as a WiFi access point with a predefined SSID. It will have a webserver, so the user can connect with a laptop or phone to this predefined access point and enter the local network settings (SSID, username and password) as well as the address for the MQTT broker. The LED will be used to indicate the mode of operation, and also as error indication. For example, when the LED is glowing steady everything is connected, long flashes means it is in config mode, short flashes means there is an error, or something similar.

My question is, is the solution I have proposed a standard way of doing things when it comes to this problem? I.e. will it feel like a familiar flow to the user, or would some other way be better? After doing a little bit of searching I have found e.g. this, which seems to be a similar user flow. Would still be interested in hearing what experience you have of this, and hear how you would solve it?

If it turns out that this is a good and useful way to solve this problem, and since it is a kind of generic solution, it would be nice to not reinvent the wheel too much. I'm thinking that there could be a library that could do all of this. The library would be configured with the input pin for the button and output pin for the LED, and then take care of the rest. It could be built upon the PubSubClient library and based on the tutorial ESP8266: Connecting to MQTT broker. So is there a library that does this or something similar? If not I'll take a stab and create my own, but would like to hear about what's out there first.

Thanks!

2 Answers 2

14

What you describe is actually a quite common (the most common?) way to solve the problem of integrating WiFi IoT devices into your local LAN.

For example, from my experience Amazon Echo Devices are setup this way or a variety of smart switches/smart plugs (e.g. Shelly devices).

Depending on your requirements, you could also get away without the LED and the button.
Here is a solution with technical details that will try to connect to the configured network and if not successful open an access point for configuration.
And here a second implementation using a captive portal in AP mode.

Implementation of the MQTT PubSub part and the button and LED into the respective sketch should be fairly straightforward.

Alternatives:
For possible alternatives to the use of AP mode for configuration, please have a look at the techniques of Wi-Fi Protected Setup (WPS) and Device Provisioning Protocol.

2
  • Thanks for a very good answer! The WifiManager library in your second link seems to be exactly what I'm looking for. I will give it a try and see if I love it. If not, I will use the information in the first link to build my own solution... Commented Jul 4, 2019 at 5:59
  • 1
    Oh... And here is an addition that adds the MQTT part as well: github.com/dreed47/WifiMQTTManager Commented Jul 4, 2019 at 6:04
3

There is also the luftdaten.info project which is a open-source particle sensor with its own firmware. They do a similar thing to what you proposed, only without the config button. They do so, by starting the web server per default, when the device is powered on. After a certain threshold (I think it's somewhere between 3-10 minutes), the internal web server will be shut down - so no more configuration is possible until the next power cycle.

This solution might be too insecure for certain scenarios, but you might want to know about nevertheless.

Edit:

To get the initial configuration into the device, the following procedure is in place:

When the device is booted, it tries to reach the configured WLAN (there is no configured WLAN at the first startup). If it fails to connect to the pre-configured WLAN, it sets a static IP and spans it's own wireless network without a password, where one can connect to and do the initial configuration via the static IP address.

2
  • 3
    Just starting a web server doesn't solve the problem of how to pass the SSID/Password for the wifi network to the device to actually get it on the network.
    – hardillb
    Commented Feb 13, 2020 at 14:45
  • 1
    @hardillb True, take a look at luftdaten.info for more details. When the device is booted, it tries to reach the configured WLAN (there is no configured WLAN at the first startup). If it fails to connect to the pre-configured WLAN, it sets a static IP and spans it's own wireless network without a password, where one can connect to and do the initial configuration via the static IP address.
    – Jens Kohl
    Commented May 5, 2020 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.