static_ip_example_main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Static IP 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 <netdb.h>
  16. #include "nvs_flash.h"
  17. /* The examples use configuration that you can set via project configuration menu
  18. If you'd rather not, just change the below entries to strings with
  19. the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
  20. */
  21. #define EXAMPLE_WIFI_SSID CONFIG_EXAMPLE_WIFI_SSID
  22. #define EXAMPLE_WIFI_PASS CONFIG_EXAMPLE_WIFI_PASSWORD
  23. #define EXAMPLE_MAXIMUM_RETRY CONFIG_EXAMPLE_MAXIMUM_RETRY
  24. #define EXAMPLE_STATIC_IP_ADDR CONFIG_EXAMPLE_STATIC_IP_ADDR
  25. #define EXAMPLE_STATIC_NETMASK_ADDR CONFIG_EXAMPLE_STATIC_NETMASK_ADDR
  26. #define EXAMPLE_STATIC_GW_ADDR CONFIG_EXAMPLE_STATIC_GW_ADDR
  27. #ifdef CONFIG_EXAMPLE_STATIC_DNS_AUTO
  28. #define EXAMPLE_MAIN_DNS_SERVER EXAMPLE_STATIC_GW_ADDR
  29. #define EXAMPLE_BACKUP_DNS_SERVER "0.0.0.0"
  30. #else
  31. #define EXAMPLE_MAIN_DNS_SERVER CONFIG_EXAMPLE_STATIC_DNS_SERVER_MAIN
  32. #define EXAMPLE_BACKUP_DNS_SERVER CONFIG_EXAMPLE_STATIC_DNS_SERVER_BACKUP
  33. #endif
  34. #ifdef CONFIG_EXAMPLE_STATIC_DNS_RESOLVE_TEST
  35. #define EXAMPLE_RESOLVE_DOMAIN CONFIG_EXAMPLE_STATIC_RESOLVE_DOMAIN
  36. #endif
  37. /* FreeRTOS event group to signal when we are connected*/
  38. static EventGroupHandle_t s_wifi_event_group;
  39. /* The event group allows multiple bits for each event, but we only care about two events:
  40. * - we are connected to the AP with an IP
  41. * - we failed to connect after the maximum amount of retries */
  42. #define WIFI_CONNECTED_BIT BIT0
  43. #define WIFI_FAIL_BIT BIT1
  44. static const char *TAG = "static_ip";
  45. static int s_retry_num = 0;
  46. static esp_err_t example_set_dns_server(esp_netif_t *netif, uint32_t addr, esp_netif_dns_type_t type)
  47. {
  48. if (addr && (addr != IPADDR_NONE)) {
  49. esp_netif_dns_info_t dns;
  50. dns.ip.u_addr.ip4.addr = addr;
  51. dns.ip.type = IPADDR_TYPE_V4;
  52. ESP_ERROR_CHECK(esp_netif_set_dns_info(netif, type, &dns));
  53. }
  54. return ESP_OK;
  55. }
  56. static void example_set_static_ip(esp_netif_t *netif)
  57. {
  58. if (esp_netif_dhcpc_stop(netif) != ESP_OK) {
  59. ESP_LOGE(TAG, "Failed to stop dhcp client");
  60. return;
  61. }
  62. esp_netif_ip_info_t ip;
  63. memset(&ip, 0 , sizeof(esp_netif_ip_info_t));
  64. ip.ip.addr = ipaddr_addr(EXAMPLE_STATIC_IP_ADDR);
  65. ip.netmask.addr = ipaddr_addr(EXAMPLE_STATIC_NETMASK_ADDR);
  66. ip.gw.addr = ipaddr_addr(EXAMPLE_STATIC_GW_ADDR);
  67. if (esp_netif_set_ip_info(netif, &ip) != ESP_OK) {
  68. ESP_LOGE(TAG, "Failed to set ip info");
  69. return;
  70. }
  71. ESP_LOGD(TAG, "Success to set static ip: %s, netmask: %s, gw: %s", EXAMPLE_STATIC_IP_ADDR, EXAMPLE_STATIC_NETMASK_ADDR, EXAMPLE_STATIC_GW_ADDR);
  72. ESP_ERROR_CHECK(example_set_dns_server(netif, ipaddr_addr(EXAMPLE_MAIN_DNS_SERVER), ESP_NETIF_DNS_MAIN));
  73. ESP_ERROR_CHECK(example_set_dns_server(netif, ipaddr_addr(EXAMPLE_BACKUP_DNS_SERVER), ESP_NETIF_DNS_BACKUP));
  74. }
  75. static void event_handler(void* arg, esp_event_base_t event_base,
  76. int32_t event_id, void* event_data)
  77. {
  78. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  79. esp_wifi_connect();
  80. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
  81. example_set_static_ip(arg);
  82. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  83. if (s_retry_num < EXAMPLE_MAXIMUM_RETRY) {
  84. esp_wifi_connect();
  85. s_retry_num++;
  86. ESP_LOGI(TAG, "retry to connect to the AP");
  87. } else {
  88. xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
  89. }
  90. ESP_LOGI(TAG,"connect to the AP fail");
  91. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  92. ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  93. ESP_LOGI(TAG, "static ip:" IPSTR, IP2STR(&event->ip_info.ip));
  94. s_retry_num = 0;
  95. xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  96. }
  97. }
  98. void wifi_init_sta(void)
  99. {
  100. s_wifi_event_group = xEventGroupCreate();
  101. ESP_ERROR_CHECK(esp_netif_init());
  102. ESP_ERROR_CHECK(esp_event_loop_create_default());
  103. esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
  104. assert(sta_netif);
  105. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  106. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  107. esp_event_handler_instance_t instance_any_id;
  108. esp_event_handler_instance_t instance_got_ip;
  109. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  110. ESP_EVENT_ANY_ID,
  111. &event_handler,
  112. sta_netif,
  113. &instance_any_id));
  114. ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
  115. IP_EVENT_STA_GOT_IP,
  116. &event_handler,
  117. sta_netif,
  118. &instance_got_ip));
  119. wifi_config_t wifi_config = {
  120. .sta = {
  121. .ssid = EXAMPLE_WIFI_SSID,
  122. .password = EXAMPLE_WIFI_PASS,
  123. /* Setting a password implies station will connect to all security modes including WEP/WPA.
  124. * However these modes are deprecated and not advisable to be used. Incase your Access point
  125. * doesn't support WPA2, these mode can be enabled by commenting below line */
  126. .threshold.authmode = WIFI_AUTH_WPA2_PSK,
  127. },
  128. };
  129. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  130. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
  131. ESP_ERROR_CHECK(esp_wifi_start() );
  132. ESP_LOGI(TAG, "wifi_init_sta finished.");
  133. /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
  134. * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
  135. EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
  136. WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  137. pdFALSE,
  138. pdFALSE,
  139. portMAX_DELAY);
  140. /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
  141. * happened. */
  142. if (bits & WIFI_CONNECTED_BIT) {
  143. ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
  144. EXAMPLE_WIFI_SSID, EXAMPLE_WIFI_PASS);
  145. } else if (bits & WIFI_FAIL_BIT) {
  146. ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
  147. EXAMPLE_WIFI_SSID, EXAMPLE_WIFI_PASS);
  148. } else {
  149. ESP_LOGE(TAG, "UNEXPECTED EVENT");
  150. }
  151. #ifdef CONFIG_EXAMPLE_STATIC_DNS_RESOLVE_TEST
  152. struct addrinfo *address_info;
  153. struct addrinfo hints;
  154. memset(&hints, 0, sizeof(hints));
  155. hints.ai_family = AF_UNSPEC;
  156. hints.ai_socktype = SOCK_STREAM;
  157. int res = getaddrinfo(EXAMPLE_RESOLVE_DOMAIN, NULL, &hints, &address_info);
  158. if (res != 0 || address_info == NULL) {
  159. ESP_LOGE(TAG, "couldn't get hostname for :%s: "
  160. "getaddrinfo() returns %d, addrinfo=%p", EXAMPLE_RESOLVE_DOMAIN, res, address_info);
  161. } else {
  162. if (address_info->ai_family == AF_INET) {
  163. struct sockaddr_in *p = (struct sockaddr_in *)address_info->ai_addr;
  164. ESP_LOGI(TAG, "Resolved IPv4 address: %s", ipaddr_ntoa((const ip_addr_t*)&p->sin_addr.s_addr));
  165. }
  166. #if CONFIG_LWIP_IPV6
  167. else if (address_info->ai_family == AF_INET6) {
  168. struct sockaddr_in6 *p = (struct sockaddr_in6 *)address_info->ai_addr;
  169. ESP_LOGI(TAG, "Resolved IPv6 address: %s", ip6addr_ntoa((const ip6_addr_t*)&p->sin6_addr));
  170. }
  171. #endif
  172. }
  173. #endif
  174. /* The event will not be processed after unregister */
  175. ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
  176. ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
  177. vEventGroupDelete(s_wifi_event_group);
  178. }
  179. void app_main(void)
  180. {
  181. //Initialize NVS
  182. esp_err_t ret = nvs_flash_init();
  183. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  184. ESP_ERROR_CHECK(nvs_flash_erase());
  185. ret = nvs_flash_init();
  186. }
  187. ESP_ERROR_CHECK(ret);
  188. ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
  189. wifi_init_sta();
  190. }