connect_wlan.cpp 5.0 KB

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