|
| 1 | +/* Static IP Example |
| 2 | +
|
| 3 | + This example code is in the Public Domain (or CC0 licensed, at your option.) |
| 4 | +
|
| 5 | + Unless required by applicable law or agreed to in writing, this |
| 6 | + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 7 | + CONDITIONS OF ANY KIND, either express or implied. |
| 8 | +*/ |
| 9 | +#include <string.h> |
| 10 | +#include "freertos/FreeRTOS.h" |
| 11 | +#include "freertos/task.h" |
| 12 | +#include "freertos/event_groups.h" |
| 13 | +#include "esp_system.h" |
| 14 | +#include "esp_wifi.h" |
| 15 | +#include "esp_event.h" |
| 16 | +#include "esp_log.h" |
| 17 | +#include "nvs_flash.h" |
| 18 | + |
| 19 | +/* The examples use configuration that you can set via project configuration menu |
| 20 | +
|
| 21 | + If you'd rather not, just change the below entries to strings with |
| 22 | + the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" |
| 23 | +*/ |
| 24 | +#define EXAMPLE_WIFI_SSID CONFIG_EXAMPLE_WIFI_SSID |
| 25 | +#define EXAMPLE_WIFI_PASS CONFIG_EXAMPLE_WIFI_PASSWORD |
| 26 | +#define EXAMPLE_MAXIMUM_RETRY CONFIG_EXAMPLE_MAXIMUM_RETRY |
| 27 | +#define EXAMPLE_STATIC_IP_ADDR CONFIG_EXAMPLE_STATIC_IP_ADDR |
| 28 | +#define EXAMPLE_STATIC_NETMASK_ADDR CONFIG_EXAMPLE_STATIC_NETMASK_ADDR |
| 29 | +#define EXAMPLE_STATIC_GW_ADDR CONFIG_EXAMPLE_STATIC_GW_ADDR |
| 30 | + |
| 31 | +/* FreeRTOS event group to signal when we are connected*/ |
| 32 | +static EventGroupHandle_t s_wifi_event_group; |
| 33 | + |
| 34 | +/* The event group allows multiple bits for each event, but we only care about two events: |
| 35 | + * - we are connected to the AP with an IP |
| 36 | + * - we failed to connect after the maximum amount of retries */ |
| 37 | +#define WIFI_CONNECTED_BIT BIT0 |
| 38 | +#define WIFI_FAIL_BIT BIT1 |
| 39 | + |
| 40 | +static const char *TAG = "static_ip"; |
| 41 | + |
| 42 | +static int s_retry_num = 0; |
| 43 | + |
| 44 | +static void example_set_static_ip(esp_netif_t *netif) |
| 45 | +{ |
| 46 | + if (esp_netif_dhcpc_stop(netif) != ESP_OK) { |
| 47 | + ESP_LOGE(TAG, "Failed to stop dhcp client"); |
| 48 | + return; |
| 49 | + } |
| 50 | + esp_netif_ip_info_t ip; |
| 51 | + memset(&ip, 0 , sizeof(esp_netif_ip_info_t)); |
| 52 | + ip.ip.addr = ipaddr_addr(EXAMPLE_STATIC_IP_ADDR); |
| 53 | + ip.netmask.addr = ipaddr_addr(EXAMPLE_STATIC_NETMASK_ADDR); |
| 54 | + ip.gw.addr = ipaddr_addr(EXAMPLE_STATIC_GW_ADDR); |
| 55 | + if (esp_netif_set_ip_info(netif, &ip) != ESP_OK) { |
| 56 | + ESP_LOGE(TAG, "Failed to set ip info"); |
| 57 | + return; |
| 58 | + } |
| 59 | + 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); |
| 60 | +} |
| 61 | + |
| 62 | +static void event_handler(void* arg, esp_event_base_t event_base, |
| 63 | + int32_t event_id, void* event_data) |
| 64 | +{ |
| 65 | + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { |
| 66 | + esp_wifi_connect(); |
| 67 | + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) { |
| 68 | + example_set_static_ip(arg); |
| 69 | + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { |
| 70 | + if (s_retry_num < EXAMPLE_MAXIMUM_RETRY) { |
| 71 | + esp_wifi_connect(); |
| 72 | + s_retry_num++; |
| 73 | + ESP_LOGI(TAG, "retry to connect to the AP"); |
| 74 | + } else { |
| 75 | + xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); |
| 76 | + } |
| 77 | + ESP_LOGI(TAG,"connect to the AP fail"); |
| 78 | + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { |
| 79 | + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; |
| 80 | + ESP_LOGI(TAG, "static ip:" IPSTR, IP2STR(&event->ip_info.ip)); |
| 81 | + s_retry_num = 0; |
| 82 | + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void wifi_init_sta(void) |
| 87 | +{ |
| 88 | + s_wifi_event_group = xEventGroupCreate(); |
| 89 | + |
| 90 | + ESP_ERROR_CHECK(esp_netif_init()); |
| 91 | + |
| 92 | + ESP_ERROR_CHECK(esp_event_loop_create_default()); |
| 93 | + |
| 94 | + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); |
| 95 | + assert(sta_netif); |
| 96 | + |
| 97 | + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); |
| 98 | + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); |
| 99 | + |
| 100 | + esp_event_handler_instance_t instance_any_id; |
| 101 | + esp_event_handler_instance_t instance_got_ip; |
| 102 | + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, |
| 103 | + ESP_EVENT_ANY_ID, |
| 104 | + &event_handler, |
| 105 | + sta_netif, |
| 106 | + &instance_any_id)); |
| 107 | + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, |
| 108 | + IP_EVENT_STA_GOT_IP, |
| 109 | + &event_handler, |
| 110 | + sta_netif, |
| 111 | + &instance_got_ip)); |
| 112 | + |
| 113 | + wifi_config_t wifi_config = { |
| 114 | + .sta = { |
| 115 | + .ssid = EXAMPLE_WIFI_SSID, |
| 116 | + .password = EXAMPLE_WIFI_PASS, |
| 117 | + /* Setting a password implies station will connect to all security modes including WEP/WPA. |
| 118 | + * However these modes are deprecated and not advisable to be used. Incase your Access point |
| 119 | + * doesn't support WPA2, these mode can be enabled by commenting below line */ |
| 120 | + .threshold.authmode = WIFI_AUTH_WPA2_PSK, |
| 121 | + |
| 122 | + .pmf_cfg = { |
| 123 | + .capable = true, |
| 124 | + .required = false |
| 125 | + }, |
| 126 | + }, |
| 127 | + }; |
| 128 | + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); |
| 129 | + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) ); |
| 130 | + ESP_ERROR_CHECK(esp_wifi_start() ); |
| 131 | + |
| 132 | + ESP_LOGI(TAG, "wifi_init_sta finished."); |
| 133 | + |
| 134 | + /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum |
| 135 | + * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ |
| 136 | + EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, |
| 137 | + WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, |
| 138 | + pdFALSE, |
| 139 | + pdFALSE, |
| 140 | + portMAX_DELAY); |
| 141 | + |
| 142 | + /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually |
| 143 | + * happened. */ |
| 144 | + if (bits & WIFI_CONNECTED_BIT) { |
| 145 | + ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", |
| 146 | + EXAMPLE_WIFI_SSID, EXAMPLE_WIFI_PASS); |
| 147 | + } else if (bits & WIFI_FAIL_BIT) { |
| 148 | + ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", |
| 149 | + EXAMPLE_WIFI_SSID, EXAMPLE_WIFI_PASS); |
| 150 | + } else { |
| 151 | + ESP_LOGE(TAG, "UNEXPECTED EVENT"); |
| 152 | + } |
| 153 | + |
| 154 | + /* The event will not be processed after unregister */ |
| 155 | + ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip)); |
| 156 | + ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id)); |
| 157 | + vEventGroupDelete(s_wifi_event_group); |
| 158 | +} |
| 159 | + |
| 160 | +void app_main(void) |
| 161 | +{ |
| 162 | + //Initialize NVS |
| 163 | + esp_err_t ret = nvs_flash_init(); |
| 164 | + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
| 165 | + ESP_ERROR_CHECK(nvs_flash_erase()); |
| 166 | + ret = nvs_flash_init(); |
| 167 | + } |
| 168 | + ESP_ERROR_CHECK(ret); |
| 169 | + |
| 170 | + ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); |
| 171 | + wifi_init_sta(); |
| 172 | +} |
0 commit comments