Search code examples
androidiotesp8266pairing

Implementing a IoT Device Pairing process in Android over Wifi


I am currently working on the integration of IoT devices into a cloud. Many people know the Gosund smart plugs. Just for fun, I've been playing around with the original firmware and wondering how this coupling process is solved. The Gosund sockets are based on the ESP8266 and have no Bluetooth on the hardware side, only W-LAN. The Android app starts a search process when pairing, finds the socket, sends the settings and the setup is finished. But how exactly is this solved on the software side?

I'm interested in that, because I want to reproduce the same way it is solved for our app. My first theory was that the Gosunds, similar to Tasmota, open an access point with a webserver behind it and the app sends the settings via REST calls.

For testing purposes I implemented this in a small prototype, which also works so far. But you can see with my code that the phone is changing the wifi. Open connections are closed and so on... With the Gosund app this is not the case. My mobile phone stays in my W-LAN the whole time but manages to send the settings via Wifi to the gosund plug at the same time.

How is that solved?

Alternative theory: The app opens an access point and the gosund plug tries to connect. But can Android do that? Stay logged in to a wifi and open a hotspot at the same time?


Solution

  • OK, so now I know how it works. The trick is to encode the WiFi credentials into the unencrypted header of UDP packets. The ESP then searches for exactly these packets on all available access points. This allows the device to be configured from the mobile phone without having to change a WiFi connection. This technique is called SmartConfig.

    Here is explained in detail how it works: https://www.eeweb.com/profile/steve6366/articles/smartconfig-how-to-turns-an-esp8266-into-a-smart-home-device