What is the first step to start the IOT project? Of course it is not hardware, but the connection between hardware and hardware! Even if there are various communication protocols that do not have good connection methods, it is absolutely impossible. If there is no screen on the peripherals and no keyboard, how can I enter the password and select the network? Yes, this is the most important solution for the WiFi module.
To solve this connection problem, lexin added the smartconfig function to its SDK. The so-called smartconfig is a UDP broadcast package or multicast package sent by the mobile app that contains the Wi-Fi user name and wi-fi password, the wifi chip of the Smart terminal can receive the UDP packet. As long as you know the UDP organization form, you can decrypt the WiFi user name and password through the received UDP packet, then, the user name and password of the smart hardware configuration are sent to the specified Wi-Fi AP.
The principles should be explained in simple steps as follows:
- Esp8266 terminal call
WiFi.beginSmartConfig()
The user name and password that will be sent by the mobile phone.
- Enter the current network password and send it to the UDP port.
- Esp8266 automatically receives and calls the connection information
WiFi.smartConfigDone()
Exit and wait.
That's easy!
Esp8266 firmware code
Sample Code for Arduino core for esp8266:
# Include <esp8266wifi. h>; void setup () {serial. begin (115200); delay (10); // the AP and station compatible mode WiFi must be used. mode (wifi_ap_sta); delay (500); // wait for the Wi-Fi in the distribution network. beginsmartconfig (); // After receiving the distribution network information, esp8266 will be automatically connected, WiFi. status status will return: connected while (WiFi. status ()! = Wl_connected) {delay (500); Serial. Print ("."); // The connection is completed and the distribution network is exited. Serial. println (WiFi. smartconfigdone ();} serial. println (""); serial. println ("WiFi connected"); serial. println ("IP Address:"); serial. println (WiFi. localip ();} int value = 0; void loop (){}
The code is simpler if nodemcu Lua is used:
wifi.setmode(wifi.STATION)wifi.startsmart(0, function(ssid, password) print(string.format("Success. SSID:%s ; PASSWORD:%s", ssid, password)) end )
Mobile phone
After writing the above Code to esp8266, you need to install an ESP-touch app provided by lexin on Android or IOS to inform esp8266 of the user name and password in the current wifi to complete the smartconfig connection.
Summary
The distribution network in this article only uses simple code to explain the principle, and the actual development of the application needs to be developed on the app side. I will also write an introduction to the app-side distribution network.
Related Resources
- Nodemcu-devkit nodemcu module description
- Smartconfig esp8266 for nodemcu and at-commands video demonstration
- Using bonjour in swift
Smart Wi-Fi network-smartconfig