connect_wlan.cpp 5.1 KB

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