main.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "esp_wifi.h"
  2. #include "esp_event_loop.h"
  3. #include "esp_log.h"
  4. #include "esp_system.h"
  5. #include "nvs_flash.h"
  6. #include "tests.h"
  7. /* The examples use simple WiFi configuration that you can set via
  8. 'make menuconfig'.
  9. If you'd rather not, just change the below entries to strings with
  10. the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
  11. */
  12. #define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
  13. #define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
  14. static const char *TAG="TEST_WIFI";
  15. static esp_err_t event_handler(void *ctx, system_event_t *event)
  16. {
  17. httpd_handle_t *hd = (httpd_handle_t *) ctx;
  18. switch(event->event_id) {
  19. case SYSTEM_EVENT_STA_START:
  20. ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
  21. esp_wifi_connect();
  22. break;
  23. case SYSTEM_EVENT_STA_GOT_IP:
  24. ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
  25. ESP_LOGI(TAG, "Got IP: '%s'",
  26. ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
  27. // Start webserver tests
  28. if (*hd == NULL) {
  29. *hd = start_tests();
  30. }
  31. break;
  32. case SYSTEM_EVENT_STA_DISCONNECTED:
  33. ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
  34. esp_wifi_connect();
  35. // Stop webserver tests
  36. if (*hd) {
  37. stop_tests(*hd);
  38. *hd = NULL;
  39. }
  40. break;
  41. default:
  42. break;
  43. }
  44. return ESP_OK;
  45. }
  46. static void initialise_wifi(void)
  47. {
  48. tcpip_adapter_init();
  49. static httpd_handle_t hd = NULL;
  50. ESP_ERROR_CHECK(esp_event_loop_init(event_handler, &hd));
  51. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  52. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  53. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  54. wifi_config_t wifi_config = {
  55. .sta = {
  56. .ssid = EXAMPLE_WIFI_SSID,
  57. .password = EXAMPLE_WIFI_PASS,
  58. },
  59. };
  60. ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
  61. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  62. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  63. ESP_ERROR_CHECK(esp_wifi_start());
  64. }
  65. void app_main()
  66. {
  67. ESP_ERROR_CHECK(nvs_flash_init());
  68. initialise_wifi();
  69. }