connect_wlan.cpp 9.6 KB

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