connect_wlan.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 = "hostname = \"" + _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. fputs(neuesfile[i].c_str(), pFile);
  224. }
  225. fclose(pFile);
  226. return true;
  227. }
  228. void LoadWlanFromFile(std::string fn)
  229. {
  230. string line = "";
  231. std::vector<string> zerlegt;
  232. hostname = std_hostname;
  233. FILE* pFile;
  234. fn = FormatFileName(fn);
  235. pFile = OpenFileAndWait(fn.c_str(), "r");
  236. printf("file loaded\n");
  237. if (pFile == NULL)
  238. return;
  239. char zw[1024];
  240. fgets(zw, 1024, pFile);
  241. line = std::string(zw);
  242. while ((line.size() > 0) || !(feof(pFile)))
  243. {
  244. printf("%s", line.c_str());
  245. zerlegt = ZerlegeZeile(line, "=");
  246. zerlegt[0] = trim(zerlegt[0], " ");
  247. for (int i = 2; i < zerlegt.size(); ++i)
  248. zerlegt[i] = zerlegt[i-1] + zerlegt[i];
  249. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "HOSTNAME")){
  250. hostname = trim(zerlegt[1]);
  251. if ((hostname[0] == '"') && (hostname[hostname.length()-1] == '"')){
  252. hostname = hostname.substr(1, hostname.length()-2);
  253. }
  254. }
  255. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "SSID")){
  256. ssid = trim(zerlegt[1]);
  257. if ((ssid[0] == '"') && (ssid[ssid.length()-1] == '"')){
  258. ssid = ssid.substr(1, ssid.length()-2);
  259. }
  260. }
  261. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "PASSWORD")){
  262. passphrase = zerlegt[1];
  263. if ((passphrase[0] == '"') && (passphrase[passphrase.length()-1] == '"')){
  264. passphrase = passphrase.substr(1, passphrase.length()-2);
  265. }
  266. }
  267. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "IP")){
  268. ipaddress = zerlegt[1];
  269. if ((ipaddress[0] == '"') && (ipaddress[ipaddress.length()-1] == '"')){
  270. ipaddress = ipaddress.substr(1, ipaddress.length()-2);
  271. }
  272. }
  273. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "GATEWAY")){
  274. gw = zerlegt[1];
  275. if ((gw[0] == '"') && (gw[gw.length()-1] == '"')){
  276. gw = gw.substr(1, gw.length()-2);
  277. }
  278. }
  279. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "NETMASK")){
  280. netmask = zerlegt[1];
  281. if ((netmask[0] == '"') && (netmask[netmask.length()-1] == '"')){
  282. netmask = netmask.substr(1, netmask.length()-2);
  283. }
  284. }
  285. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "DNS")){
  286. dns = zerlegt[1];
  287. if ((dns[0] == '"') && (dns[dns.length()-1] == '"')){
  288. dns = dns.substr(1, dns.length()-2);
  289. }
  290. }
  291. if (fgets(zw, 1024, pFile) == NULL)
  292. {
  293. line = "";
  294. }
  295. else
  296. {
  297. line = std::string(zw);
  298. }
  299. }
  300. fclose(pFile);
  301. // Check if Hostname was empty in .ini if yes set to std_hostname
  302. if(hostname.length() <= 0){
  303. hostname = std_hostname;
  304. }
  305. printf("\nWLan: %s, %s\n", ssid.c_str(), passphrase.c_str());
  306. printf("Hostename: %s\n", hostname.c_str());
  307. printf("Fixed IP: %s, Gateway %s, Netmask %s, DNS %s\n", ipaddress.c_str(), gw.c_str(), netmask.c_str(), dns.c_str());
  308. }
  309. void LoadNetConfigFromFile(std::string fn, std::string &_ip, std::string &_gw, std::string &_netmask, std::string &_dns)
  310. {
  311. string line = "";
  312. std::vector<string> zerlegt;
  313. FILE* pFile;
  314. fn = FormatFileName(fn);
  315. pFile = OpenFileAndWait(fn.c_str(), "r");
  316. printf("file loaded\n");
  317. if (pFile == NULL)
  318. return;
  319. char zw[1024];
  320. fgets(zw, 1024, pFile);
  321. line = std::string(zw);
  322. while ((line.size() > 0) || !(feof(pFile)))
  323. {
  324. printf("%s", line.c_str());
  325. zerlegt = ZerlegeZeile(line, "=");
  326. zerlegt[0] = trim(zerlegt[0], " ");
  327. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "IP")){
  328. _ip = zerlegt[1];
  329. if ((_ip[0] == '"') && (_ip[_ip.length()-1] == '"')){
  330. _ip = _ip.substr(1, _ip.length()-2);
  331. }
  332. }
  333. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "GATEWAY")){
  334. _gw = zerlegt[1];
  335. if ((_gw[0] == '"') && (_gw[_gw.length()-1] == '"')){
  336. _gw = _gw.substr(1, _gw.length()-2);
  337. }
  338. }
  339. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "NETMASK")){
  340. _netmask = zerlegt[1];
  341. if ((_netmask[0] == '"') && (_netmask[_netmask.length()-1] == '"')){
  342. _netmask = _netmask.substr(1, _netmask.length()-2);
  343. }
  344. }
  345. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "DNS")){
  346. _dns = zerlegt[1];
  347. if ((_dns[0] == '"') && (_dns[_dns.length()-1] == '"')){
  348. _dns = _dns.substr(1, _dns.length()-2);
  349. }
  350. }
  351. if (fgets(zw, 1024, pFile) == NULL)
  352. {
  353. line = "";
  354. }
  355. else
  356. {
  357. line = std::string(zw);
  358. }
  359. }
  360. fclose(pFile);
  361. }
  362. std::string getHostname(){
  363. return hostname;
  364. }
  365. std::string getIPAddress(){
  366. return ipaddress;
  367. }
  368. std::string getSSID(){
  369. return ssid;
  370. }
  371. std::string getNetMask(){
  372. return netmask;
  373. }
  374. std::string getGW(){
  375. return gw;
  376. }