connect_wlan.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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()
  88. {
  89. ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
  90. wifi_event_group = xEventGroupCreate();
  91. esp_log_level_set("wifi", ESP_LOG_NONE); // disable wifi driver logging
  92. tcpip_adapter_init();
  93. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  94. ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  95. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  96. ESP_ERROR_CHECK( esp_wifi_start() );
  97. esp_err_t ret = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA , hostname.c_str());
  98. if(ret != ESP_OK ){
  99. ESP_LOGE(MAIN_TAG,"failed to set hostname:%d",ret);
  100. }
  101. xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,true,true,portMAX_DELAY);
  102. tcpip_adapter_ip_info_t ip_info;
  103. ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
  104. ipaddress = std::string(ip4addr_ntoa(&ip_info.ip));
  105. netmask = std::string(ip4addr_ntoa(&ip_info.netmask));
  106. gw = std::string(ip4addr_ntoa(&ip_info.gw));
  107. printf("IPv4 : %s\n", ip4addr_ntoa(&ip_info.ip));
  108. printf("HostName : %s\n", hostname.c_str());
  109. }
  110. ///////////////////////////////////////////////////////////
  111. ///////////////////////////////////////////////////////////
  112. void strinttoip4(std::string ip, int &a, int &b, int &c, int &d) {
  113. std::stringstream s(ip);
  114. char ch; //to temporarily store the '.'
  115. s >> a >> ch >> b >> ch >> c >> ch >> d;
  116. }
  117. void initialise_wifi_fixed_ip()
  118. {
  119. wifi_event_group = xEventGroupCreate();
  120. ESP_ERROR_CHECK(esp_netif_init());
  121. ESP_ERROR_CHECK(esp_event_loop_create_default());
  122. esp_netif_t *my_sta = esp_netif_create_default_wifi_sta();
  123. esp_netif_dhcpc_stop(my_sta);
  124. esp_netif_ip_info_t ip_info;
  125. int a, b, c, d;
  126. strinttoip4(ipaddress, a, b, c, d);
  127. IP4_ADDR(&ip_info.ip, a, b, c, d);
  128. strinttoip4(gw, a, b, c, d);
  129. IP4_ADDR(&ip_info.gw, a, b, c, d);
  130. strinttoip4(netmask, a, b, c, d);
  131. IP4_ADDR(&ip_info.netmask, a, b, c, d);
  132. esp_netif_set_ip_info(my_sta, &ip_info);
  133. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  134. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  135. if (dns.length() > 0) {
  136. esp_netif_dns_info_t dns_info;
  137. ip4_addr_t ip;
  138. ip.addr = esp_ip4addr_aton(dns.c_str());
  139. ip_addr_set_ip4_u32(&dns_info.ip, ip.addr);
  140. ESP_ERROR_CHECK(esp_netif_set_dns_info(my_sta, ESP_NETIF_DNS_MAIN, &dns_info));
  141. }
  142. ESP_ERROR_CHECK(esp_event_loop_init(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. if (bits & CONNECTED_BIT) {
  152. ESP_LOGI(MAIN_TAG, "connected to ap SSID:%s password:%s",
  153. ssid.c_str(), passphrase.c_str());
  154. } else {
  155. ESP_LOGI(MAIN_TAG, "Failed to connect to SSID:%s, password:%s",
  156. ssid.c_str(), passphrase.c_str());
  157. }
  158. tcpip_adapter_ip_info_t ip_info2;
  159. ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info2));
  160. ipaddress = std::string(ip4addr_ntoa(&ip_info2.ip));
  161. netmask = std::string(ip4addr_ntoa(&ip_info2.netmask));
  162. gw = std::string(ip4addr_ntoa(&ip_info2.gw));
  163. // vEventGroupDelete(wifi_event_group);
  164. }
  165. void ConnectToWLAN()
  166. {
  167. if (ipaddress.length() == 0 || gw.length() == 0 || netmask.length() == 0)
  168. {
  169. printf("Connect to WLAN with dyn. IP\n");
  170. initialise_wifi();
  171. }
  172. else
  173. {
  174. printf("Connect to WLAN with fixed IP\n");
  175. initialise_wifi_fixed_ip();
  176. }
  177. }
  178. bool ChangeHostName(std::string fn, std::string _newhostname)
  179. {
  180. if (_newhostname == hostname)
  181. return false;
  182. string line = "";
  183. std::vector<string> zerlegt;
  184. bool found = false;
  185. std::vector<string> neuesfile;
  186. FILE* pFile;
  187. fn = FormatFileName(fn);
  188. pFile = OpenFileAndWait(fn.c_str(), "r");
  189. printf("file loaded\n");
  190. if (pFile == NULL)
  191. return false;
  192. char zw[1024];
  193. fgets(zw, 1024, pFile);
  194. line = std::string(zw);
  195. while ((line.size() > 0) || !(feof(pFile)))
  196. {
  197. printf("%s", line.c_str());
  198. zerlegt = ZerlegeZeile(line, "=");
  199. zerlegt[0] = trim(zerlegt[0], " ");
  200. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "HOSTNAME")){
  201. line = "hostname = \"" + _newhostname + "\"\n";
  202. found = true;
  203. }
  204. neuesfile.push_back(line);
  205. if (fgets(zw, 1024, pFile) == NULL)
  206. {
  207. line = "";
  208. }
  209. else
  210. {
  211. line = std::string(zw);
  212. }
  213. }
  214. if (!found)
  215. {
  216. line = "\nhostname = \"" + _newhostname + "\"\n";
  217. neuesfile.push_back(line);
  218. }
  219. fclose(pFile);
  220. pFile = OpenFileAndWait(fn.c_str(), "w+");
  221. for (int i = 0; i < neuesfile.size(); ++i)
  222. {
  223. printf(neuesfile[i].c_str());
  224. fputs(neuesfile[i].c_str(), pFile);
  225. }
  226. fclose(pFile);
  227. printf("*** Update hostname done ***\n");
  228. return true;
  229. }
  230. void LoadWlanFromFile(std::string fn)
  231. {
  232. string line = "";
  233. std::vector<string> zerlegt;
  234. hostname = std_hostname;
  235. FILE* pFile;
  236. fn = FormatFileName(fn);
  237. pFile = OpenFileAndWait(fn.c_str(), "r");
  238. printf("file loaded\n");
  239. if (pFile == NULL)
  240. return;
  241. char zw[1024];
  242. fgets(zw, 1024, pFile);
  243. line = std::string(zw);
  244. while ((line.size() > 0) || !(feof(pFile)))
  245. {
  246. printf("%s", line.c_str());
  247. zerlegt = ZerlegeZeile(line, "=");
  248. zerlegt[0] = trim(zerlegt[0], " ");
  249. for (int i = 2; i < zerlegt.size(); ++i)
  250. zerlegt[1] = zerlegt[1] + zerlegt[i];
  251. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "HOSTNAME")){
  252. hostname = trim(zerlegt[1]);
  253. if ((hostname[0] == '"') && (hostname[hostname.length()-1] == '"')){
  254. hostname = hostname.substr(1, hostname.length()-2);
  255. }
  256. }
  257. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "SSID")){
  258. ssid = trim(zerlegt[1]);
  259. if ((ssid[0] == '"') && (ssid[ssid.length()-1] == '"')){
  260. ssid = ssid.substr(1, ssid.length()-2);
  261. }
  262. }
  263. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "PASSWORD")){
  264. passphrase = zerlegt[1];
  265. if ((passphrase[0] == '"') && (passphrase[passphrase.length()-1] == '"')){
  266. passphrase = passphrase.substr(1, passphrase.length()-2);
  267. }
  268. }
  269. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "IP")){
  270. ipaddress = zerlegt[1];
  271. if ((ipaddress[0] == '"') && (ipaddress[ipaddress.length()-1] == '"')){
  272. ipaddress = ipaddress.substr(1, ipaddress.length()-2);
  273. }
  274. }
  275. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "GATEWAY")){
  276. gw = zerlegt[1];
  277. if ((gw[0] == '"') && (gw[gw.length()-1] == '"')){
  278. gw = gw.substr(1, gw.length()-2);
  279. }
  280. }
  281. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "NETMASK")){
  282. netmask = zerlegt[1];
  283. if ((netmask[0] == '"') && (netmask[netmask.length()-1] == '"')){
  284. netmask = netmask.substr(1, netmask.length()-2);
  285. }
  286. }
  287. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "DNS")){
  288. dns = zerlegt[1];
  289. if ((dns[0] == '"') && (dns[dns.length()-1] == '"')){
  290. dns = dns.substr(1, dns.length()-2);
  291. }
  292. }
  293. if (fgets(zw, 1024, pFile) == NULL)
  294. {
  295. line = "";
  296. }
  297. else
  298. {
  299. line = std::string(zw);
  300. }
  301. }
  302. fclose(pFile);
  303. // Check if Hostname was empty in .ini if yes set to std_hostname
  304. if(hostname.length() <= 0){
  305. hostname = std_hostname;
  306. }
  307. printf("\nWLan: %s, %s\n", ssid.c_str(), passphrase.c_str());
  308. printf("Hostename: %s\n", hostname.c_str());
  309. printf("Fixed IP: %s, Gateway %s, Netmask %s, DNS %s\n", ipaddress.c_str(), gw.c_str(), netmask.c_str(), dns.c_str());
  310. }
  311. void LoadNetConfigFromFile(std::string fn, std::string &_ip, std::string &_gw, std::string &_netmask, std::string &_dns)
  312. {
  313. string line = "";
  314. std::vector<string> zerlegt;
  315. FILE* pFile;
  316. fn = FormatFileName(fn);
  317. pFile = OpenFileAndWait(fn.c_str(), "r");
  318. printf("file loaded\n");
  319. if (pFile == NULL)
  320. return;
  321. char zw[1024];
  322. fgets(zw, 1024, pFile);
  323. line = std::string(zw);
  324. while ((line.size() > 0) || !(feof(pFile)))
  325. {
  326. printf("%s", line.c_str());
  327. zerlegt = ZerlegeZeile(line, "=");
  328. zerlegt[0] = trim(zerlegt[0], " ");
  329. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "IP")){
  330. _ip = zerlegt[1];
  331. if ((_ip[0] == '"') && (_ip[_ip.length()-1] == '"')){
  332. _ip = _ip.substr(1, _ip.length()-2);
  333. }
  334. }
  335. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "GATEWAY")){
  336. _gw = zerlegt[1];
  337. if ((_gw[0] == '"') && (_gw[_gw.length()-1] == '"')){
  338. _gw = _gw.substr(1, _gw.length()-2);
  339. }
  340. }
  341. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "NETMASK")){
  342. _netmask = zerlegt[1];
  343. if ((_netmask[0] == '"') && (_netmask[_netmask.length()-1] == '"')){
  344. _netmask = _netmask.substr(1, _netmask.length()-2);
  345. }
  346. }
  347. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "DNS")){
  348. _dns = zerlegt[1];
  349. if ((_dns[0] == '"') && (_dns[_dns.length()-1] == '"')){
  350. _dns = _dns.substr(1, _dns.length()-2);
  351. }
  352. }
  353. if (fgets(zw, 1024, pFile) == NULL)
  354. {
  355. line = "";
  356. }
  357. else
  358. {
  359. line = std::string(zw);
  360. }
  361. }
  362. fclose(pFile);
  363. }
  364. std::string getHostname(){
  365. return hostname;
  366. }
  367. std::string getIPAddress(){
  368. return ipaddress;
  369. }
  370. std::string getSSID(){
  371. return ssid;
  372. }
  373. std::string getNetMask(){
  374. return netmask;
  375. }
  376. std::string getGW(){
  377. return gw;
  378. }