connect_wlan.cpp 12 KB

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