connect_wlan.cpp 9.1 KB

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