You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.5 KiB

3 years ago
  1. /* ESPRESSIF MIT License
  2. *
  3. * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  4. *
  5. * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
  6. * it is free of charge, to any person obtaining a copy of this software and associated
  7. * documentation files (the "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  10. * to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all copies or
  13. * substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <string.h>
  23. #include "freertos/FreeRTOS.h"
  24. #include "freertos/task.h"
  25. #include "freertos/event_groups.h"
  26. #include "esp_system.h"
  27. #include "esp_wifi.h"
  28. #include "esp_event_loop.h"
  29. #include "esp_log.h"
  30. #include "nvs_flash.h"
  31. #include "sdkconfig.h"
  32. #include "lwip/err.h"
  33. #include "lwip/sys.h"
  34. #include "mdns.h"
  35. /* The examples use WiFi configuration that you can set via 'make menuconfig'.
  36. If you'd rather not, just change the below entries to strings with
  37. the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
  38. */
  39. #define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
  40. #define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
  41. #define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
  42. #define EXAMPLE_ESP_WIFI_AP_SSID CONFIG_ESP_WIFI_AP_SSID
  43. #define EXAMPLE_ESP_WIFI_AP_PASS CONFIG_ESP_WIFI_AP_PASSWORD
  44. #define EXAMPLE_MAX_STA_CONN CONFIG_MAX_STA_CONN
  45. #define EXAMPLE_IP_ADDR CONFIG_SERVER_IP
  46. #define EXAMPLE_ESP_WIFI_AP_CHANNEL CONFIG_ESP_WIFI_AP_CHANNEL
  47. static const char *TAG = "camera wifi";
  48. static int s_retry_num = 0;
  49. static esp_err_t event_handler(void *ctx, system_event_t *event)
  50. {
  51. switch (event->event_id)
  52. {
  53. case SYSTEM_EVENT_AP_STACONNECTED:
  54. ESP_LOGI(TAG, "station:" MACSTR " join, AID=%d",
  55. MAC2STR(event->event_info.sta_connected.mac),
  56. event->event_info.sta_connected.aid);
  57. break;
  58. case SYSTEM_EVENT_AP_STADISCONNECTED:
  59. ESP_LOGI(TAG, "station:" MACSTR "leave, AID=%d",
  60. MAC2STR(event->event_info.sta_disconnected.mac),
  61. event->event_info.sta_disconnected.aid);
  62. break;
  63. case SYSTEM_EVENT_STA_START:
  64. esp_wifi_connect();
  65. break;
  66. case SYSTEM_EVENT_STA_GOT_IP:
  67. ESP_LOGI(TAG, "got ip:%s",
  68. ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
  69. s_retry_num = 0;
  70. break;
  71. case SYSTEM_EVENT_STA_DISCONNECTED:
  72. {
  73. if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY)
  74. {
  75. esp_wifi_connect();
  76. s_retry_num++;
  77. ESP_LOGI(TAG, "retry to connect to the AP");
  78. }
  79. ESP_LOGI(TAG, "connect to the AP fail");
  80. break;
  81. }
  82. default:
  83. break;
  84. }
  85. mdns_handle_system_event(ctx, event);
  86. return ESP_OK;
  87. }
  88. void wifi_init_softap()
  89. {
  90. if (strcmp(EXAMPLE_IP_ADDR, "192.168.4.1"))
  91. {
  92. int a, b, c, d;
  93. sscanf(EXAMPLE_IP_ADDR, "%d.%d.%d.%d", &a, &b, &c, &d);
  94. tcpip_adapter_ip_info_t ip_info;
  95. IP4_ADDR(&ip_info.ip, a, b, c, d);
  96. IP4_ADDR(&ip_info.gw, a, b, c, d);
  97. IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);
  98. ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(WIFI_IF_AP));
  99. ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(WIFI_IF_AP, &ip_info));
  100. ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(WIFI_IF_AP));
  101. }
  102. wifi_config_t wifi_config;
  103. memset(&wifi_config, 0, sizeof(wifi_config_t));
  104. snprintf((char *)wifi_config.ap.ssid, 32, "%s", EXAMPLE_ESP_WIFI_AP_SSID);
  105. wifi_config.ap.ssid_len = strlen((char *)wifi_config.ap.ssid);
  106. snprintf((char *)wifi_config.ap.password, 64, "%s", EXAMPLE_ESP_WIFI_AP_PASS);
  107. wifi_config.ap.max_connection = EXAMPLE_MAX_STA_CONN;
  108. wifi_config.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;
  109. if (strlen(EXAMPLE_ESP_WIFI_AP_PASS) == 0)
  110. {
  111. wifi_config.ap.authmode = WIFI_AUTH_OPEN;
  112. }
  113. if (strlen(EXAMPLE_ESP_WIFI_AP_CHANNEL))
  114. {
  115. int channel;
  116. sscanf(EXAMPLE_ESP_WIFI_AP_CHANNEL, "%d", &channel);
  117. wifi_config.ap.channel = channel;
  118. }
  119. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
  120. ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s",
  121. EXAMPLE_ESP_WIFI_AP_SSID, EXAMPLE_ESP_WIFI_AP_PASS);
  122. }
  123. void wifi_init_sta()
  124. {
  125. wifi_config_t wifi_config;
  126. memset(&wifi_config, 0, sizeof(wifi_config_t));
  127. snprintf((char *)wifi_config.sta.ssid, 32, "%s", EXAMPLE_ESP_WIFI_SSID);
  128. snprintf((char *)wifi_config.sta.password, 64, "%s", EXAMPLE_ESP_WIFI_PASS);
  129. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  130. ESP_LOGI(TAG, "wifi_init_sta finished.");
  131. ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
  132. EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
  133. }
  134. void app_wifi_main()
  135. {
  136. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  137. wifi_mode_t mode = WIFI_MODE_NULL;
  138. if (strlen(EXAMPLE_ESP_WIFI_AP_SSID) && strlen(EXAMPLE_ESP_WIFI_SSID))
  139. {
  140. mode = WIFI_MODE_APSTA;
  141. }
  142. else if (strlen(EXAMPLE_ESP_WIFI_AP_SSID))
  143. {
  144. mode = WIFI_MODE_AP;
  145. }
  146. else if (strlen(EXAMPLE_ESP_WIFI_SSID))
  147. {
  148. mode = WIFI_MODE_STA;
  149. }
  150. esp_err_t ret = nvs_flash_init();
  151. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
  152. {
  153. ESP_ERROR_CHECK(nvs_flash_erase());
  154. ret = nvs_flash_init();
  155. }
  156. ESP_ERROR_CHECK(ret);
  157. if (mode == WIFI_MODE_NULL)
  158. {
  159. ESP_LOGW(TAG, "Neither AP or STA have been configured. WiFi will be off.");
  160. return;
  161. }
  162. tcpip_adapter_init();
  163. ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
  164. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  165. ESP_ERROR_CHECK(esp_wifi_set_mode(mode));
  166. if (mode & WIFI_MODE_AP)
  167. {
  168. wifi_init_softap();
  169. }
  170. if (mode & WIFI_MODE_STA)
  171. {
  172. wifi_init_sta();
  173. }
  174. ESP_ERROR_CHECK(esp_wifi_start());
  175. ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
  176. }