connect_wlan.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "connect_wlan.h"
  2. #include <string.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "freertos/event_groups.h"
  6. #include "driver/gpio.h"
  7. #include "esp_system.h"
  8. #include "esp_wifi.h"
  9. #include "esp_event.h"
  10. #include "esp_log.h"
  11. #include "nvs_flash.h"
  12. #include "lwip/err.h"
  13. #include "lwip/sys.h"
  14. #include "lwip/ip_addr.h"
  15. #include "lwip/inet.h"
  16. #include <fstream>
  17. #include <string>
  18. #include <vector>
  19. #include <sstream>
  20. #include <iostream>
  21. #define __HIDE_PASSWORD
  22. #define EXAMPLE_ESP_MAXIMUM_RETRY 1000
  23. /* FreeRTOS event group to signal when we are connected*/
  24. static EventGroupHandle_t s_wifi_event_group;
  25. /* The event group allows multiple bits for each event, but we only care about two events:
  26. * - we are connected to the AP with an IP
  27. * - we failed to connect after the maximum amount of retries */
  28. #define WIFI_CONNECTED_BIT BIT0
  29. #define WIFI_FAIL_BIT BIT1
  30. static const char *TAG = "wifi station";
  31. static esp_netif_t *my_sta;
  32. static int s_retry_num = 0;
  33. ///////////////////////////////////////////////////////////
  34. #ifdef BLINK_GPIO
  35. #undef BLINK_GPIO
  36. #endif
  37. #define BLINK_GPIO GPIO_NUM_33
  38. int BlinkDauer;
  39. int BlinkAnzahl;
  40. bool BlinkOff;
  41. bool BlinkIsRunning = false;
  42. std::string hostname = "";
  43. std::string std_hostname = "watermeter";
  44. std::string ipadress = "";
  45. std::string ssid = "";
  46. std::string* getIPAddress()
  47. {
  48. return &ipadress;
  49. }
  50. std::string* getSSID()
  51. {
  52. return &ssid;
  53. }
  54. void task_doBlink(void *pvParameter)
  55. {
  56. ESP_LOGI("BLINK", "Flash - start");
  57. while (BlinkIsRunning)
  58. {
  59. // ESP_LOGI("BLINK", "Blinken - wait");
  60. vTaskDelay(100 / portTICK_PERIOD_MS);
  61. }
  62. BlinkIsRunning = true;
  63. // Init the GPIO
  64. #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0))
  65. esp_rom_gpio_pad_select_gpio(BLINK_GPIO);
  66. #else
  67. gpio_pad_select_gpio(BLINK_GPIO);
  68. #endif
  69. /* Set the GPIO as a push/pull output */
  70. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  71. for (int i = 0; i < BlinkAnzahl; ++i)
  72. {
  73. if (BlinkAnzahl > 1)
  74. {
  75. gpio_set_level(BLINK_GPIO, 1);
  76. vTaskDelay(BlinkDauer / portTICK_PERIOD_MS);
  77. }
  78. gpio_set_level(BLINK_GPIO, 0);
  79. vTaskDelay(BlinkDauer / portTICK_PERIOD_MS);
  80. }
  81. if (BlinkOff)
  82. gpio_set_level(BLINK_GPIO, 1);
  83. ESP_LOGI("BLINK", "Flash - done");
  84. BlinkIsRunning = false;
  85. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  86. }
  87. void LEDBlinkTask(int _dauer, int _anz, bool _off)
  88. {
  89. BlinkDauer = _dauer;
  90. BlinkAnzahl = _anz;
  91. BlinkOff = _off;
  92. xTaskCreate(&task_doBlink, "task_doBlink", configMINIMAL_STACK_SIZE * 8, NULL, tskIDLE_PRIORITY+1, NULL);
  93. }
  94. /////////////////////////////////////////////////////////
  95. static void event_handler(void* arg, esp_event_base_t event_base,
  96. int32_t event_id, void* event_data)
  97. {
  98. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  99. LEDBlinkTask(200, 1, true);
  100. esp_wifi_connect();
  101. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  102. // if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
  103. esp_wifi_connect();
  104. s_retry_num++;
  105. ESP_LOGI(TAG, "retrying connection to the AP");
  106. // } else {
  107. // xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
  108. // }
  109. ESP_LOGI(TAG,"connection to the AP failed");
  110. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  111. ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  112. ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
  113. char ip_addr[16];
  114. inet_ntoa_r(event->ip_info.ip.addr, ip_addr, 16);
  115. ipadress = std::string(ip_addr);
  116. s_retry_num = 0;
  117. xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  118. LEDBlinkTask(1000, 5, true);
  119. }
  120. }
  121. void strinttoip4(const char *ip, int &a, int &b, int &c, int &d) {
  122. std::string zw = std::string(ip);
  123. std::stringstream s(zw);
  124. char ch; //to temporarily store the '.'
  125. s >> a >> ch >> b >> ch >> c >> ch >> d;
  126. }
  127. void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostname, const char *_ipadr, const char *_gw, const char *_netmask, const char *_dns)
  128. {
  129. s_wifi_event_group = xEventGroupCreate();
  130. ESP_ERROR_CHECK(esp_netif_init());
  131. ESP_ERROR_CHECK(esp_event_loop_create_default());
  132. my_sta = esp_netif_create_default_wifi_sta();
  133. if ((_ipadr != NULL) && (_gw != NULL) && (_netmask != NULL))
  134. {
  135. /*
  136. tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
  137. tcpip_adapter_ip_info_t ip_info;
  138. int a, b, c, d;
  139. strinttoip4(_ipadr, a, b, c, d);
  140. IP4_ADDR(&ip_info.ip, a, b, c, d);
  141. strinttoip4(_gw, a, b, c, d);
  142. IP4_ADDR(&ip_info.gw, a, b, c, d);
  143. strinttoip4(_netmask, a, b, c, d);
  144. IP4_ADDR(&ip_info.netmask, a, b, c, d);
  145. tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info);
  146. */
  147. ESP_LOGI(TAG, "set IP %s, GW %s, Netmask %s manual", _ipadr, _gw, _netmask);
  148. esp_netif_dhcpc_stop(my_sta);
  149. esp_netif_ip_info_t ip_info;
  150. int a, b, c, d;
  151. strinttoip4(_ipadr, a, b, c, d);
  152. IP4_ADDR(&ip_info.ip, a, b, c, d);
  153. strinttoip4(_gw, a, b, c, d);
  154. IP4_ADDR(&ip_info.gw, a, b, c, d);
  155. strinttoip4(_netmask, a, b, c, d);
  156. IP4_ADDR(&ip_info.netmask, a, b, c, d);
  157. esp_netif_set_ip_info(my_sta, &ip_info);
  158. }
  159. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  160. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  161. if ((_ipadr != NULL) && (_gw != NULL) && (_netmask != NULL))
  162. {
  163. if (_dns == NULL)
  164. _dns = _gw;
  165. ESP_LOGI(TAG, "set DNS manual");
  166. esp_netif_dns_info_t dns_info;
  167. ip4_addr_t ip;
  168. ip.addr = esp_ip4addr_aton(_dns);
  169. ip_addr_set_ip4_u32(&dns_info.ip, ip.addr);
  170. ESP_ERROR_CHECK(esp_netif_set_dns_info(my_sta, ESP_NETIF_DNS_MAIN, &dns_info));
  171. }
  172. esp_event_handler_instance_t instance_any_id;
  173. esp_event_handler_instance_t instance_got_ip;
  174. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  175. ESP_EVENT_ANY_ID,
  176. &event_handler,
  177. NULL,
  178. &instance_any_id));
  179. ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
  180. IP_EVENT_STA_GOT_IP,
  181. &event_handler,
  182. NULL,
  183. &instance_got_ip));
  184. wifi_config_t wifi_config = { };
  185. strcpy((char*)wifi_config.sta.ssid, (const char*)_ssid);
  186. strcpy((char*)wifi_config.sta.password, (const char*)_password);
  187. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  188. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
  189. ESP_ERROR_CHECK(esp_wifi_start() );
  190. if (_hostname != NULL)
  191. {
  192. esp_err_t ret = esp_netif_set_hostname(my_sta, _hostname);
  193. hostname = std::string(_hostname);
  194. if(ret != ESP_OK ){
  195. ESP_LOGE(TAG,"failed to set hostname:%d",ret);
  196. }
  197. else {
  198. ESP_LOGE(TAG,"Set Hostname to:%s", _hostname);
  199. }
  200. }
  201. ESP_LOGI(TAG, "wifi_init_sta finished.");
  202. /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
  203. * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
  204. EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
  205. WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  206. pdFALSE,
  207. pdFALSE,
  208. portMAX_DELAY);
  209. /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
  210. * happened. */
  211. if (bits & WIFI_CONNECTED_BIT) {
  212. #ifdef __HIDE_PASSWORD
  213. ESP_LOGI(TAG, "connected to ap SSID: %s, password: XXXXXXX", _ssid);
  214. #else
  215. ESP_LOGI(TAG, "connected to ap SSID: %s, password: %s", _ssid, _password);
  216. #endif
  217. } else if (bits & WIFI_FAIL_BIT) {
  218. #ifdef __HIDE_PASSWORD
  219. ESP_LOGI(TAG, "Failed to connect to SSID: %s, password: XXXXXXXX", _ssid);
  220. #else
  221. ESP_LOGI(TAG, "Failed to connect to SSID: %s, password: %s", _ssid, _password);
  222. #endif
  223. } else {
  224. ESP_LOGE(TAG, "UNEXPECTED EVENT");
  225. }
  226. ssid = std::string(_ssid);
  227. /* The event will not be processed after unregister */
  228. // ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
  229. // ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
  230. // vEventGroupDelete(s_wifi_event_group);
  231. }
  232. int get_WIFI_RSSI()
  233. {
  234. wifi_ap_record_t ap;
  235. esp_wifi_sta_get_ap_info(&ap);
  236. return ap.rssi;
  237. }
  238. void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostname)
  239. {
  240. wifi_init_sta(_ssid, _password, _hostname, NULL, NULL, NULL, NULL);
  241. }
  242. void wifi_init_sta(const char *_ssid, const char *_password)
  243. {
  244. wifi_init_sta(_ssid, _password, NULL, NULL, NULL, NULL, NULL);
  245. }