app_main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Local Ctrl Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/event_groups.h"
  11. #include "esp_system.h"
  12. #include "esp_wifi.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "nvs_flash.h"
  16. #include "lwip/err.h"
  17. #include "lwip/sys.h"
  18. /* The examples use WiFi configuration that you can set via 'idf.py menuconfig'.
  19. If you'd rather not, just change the below entries to strings with
  20. the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
  21. */
  22. #define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
  23. #define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
  24. #define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
  25. /* FreeRTOS event group to signal when we are connected*/
  26. static EventGroupHandle_t s_wifi_event_group;
  27. /* The event group allows multiple bits for each event, but we only care about one event
  28. * - are we connected to the AP with an IP? */
  29. const int WIFI_CONNECTED_BIT = BIT0;
  30. static const char *TAG = "local_ctrl_example";
  31. static int s_retry_num = 0;
  32. static void event_handler(void* arg, esp_event_base_t event_base,
  33. int32_t event_id, void* event_data)
  34. {
  35. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  36. esp_wifi_connect();
  37. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  38. if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
  39. esp_wifi_connect();
  40. xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  41. s_retry_num++;
  42. ESP_LOGI(TAG, "retry to connect to the AP");
  43. }
  44. ESP_LOGI(TAG,"connect to the AP fail");
  45. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  46. ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  47. ESP_LOGI(TAG, "got ip:%s",
  48. ip4addr_ntoa(&event->ip_info.ip));
  49. s_retry_num = 0;
  50. xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  51. }
  52. }
  53. void wifi_init_sta(void)
  54. {
  55. s_wifi_event_group = xEventGroupCreate();
  56. tcpip_adapter_init();
  57. ESP_ERROR_CHECK(esp_event_loop_create_default());
  58. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  59. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  60. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  61. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
  62. wifi_config_t wifi_config = {
  63. .sta = {
  64. .ssid = EXAMPLE_ESP_WIFI_SSID,
  65. .password = EXAMPLE_ESP_WIFI_PASS
  66. },
  67. };
  68. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  69. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  70. ESP_ERROR_CHECK(esp_wifi_start() );
  71. ESP_LOGI(TAG, "wifi_init_sta finished.");
  72. ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
  73. EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
  74. }
  75. /* Function responsible for configuring and starting the esp_local_ctrl service.
  76. * See local_ctrl_service.c for implementation */
  77. extern void start_esp_local_ctrl_service(void);
  78. void app_main(void)
  79. {
  80. //Initialize NVS
  81. esp_err_t ret = nvs_flash_init();
  82. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  83. ESP_ERROR_CHECK(nvs_flash_erase());
  84. ret = nvs_flash_init();
  85. }
  86. ESP_ERROR_CHECK(ret);
  87. ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
  88. wifi_init_sta();
  89. start_esp_local_ctrl_service();
  90. }