connect_wlan.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "connect_wlan.h"
  2. #include <string.h>
  3. #include "esp_wifi.h"
  4. #include "esp_event_loop.h"
  5. #include "freertos/event_groups.h"
  6. #include "esp_log.h"
  7. #include <fstream>
  8. #include <string>
  9. #include <vector>
  10. #include "Helper.h"
  11. static const char *MAIN_TAG = "connect_wlan";
  12. std::string ssid;
  13. std::string passphrase;
  14. std::string hostname;
  15. std::string ipaddress;
  16. std::string std_hostname = "watermeter";
  17. static EventGroupHandle_t wifi_event_group;
  18. #define BLINK_GPIO GPIO_NUM_33
  19. std::vector<string> ZerlegeZeile(std::string input, std::string _delimiter = "")
  20. {
  21. std::vector<string> Output;
  22. std::string delimiter = " =,";
  23. if (_delimiter.length() > 0){
  24. delimiter = _delimiter;
  25. }
  26. input = trim(input, delimiter);
  27. size_t pos = findDelimiterPos(input, delimiter);
  28. std::string token;
  29. while (pos != std::string::npos) {
  30. token = input.substr(0, pos);
  31. token = trim(token, delimiter);
  32. Output.push_back(token);
  33. input.erase(0, pos + 1);
  34. input = trim(input, delimiter);
  35. pos = findDelimiterPos(input, delimiter);
  36. }
  37. Output.push_back(input);
  38. return Output;
  39. }
  40. void wifi_connect(){
  41. wifi_config_t cfg = { };
  42. strcpy((char*)cfg.sta.ssid, (const char*)ssid.c_str());
  43. strcpy((char*)cfg.sta.password, (const char*)passphrase.c_str());
  44. ESP_ERROR_CHECK( esp_wifi_disconnect() );
  45. ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &cfg) );
  46. ESP_ERROR_CHECK( esp_wifi_connect() );
  47. }
  48. void blinkstatus(int dauer, int _anzahl)
  49. {
  50. gpio_reset_pin(BLINK_GPIO);
  51. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  52. for (int i = 0; i < _anzahl; ++i)
  53. {
  54. gpio_set_level(BLINK_GPIO, 0);
  55. vTaskDelay(dauer / portTICK_PERIOD_MS);
  56. gpio_set_level(BLINK_GPIO, 1);
  57. vTaskDelay(dauer / portTICK_PERIOD_MS);
  58. }
  59. }
  60. static esp_err_t event_handler(void *ctx, system_event_t *event)
  61. {
  62. switch(event->event_id) {
  63. case SYSTEM_EVENT_STA_START:
  64. blinkstatus(200, 5);
  65. wifi_connect();
  66. break;
  67. case SYSTEM_EVENT_STA_GOT_IP:
  68. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  69. blinkstatus(1000, 3);
  70. break;
  71. case SYSTEM_EVENT_STA_DISCONNECTED:
  72. blinkstatus(200, 5);
  73. esp_wifi_connect();
  74. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  75. break;
  76. default:
  77. break;
  78. }
  79. return ESP_OK;
  80. }
  81. void initialise_wifi(std::string _ssid, std::string _passphrase, std::string _hostname)
  82. {
  83. ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
  84. wifi_event_group = xEventGroupCreate();
  85. ssid = _ssid;
  86. passphrase = _passphrase;
  87. hostname = _hostname;
  88. esp_log_level_set("wifi", ESP_LOG_NONE); // disable wifi driver logging
  89. tcpip_adapter_init();
  90. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  91. ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  92. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  93. ESP_ERROR_CHECK( esp_wifi_start() );
  94. esp_err_t ret = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA , hostname.c_str());
  95. if(ret != ESP_OK ){
  96. ESP_LOGE(MAIN_TAG,"failed to set hostname:%d",ret);
  97. }
  98. xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,true,true,portMAX_DELAY);
  99. tcpip_adapter_ip_info_t ip_info;
  100. ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
  101. ipaddress = std::string(ip4addr_ntoa(&ip_info.ip));
  102. printf("IPv4 : %s\n", ip4addr_ntoa(&ip_info.ip));
  103. printf("HostName : %s\n", hostname.c_str());
  104. }
  105. void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphrase, std::string &_hostname)
  106. {
  107. string line = "";
  108. std::vector<string> zerlegt;
  109. _hostname = std_hostname;
  110. FILE* pFile;
  111. fn = FormatFileName(fn);
  112. pFile = fopen(fn.c_str(), "r");
  113. if (pFile == NULL)
  114. return;
  115. char zw[1024];
  116. fgets(zw, 1024, pFile);
  117. line = std::string(zw);
  118. while ((line.size() > 0) || !(feof(pFile)))
  119. {
  120. // printf("%s", line.c_str());
  121. zerlegt = ZerlegeZeile(line, "=");
  122. zerlegt[0] = trim(zerlegt[0], " ");
  123. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "HOSTNAME")){
  124. _hostname = trim(zerlegt[1]);
  125. if ((_hostname[0] == '"') && (_hostname[_hostname.length()-1] == '"')){
  126. _hostname = _hostname.substr(1, _hostname.length()-2);
  127. }
  128. }
  129. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "SSID")){
  130. _ssid = trim(zerlegt[1]);
  131. if ((_ssid[0] == '"') && (_ssid[_ssid.length()-1] == '"')){
  132. _ssid = _ssid.substr(1, _ssid.length()-2);
  133. }
  134. }
  135. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "PASSWORD")){
  136. _passphrase = zerlegt[1];
  137. if ((_passphrase[0] == '"') && (_passphrase[_passphrase.length()-1] == '"')){
  138. _passphrase = _passphrase.substr(1, _passphrase.length()-2);
  139. }
  140. }
  141. if (fgets(zw, 1024, pFile) == NULL)
  142. {
  143. line = "";
  144. }
  145. else
  146. {
  147. line = std::string(zw);
  148. }
  149. }
  150. fclose(pFile);
  151. // Check if Hostname was empty in .ini if yes set to std_hostname
  152. if(_hostname.length() <= 0){
  153. _hostname = std_hostname;
  154. }
  155. }
  156. std::string getHostname(){
  157. return hostname;
  158. }
  159. std::string getIPAddress(){
  160. return ipaddress;
  161. }
  162. std::string getSSID(){
  163. return ssid;
  164. }