Advertisement
mzimmers

main_cpp

Mar 26th, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #include <string.h>
  2.  
  3. #include "esp_wifi.h"
  4. #include "esp_system.h"
  5. #include "esp_event.h"
  6. #include "esp_event_loop.h"
  7. #include "nvs_flash.h"
  8.  
  9. #include "console.h"
  10. #include "socket.h"
  11.  
  12. esp_err_t event_handler(void *ctx, system_event_t *event)
  13. {
  14. int i;
  15.  
  16. if (event->event_id == SYSTEM_EVENT_SCAN_DONE) {
  17. printf("Number of access points found: %d\n",
  18. event->event_info.scan_done.number);
  19. uint16_t apCount = event->event_info.scan_done.number;
  20. if (apCount == 0) {
  21. return ESP_OK;
  22. }
  23. wifi_ap_record_t *list =
  24. (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * apCount);
  25. ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&apCount, list));
  26. for (i = 0; i < apCount; i++) {
  27. char authmode[64];
  28. switch(list[i].authmode) {
  29. case WIFI_AUTH_OPEN:
  30. strcpy(authmode, "WIFI_AUTH_OPEN");
  31. break;
  32. case WIFI_AUTH_WEP:
  33. strcpy(authmode, "WIFI_AUTH_WEP");
  34. break;
  35. case WIFI_AUTH_WPA_PSK:
  36. strcpy(authmode, "WIFI_AUTH_WPA_PSK");
  37. break;
  38. case WIFI_AUTH_WPA2_PSK:
  39. strcpy(authmode, "WIFI_AUTH_WPA2_PSK");
  40. break;
  41. case WIFI_AUTH_WPA_WPA2_PSK:
  42. strcpy(authmode, "WIFI_AUTH_WPA_WPA2_PSK");
  43. break;
  44. default:
  45. strcpy(authmode, "Unknown");
  46. break;
  47. }
  48. printf("MAC=");
  49. for (int j = 0; j < 5; ++j)
  50. {
  51. printf("%02x:", list[i].bssid[j]);
  52. }
  53. printf("%02x", list[i].bssid[5]);
  54.  
  55. printf(", ssid=%s, rssi=%d, ", list[i].ssid, list[i].rssi);
  56. printf("authmode=%s\n", authmode);
  57. }
  58. free(list);
  59. }
  60. return ESP_OK;
  61. }
  62.  
  63. extern "C" { // needed for the C functions to call it
  64. int app_main(void)
  65. {
  66. int portNbr = PORT_NBR;
  67. nvs_flash_init();
  68. tcpip_adapter_init();
  69.  
  70. ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
  71.  
  72. wifiInit();
  73. socketInit(portNbr);
  74.  
  75. // Let us test a WiFi scan ...
  76. // 2018 03 22 mzimmers: convert these initializers for C++.
  77. // wifi_scan_config_t scanConf = {
  78. // .ssid = NULL,
  79. // .bssid = NULL,
  80. // .channel = 0,
  81. // .show_hidden = 1
  82. wifi_scan_config_t scanConf =
  83. {
  84. NULL,
  85. NULL,
  86. 0,
  87. 1,
  88. WIFI_SCAN_TYPE_ACTIVE,
  89. 0, 1000 // min/max scan times.
  90. };
  91. ESP_ERROR_CHECK(esp_wifi_scan_start(&scanConf, 0));
  92.  
  93. initialize_console();
  94.  
  95. return 0;
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement