1. AliOS Things Wi-Fi networking background
Connecting to a Wi-Fi network is the first step for most IoT devices to connect to the Internet.
Access to Wi-Fi generally requires two stages: network distribution and network connection.
1.1 Introduction to Netmgr
The Netmgr module introduced in this article abstracts the Wi-Fi-driven network distribution and networking capabilities to facilitate IoT devices to quickly access the network and connect to the cloud.
Wi-Fi devices need to be connected to Wi-Fi hotspots (Wi-Fi AP) before they can communicate with other devices based on IP. We call the step of Wi-Fi devices obtaining the SSID/password of Wi-Fi hotspots as Wi-Fi. Fi distribution network. For mobile phones/computers/tablets, users can enter the SSID/password of the Wi-Fi hotspot through the keyboard or touch screen. But for IoT devices with no keyboard and no touch screen, how to obtain the SSID/password of the Wi-Fi hotspot is the first key step to realize device network management.
1.2 Distribution network
Briefly introduce the following two network distribution methods:
1. Zero configuration : A network configuration solution that does not require users to input hotspot information. It allows a device connected to the Internet hotspot to send the hotspot's SSID/password to the device to be configured.
2. One-click network configuration : The mobile phone APP packs the corresponding information into a specific area of the 802.11 data packet and sends it to the surrounding environment; the Wi-Fi module of the smart device is in the Promiscuous Model and monitors all messages in the network , Until the required information is parsed (the two parties agreed on the data format before).
For more information about the distribution network, please refer to the article:
N ways of Wi-Fi Internet of Things device network distribution
1.3, network connection
Either way, the ultimate goal is to get the SSID/PASSWORD, connect to the AP, and access the network.
The terminal device needs to go through three stages to successfully connect to Wi-Fi:
1) Scanning phase (SCAN);
2) Authentication phase (Authentication);
3) Association (Association).
After the association is successful, the terminal device initiates a DHCP request or uses a static IP address, which means that the network is successfully connected.
2. Important Netmgr API
Netmgr provides a set of API support to facilitate users to quickly access AP.
2.1、netmgr_init
Initialize the netmgr module
Function prototype
int netmgr_init(void);
parameter name | Parameter Description | Parameter example |
无 |
no |
no |
Return parameter
0, success
Less than 0, failure
2.2 、 netmgr_deinit
Uninitialize netmgr
Function prototype
void netmgr_deinit(void);
parameter list
parameter name | Parameter Description | Parameter example |
无 |
no |
no |
Return parameter
no
2.3、netmgr_start
Start netmgr
Function prototype
int netmgr_start(bool autoconfig);
parameter list
parameter name | Parameter Description | Parameter example |
|
Whether to automatically initiate Wi-Fi connection |
no |
Return parameter
0, success
Less than 0, failure
note:
When the parameter autoconfig is true, the automatic Wi-Fi connection function is enabled. If the device has a record of successfully connecting to an AP, it will automatically connect to the SSID and Password in the record of the successfully connected AP.
If the device does not successfully connect to the AP, it will not automatically initiate a network connection. When the parameter autoconfig is false, the function of automatically connecting to the AP is disabled and the AP will not be connected.
2.4、netmgr_connect
Connect Network
Function prototype
int32_t netmgr_connect(const char *ssid, const uint8_t *password, uint32_t timeout);
parameter list
parameter name | Parameter Description | Parameter example |
|
Wi-Fi SSID |
to |
password |
password |
123456 |
timeout |
Timeout (milliseconds) |
100 |
Return parameter
0, success
Other, fail
2.5、netmgr_stats
Get network statistics, now mainly IP address.
Function prototype
void netmgr_stats(int32_t interface, netmgr_stats_t *stats);
parameter list
parameter name | Parameter Description | Parameter example |
|
NIC name |
INTERFACE_WIFI |
stats |
Network card statistics |
/ |
Return parameter
no
3. API usage example
3.1, direct connection
#include "netmgr.h"
#include "aos/yloop.h"
static void wifi_service_event(input_event_t *event, void *priv_data)
{
if (event->type != EV_WIFI) {
return;
}
if (event->code != CODE_WIFI_ON_GOT_IP) {
return;
}
// add application start logic here
}
void start_netmgr(void) {
netmgr_init();
aos_register_event_filter(EV_WIFI, wifi_service_event, NULL);
netmgr_start(false);
netmgr_connect("Test_WiFi", "123456", 10000);
}
3.2. Use history to connect to the Internet
#include "netmgr.h"
#include "aos/yloop.h"
static void wifi_service_event(input_event_t *event, void *priv_data)
{
if (event->type != EV_WIFI) {
return;
}
if (event->code != CODE_WIFI_ON_GOT_IP) {
return;
}
// add application start logic here
}
void start_netmgr(void) {
netmgr_init();
aos_register_event_filter(EV_WIFI, wifi_service_event, NULL);
netmgr_start(true);
}
3.3, use netmgr example
In the above example, a wifi_service_event event is registered to monitor the event. When the type is EV_WIFI and the code is CODE_WIFI_ON_GOT_IP, it means that the IP address is successfully obtained.
Add the start_netmgr function in the above example to the file application/example/helloworld_demo / appdemo.c.
Note that two header files, netmgr.h and aos/yloop.h, need to be added at the same time.
int application_start(int argc, char *argv[])
int count = 0;
printf("nano entry here!\r\n");
start_netmgr();
while(1) {
printf("hello world! count %d \r\n", count++);
aos_msleep(1000);
};
}
3.4, add netmgr module
在application/example/helloworld_demo/Config.in里增加"select AOS_COMP_NETMGR if !AOS_CREATE_PROJECT"
config AOS_APP_HELLOWORLD_DEMO
bool "Helloworld Demo"
select AOS_COMP_OSAL_AOS if !AOS_CREATE_PROJECT
select AOS_COMP_NETMGR if !AOS_CREATE_PROJECT
help
helloworld demo
if AOS_APP_HELLOWORLD_DEMO
# Configurations for app helloworld_demo
config SYSINFO_APP_VERSION
string "Firmware Version"
default "app-1.0.0-20200214.140831"
help
application main firmware version
endif
4. Netmgr command
4.1. Command introduction
Supports Wi-Fi connection-related operations using commands, such as:
-Read/write/delete the stored connection information
-Print the information of all APs in the current network,
-Connect AP, disconnect AP
-Query network status, etc.
Command Line |
Description |
netmgr -t wifi -i |
initialization |
netmgr -t wifi -a [0/1] |
Set whether to automatically reconnect. 0, do not automatically reconnect; 1, automatically reconnect. |
netmgr -t wifi -b [0/1] |
Whether to save historical connection records. 0, do not save historical connection records. 1. Save historical connection records. |
netmgr -t wifi -c [ssid] [password] |
Use ssid password to connect to the Internet |
netmgr -t wifi -e |
Disconnect Wi-Fi connection |
netmgr -t wifi -w [wifi_config] |
Write Wi-Fi configuration files. wifi_config format, such as network={\\nssid=\"aos\"\\npassword=\"123456\"\\nchannel=\"0\"\\n}\\n |
netmgr -t wifi -r |
Read Wi-Fi profile |
netmgr -t wifi -d | Delete Wi-Fi profile |
netmgr -t wifi -p |
Print current network status |
netmgr -t wifi -s |
Print AP information on the current network |
4.2. Command example
Use the following command to quickly connect to the AP whose SSID is "aos" and password is "123456".
netmgr -t wifi -i
netmgr -t wifi -b 1
netmgr -t wifi -a 1
netmgr -t wifi -c aos 123456
You can also write the configuration commands manually, and use the following commands to connect to the network. Among them, if the channel information of the AP cannot be determined, use 0 to scan the entire network segment.
netmgr -t wifi -i
netmgr -t wifi -w network={\\nssid=\"aos\"\\npassword=\"123456\"\\nchannel=\"0\"\\n}\\n
netmgr -t a 1
4.3, Netmgr command use case
Build smart home applications based on HaaS 100
If you want to learn more about the above functions, welcome to visit the AliOS Things Github project homepage and use the latest open source version of AliOS Things.
More functions will be launched in the follow-up open source version of AliOS Things.
5. Developer technical support
If you need more technical support, you can join the Dingding Developer Group
For more technology and solution introduction, please visit the Aliyun AIoT homepage https://iot.aliyun.com/