connect_wlan.cpp 4.5 KB

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