3.1.7.1 Execution
main.c - Initialize the ATWINC15x0/ATWINC3400 and scan for APs until the defined AP is found.
- Code summary:
- Configure the network
parameters in
main.h.
/** Wi-Fi Settings */ #define MAIN_WLAN_SSID "DEMO_AP" /* < Destination SSID */ #define MAIN_WLAN_AUTH M2M_WIFI_SEC_WPA_PSK /* < Security type */ #define MAIN_WLAN_PSK "12345678" /* < Password for Destination SSID */
- Request to scan in all
channels.
/* Request scan. */ m2m_wifi_request_scan(M2M_WIFI_CH_ALL);
- The
wifi_cb()
function is called with theM2M_WIFI_RESP_SCAN_DONE
message when scanning is done. At this point, a specific scan result can be requested by calling them2m_wifi_req_scan_result()
API with a specific index.static void wifi_cb(uint8_t u8MsgType, void *pvMsg) { case M2M_WIFI_RESP_SCAN_DONE: { tstrM2mScanDone *pstrInfo = (tstrM2mScanDone *)pvMsg; scan_request_index = 0; if (pstrInfo->u8NumofCh >= 1) { m2m_wifi_req_scan_result(scan_request_index); scan_request_index++; }
- The
wifi_cb()
function will be called again with theM2M_WIFI_RESP_SCAN_RESULT
message. The application will get the AP information for the specific result index requested. If the scan result is the same as the AP information in main.h, then the device will connect to the AP.static void wifi_cb(uint8_t u8MsgType, void *pvMsg) { case M2M_WIFI_RESP_SCAN_RESULT: { tstrM2mWifiscanResult *pstrScanResult = (tstrM2mWifiscanResult *)pvMsg; uint16_t demo_ssid_len; uint16_t scan_ssid_len = strlen((const char *)pstrScanResult->au8SSID); /* display AP found. */ printf("[%d] SSID:%s\r\n", scan_request_index, pstrScanResult->au8SSID); num_found_ap = m2m_wifi_get_num_found_ap(); if (scan_ssid_len) { /* check same SSID. */ demo_ssid_len = strlen((const char *)MAIN_WLAN_SSID); if((demo_ssid_len == scan_ssid_len) && (!memcmp(pstrScanResult->au8SSID, (uint8_t *)MAIN_WLAN_SSID, demo_ssid_len))) { printf("Found %s \r\n", MAIN_WLAN_SSID); m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID), MAIN_WLAN_AUTH, (void *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL);
- Configure the network
parameters in
main.h.
- Build the program and download it into the board.
- Start the application.