connect_wlan.cpp 9.5 KB

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