connect_wlan.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <sstream>
  11. #include <iostream>
  12. #include <arpa/inet.h>
  13. #include "Helper.h"
  14. static const char *MAIN_TAG = "connect_wlan";
  15. std::string ssid;
  16. std::string passphrase;
  17. std::string hostname;
  18. std::string ipaddress;
  19. std::string gw;
  20. std::string netmask;
  21. std::string std_hostname = "watermeter";
  22. static EventGroupHandle_t wifi_event_group;
  23. #define BLINK_GPIO GPIO_NUM_33
  24. std::vector<string> ZerlegeZeile(std::string input, std::string _delimiter = "")
  25. {
  26. std::vector<string> Output;
  27. std::string delimiter = " =,";
  28. if (_delimiter.length() > 0){
  29. delimiter = _delimiter;
  30. }
  31. input = trim(input, delimiter);
  32. size_t pos = findDelimiterPos(input, delimiter);
  33. std::string token;
  34. while (pos != std::string::npos) {
  35. token = input.substr(0, pos);
  36. token = trim(token, delimiter);
  37. Output.push_back(token);
  38. input.erase(0, pos + 1);
  39. input = trim(input, delimiter);
  40. pos = findDelimiterPos(input, delimiter);
  41. }
  42. Output.push_back(input);
  43. return Output;
  44. }
  45. void wifi_connect(){
  46. wifi_config_t cfg = { };
  47. strcpy((char*)cfg.sta.ssid, (const char*)ssid.c_str());
  48. strcpy((char*)cfg.sta.password, (const char*)passphrase.c_str());
  49. ESP_ERROR_CHECK( esp_wifi_disconnect() );
  50. ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &cfg) );
  51. ESP_ERROR_CHECK( esp_wifi_connect() );
  52. }
  53. void blinkstatus(int dauer, int _anzahl)
  54. {
  55. gpio_reset_pin(BLINK_GPIO);
  56. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  57. for (int i = 0; i < _anzahl; ++i)
  58. {
  59. gpio_set_level(BLINK_GPIO, 0);
  60. vTaskDelay(dauer / portTICK_PERIOD_MS);
  61. gpio_set_level(BLINK_GPIO, 1);
  62. vTaskDelay(dauer / portTICK_PERIOD_MS);
  63. }
  64. }
  65. static esp_err_t event_handler(void *ctx, system_event_t *event)
  66. {
  67. switch(event->event_id) {
  68. case SYSTEM_EVENT_STA_START:
  69. blinkstatus(200, 1);
  70. wifi_connect();
  71. break;
  72. case SYSTEM_EVENT_STA_GOT_IP:
  73. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  74. blinkstatus(1000, 3);
  75. break;
  76. case SYSTEM_EVENT_STA_DISCONNECTED:
  77. blinkstatus(200, 5);
  78. esp_wifi_connect();
  79. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  80. break;
  81. default:
  82. break;
  83. }
  84. return ESP_OK;
  85. }
  86. void initialise_wifi(std::string _ssid, std::string _passphrase, std::string _hostname)
  87. {
  88. ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
  89. wifi_event_group = xEventGroupCreate();
  90. ssid = _ssid;
  91. passphrase = _passphrase;
  92. hostname = _hostname;
  93. esp_log_level_set("wifi", ESP_LOG_NONE); // disable wifi driver logging
  94. tcpip_adapter_init();
  95. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  96. ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  97. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  98. ESP_ERROR_CHECK( esp_wifi_start() );
  99. esp_err_t ret = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA , hostname.c_str());
  100. if(ret != ESP_OK ){
  101. ESP_LOGE(MAIN_TAG,"failed to set hostname:%d",ret);
  102. }
  103. xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,true,true,portMAX_DELAY);
  104. tcpip_adapter_ip_info_t ip_info;
  105. ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
  106. ipaddress = std::string(ip4addr_ntoa(&ip_info.ip));
  107. netmask = std::string(ip4addr_ntoa(&ip_info.netmask));
  108. gw = std::string(ip4addr_ntoa(&ip_info.gw));
  109. printf("IPv4 : %s\n", ip4addr_ntoa(&ip_info.ip));
  110. printf("HostName : %s\n", hostname.c_str());
  111. }
  112. ///////////////////////////////////////////////////////////
  113. ///////////////////////////////////////////////////////////
  114. void strinttoip4(std::string ip, int &a, int &b, int &c, int &d) {
  115. std::stringstream s(ip);
  116. char ch; //to temporarily store the '.'
  117. s >> a >> ch >> b >> ch >> c >> ch >> d;
  118. }
  119. void initialise_wifi_fixed_ip(std::string _ip, std::string _gw, std::string _netmask, std::string _ssid, std::string _passphrase, std::string _hostname)
  120. {
  121. ssid = _ssid;
  122. passphrase = _passphrase;
  123. hostname = _hostname;
  124. wifi_event_group = xEventGroupCreate();
  125. ESP_ERROR_CHECK(esp_netif_init());
  126. ESP_ERROR_CHECK(esp_event_loop_create_default());
  127. esp_netif_t *my_sta = esp_netif_create_default_wifi_sta();
  128. esp_netif_dhcpc_stop(my_sta);
  129. esp_netif_ip_info_t ip_info;
  130. int a, b, c, d;
  131. strinttoip4(_ip, a, b, c, d);
  132. IP4_ADDR(&ip_info.ip, a, b, c, d);
  133. strinttoip4(_gw, a, b, c, d);
  134. IP4_ADDR(&ip_info.gw, a, b, c, d);
  135. strinttoip4(_netmask, a, b, c, d);
  136. IP4_ADDR(&ip_info.netmask, a, b, c, d);
  137. esp_netif_set_ip_info(my_sta, &ip_info);
  138. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  139. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  140. ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
  141. // ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  142. // ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
  143. wifi_config_t wifi_config = { };
  144. strcpy((char*)wifi_config.sta.ssid, (const char*)ssid.c_str());
  145. strcpy((char*)wifi_config.sta.password, (const char*)passphrase.c_str());
  146. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  147. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  148. ESP_ERROR_CHECK(esp_wifi_start() );
  149. ESP_LOGI(MAIN_TAG, "wifi_init_sta finished.");
  150. EventBits_t bits = xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,true,true,portMAX_DELAY);
  151. // EventBits_t bits = xEventGroupWaitBits(wifi_event_group,
  152. // WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  153. // pdFALSE,
  154. // pdFALSE,
  155. // portMAX_DELAY);
  156. if (bits & CONNECTED_BIT) {
  157. ESP_LOGI(MAIN_TAG, "connected to ap SSID:%s password:%s",
  158. ssid.c_str(), passphrase.c_str());
  159. } else {
  160. ESP_LOGI(MAIN_TAG, "Failed to connect to SSID:%s, password:%s",
  161. ssid.c_str(), passphrase.c_str());
  162. }
  163. // ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
  164. // ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
  165. vEventGroupDelete(wifi_event_group);
  166. }
  167. ///////////////////////////////////////////////////////////
  168. ///////////////////////////////////////////////////////////
  169. void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphrase, std::string &_hostname, std::string &_ip, std::string &_gw, std::string &_netmask)
  170. {
  171. string line = "";
  172. std::vector<string> zerlegt;
  173. _hostname = std_hostname;
  174. FILE* pFile;
  175. fn = FormatFileName(fn);
  176. pFile = fopen(fn.c_str(), "r");
  177. if (pFile == NULL)
  178. return;
  179. char zw[1024];
  180. fgets(zw, 1024, pFile);
  181. line = std::string(zw);
  182. while ((line.size() > 0) || !(feof(pFile)))
  183. {
  184. // printf("%s", line.c_str());
  185. zerlegt = ZerlegeZeile(line, "=");
  186. zerlegt[0] = trim(zerlegt[0], " ");
  187. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "HOSTNAME")){
  188. _hostname = trim(zerlegt[1]);
  189. if ((_hostname[0] == '"') && (_hostname[_hostname.length()-1] == '"')){
  190. _hostname = _hostname.substr(1, _hostname.length()-2);
  191. }
  192. }
  193. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "SSID")){
  194. _ssid = trim(zerlegt[1]);
  195. if ((_ssid[0] == '"') && (_ssid[_ssid.length()-1] == '"')){
  196. _ssid = _ssid.substr(1, _ssid.length()-2);
  197. }
  198. }
  199. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "PASSWORD")){
  200. _passphrase = zerlegt[1];
  201. if ((_passphrase[0] == '"') && (_passphrase[_passphrase.length()-1] == '"')){
  202. _passphrase = _passphrase.substr(1, _passphrase.length()-2);
  203. }
  204. }
  205. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "IP")){
  206. _ip = zerlegt[1];
  207. if ((_ip[0] == '"') && (_ip[_ip.length()-1] == '"')){
  208. _ip = _ip.substr(1, _ip.length()-2);
  209. }
  210. }
  211. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "GATEWAY")){
  212. _gw = zerlegt[1];
  213. if ((_gw[0] == '"') && (_gw[_gw.length()-1] == '"')){
  214. _gw = _gw.substr(1, _gw.length()-2);
  215. }
  216. }
  217. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "NETMASK")){
  218. _netmask = zerlegt[1];
  219. if ((_netmask[0] == '"') && (_netmask[_netmask.length()-1] == '"')){
  220. _netmask = _netmask.substr(1, _netmask.length()-2);
  221. }
  222. }
  223. if (fgets(zw, 1024, pFile) == NULL)
  224. {
  225. line = "";
  226. }
  227. else
  228. {
  229. line = std::string(zw);
  230. }
  231. }
  232. fclose(pFile);
  233. // Check if Hostname was empty in .ini if yes set to std_hostname
  234. if(_hostname.length() <= 0){
  235. _hostname = std_hostname;
  236. }
  237. }
  238. std::string getHostname(){
  239. return hostname;
  240. }
  241. std::string getIPAddress(){
  242. return ipaddress;
  243. }
  244. std::string getSSID(){
  245. return ssid;
  246. }
  247. std::string getNetMask(){
  248. return netmask;
  249. }
  250. std::string getGW(){
  251. return gw;
  252. }