Writing a program

To write the program, we use gpsdemo. We add the Wi-Fi API to retrieve the SSID names on the current environment and then we start to modify the gpsdemo.c file:

  1. First, we add the required header files to access the Wi-Fi hotspots:
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
  1. Then we define the config_wifi() function to initialize the Wi-Fi service on the ESP32 board. We activate our Wi-Fi service as WIFI_MODE_STA, and start the Wi-Fi up by calling esp_wifi_start().
  2. We will also pass the wifi_scan_event_handler() function to listen to Wi-Fi events from the ESP32 Wi-FI service:
static void config_wifi(void) {
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(wifi_scan_event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
}
  1. Now we can implement the wifi_scan_event_handler() function to listen to Wi-Fi events from the ESP32 Wi-Fi service.
  2. When we get the SYSTEM_EVENT_SCAN_DONE event, we can retrieve a number of SSIDs by calling the esp_wifi_scan_get_ap_num() function:
esp_err_t wifi_scan_event_handler(void *ctx, system_event_t *event)
{
if (event->event_id == SYSTEM_EVENT_SCAN_DONE) {
uint16_t apCount = 0;
esp_wifi_scan_get_ap_num(&apCount);
printf("Wi-Fi found: %d ",event->event_info.scan_done.number);
if (apCount == 0) {
return ESP_OK;
}

  1. Then, we get the details of each SSID data using the esp_wifi_scan_get_ap_records() function:
wifi_ap_record_t *wifi = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * apCount);
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&apCount, wifi));
  1. We map the Wi-Fi authentication model, as follows:
      for (int i=0; i<apCount; i++) {
char *authmode;
switch(wifi[i].authmode) {
case WIFI_AUTH_OPEN:
authmode = "NO AUTH";
break;
case WIFI_AUTH_WEP:
authmode = "WEP";
break;
case WIFI_AUTH_WPA_PSK:
authmode = "WPA PSK";
break;
case WIFI_AUTH_WPA2_PSK:
authmode = "WPA2 PSK";
break;
case WIFI_AUTH_WPA_WPA2_PSK:
authmode = "WPA/WPA2 PSK";
break;
default:
authmode = "Unknown";
break;
}
printf("Lat: %f Long: %f SSID: %15.15s RSSI: %4d AUTH: %10.10s ",latitude, longitude,
wifi[i].ssid, wifi[i].rssi, authmode);
  1. Next, we print all the SSID names with the current position, such as latitude and longitude:
     printf("Lat: %f Long: %f SSID: %15.15s RSSI: %4d AUTH: %10.10s
",latitude, longitude, 
wifi[i].ssid, wifi[i].rssi, authmode);
  1. In the main entry named app_main(), we initialize the Wi-Fi service by calling the config_wifi() function.
  1. We perform looping to scan all Wi-Fi SSIDs using the esp_wifi_scan_start() function:
int app_main(void)
{
ESP_LOGI(TAG, "Configuring flash");
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );

Two important aspects are now addressed. One is to configure the Wi-Fi by calling the config_wifi() function.

The other one is to start a separated freeRTOS task that will configure the UART for GPS connectivity and receive over the serial connection the values for latitude and longitude and those that will be paired with the Wi-Fi networks discovered in the surrounding area.

The latitude and longitude will be available in the other task by using the global variables latitude and longitude.




config_wifi();


ESP_LOGI(TAG, "Configuring UART");
config_gps_uart();

wifi_scan_config_t scanConf = {
.ssid = NULL,
.bssid = NULL,
.channel = 0,
.show_hidden = true
};

while(true){
ESP_ERROR_CHECK(esp_wifi_scan_start(&scanConf, true));
vTaskDelay(3000 / portTICK_PERIOD_MS);
}


return 0;
}

  1. Finally, we save all the changes, and then compile and run our program.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.86.155