connect_wlan.cpp 3.9 KB

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