|
@@ -11,6 +11,10 @@
|
|
|
#include <fstream>
|
|
#include <fstream>
|
|
|
#include <string>
|
|
#include <string>
|
|
|
#include <vector>
|
|
#include <vector>
|
|
|
|
|
+#include <sstream>
|
|
|
|
|
+#include <iostream>
|
|
|
|
|
+
|
|
|
|
|
+#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include "Helper.h"
|
|
#include "Helper.h"
|
|
|
|
|
|
|
@@ -23,6 +27,9 @@ std::string ssid;
|
|
|
std::string passphrase;
|
|
std::string passphrase;
|
|
|
std::string hostname;
|
|
std::string hostname;
|
|
|
std::string ipaddress;
|
|
std::string ipaddress;
|
|
|
|
|
+std::string gw;
|
|
|
|
|
+std::string netmask;
|
|
|
|
|
+std::string dns;
|
|
|
|
|
|
|
|
std::string std_hostname = "watermeter";
|
|
std::string std_hostname = "watermeter";
|
|
|
|
|
|
|
@@ -86,7 +93,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
|
|
|
{
|
|
{
|
|
|
switch(event->event_id) {
|
|
switch(event->event_id) {
|
|
|
case SYSTEM_EVENT_STA_START:
|
|
case SYSTEM_EVENT_STA_START:
|
|
|
- blinkstatus(200, 5);
|
|
|
|
|
|
|
+ blinkstatus(200, 1);
|
|
|
wifi_connect();
|
|
wifi_connect();
|
|
|
break;
|
|
break;
|
|
|
case SYSTEM_EVENT_STA_GOT_IP:
|
|
case SYSTEM_EVENT_STA_GOT_IP:
|
|
@@ -125,10 +132,97 @@ void initialise_wifi(std::string _ssid, std::string _passphrase, std::string _ho
|
|
|
tcpip_adapter_ip_info_t ip_info;
|
|
tcpip_adapter_ip_info_t ip_info;
|
|
|
ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
|
|
ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
|
|
|
ipaddress = std::string(ip4addr_ntoa(&ip_info.ip));
|
|
ipaddress = std::string(ip4addr_ntoa(&ip_info.ip));
|
|
|
|
|
+ netmask = std::string(ip4addr_ntoa(&ip_info.netmask));
|
|
|
|
|
+ gw = std::string(ip4addr_ntoa(&ip_info.gw));
|
|
|
printf("IPv4 : %s\n", ip4addr_ntoa(&ip_info.ip));
|
|
printf("IPv4 : %s\n", ip4addr_ntoa(&ip_info.ip));
|
|
|
printf("HostName : %s\n", hostname.c_str());
|
|
printf("HostName : %s\n", hostname.c_str());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+///////////////////////////////////////////////////////////
|
|
|
|
|
+///////////////////////////////////////////////////////////
|
|
|
|
|
+
|
|
|
|
|
+void strinttoip4(std::string ip, int &a, int &b, int &c, int &d) {
|
|
|
|
|
+ std::stringstream s(ip);
|
|
|
|
|
+ char ch; //to temporarily store the '.'
|
|
|
|
|
+ s >> a >> ch >> b >> ch >> c >> ch >> d;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+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)
|
|
|
|
|
+{
|
|
|
|
|
+
|
|
|
|
|
+ ssid = _ssid;
|
|
|
|
|
+ passphrase = _passphrase;
|
|
|
|
|
+ hostname = _hostname;
|
|
|
|
|
+ dns = _dns;
|
|
|
|
|
+
|
|
|
|
|
+ wifi_event_group = xEventGroupCreate();
|
|
|
|
|
+
|
|
|
|
|
+ ESP_ERROR_CHECK(esp_netif_init());
|
|
|
|
|
+ ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
+
|
|
|
|
|
+ esp_netif_t *my_sta = esp_netif_create_default_wifi_sta();
|
|
|
|
|
+
|
|
|
|
|
+ esp_netif_dhcpc_stop(my_sta);
|
|
|
|
|
+
|
|
|
|
|
+ esp_netif_ip_info_t ip_info;
|
|
|
|
|
+
|
|
|
|
|
+ int a, b, c, d;
|
|
|
|
|
+
|
|
|
|
|
+ strinttoip4(_ip, a, b, c, d);
|
|
|
|
|
+ IP4_ADDR(&ip_info.ip, a, b, c, d);
|
|
|
|
|
+
|
|
|
|
|
+ strinttoip4(_gw, a, b, c, d);
|
|
|
|
|
+ IP4_ADDR(&ip_info.gw, a, b, c, d);
|
|
|
|
|
+
|
|
|
|
|
+ strinttoip4(_netmask, a, b, c, d);
|
|
|
|
|
+ IP4_ADDR(&ip_info.netmask, a, b, c, d);
|
|
|
|
|
+
|
|
|
|
|
+ esp_netif_set_ip_info(my_sta, &ip_info);
|
|
|
|
|
+
|
|
|
|
|
+ wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
|
+ ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
|
|
+
|
|
|
|
|
+ if (dns.length() > 0) {
|
|
|
|
|
+ esp_netif_dns_info_t dns_info;
|
|
|
|
|
+ ip4_addr_t ip;
|
|
|
|
|
+ ip.addr = esp_ip4addr_aton(dns.c_str());
|
|
|
|
|
+ ip_addr_set_ip4_u32(&dns_info.ip, ip.addr);
|
|
|
|
|
+ ESP_ERROR_CHECK(esp_netif_set_dns_info(my_sta, ESP_NETIF_DNS_MAIN, &dns_info));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
|
|
|
|
|
+
|
|
|
|
|
+ wifi_config_t wifi_config = { };
|
|
|
|
|
+ strcpy((char*)wifi_config.sta.ssid, (const char*)ssid.c_str());
|
|
|
|
|
+ strcpy((char*)wifi_config.sta.password, (const char*)passphrase.c_str());
|
|
|
|
|
+
|
|
|
|
|
+ ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
|
|
|
|
|
+ ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
|
|
|
|
|
+ ESP_ERROR_CHECK(esp_wifi_start() );
|
|
|
|
|
+
|
|
|
|
|
+ ESP_LOGI(MAIN_TAG, "wifi_init_sta finished.");
|
|
|
|
|
+
|
|
|
|
|
+ EventBits_t bits = xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,true,true,portMAX_DELAY);
|
|
|
|
|
+
|
|
|
|
|
+ if (bits & CONNECTED_BIT) {
|
|
|
|
|
+ ESP_LOGI(MAIN_TAG, "connected to ap SSID:%s password:%s",
|
|
|
|
|
+ ssid.c_str(), passphrase.c_str());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ESP_LOGI(MAIN_TAG, "Failed to connect to SSID:%s, password:%s",
|
|
|
|
|
+ ssid.c_str(), passphrase.c_str());
|
|
|
|
|
+ }
|
|
|
|
|
+ tcpip_adapter_ip_info_t ip_info2;
|
|
|
|
|
+ ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info2));
|
|
|
|
|
+ ipaddress = std::string(ip4addr_ntoa(&ip_info2.ip));
|
|
|
|
|
+ netmask = std::string(ip4addr_ntoa(&ip_info2.netmask));
|
|
|
|
|
+ gw = std::string(ip4addr_ntoa(&ip_info2.gw));
|
|
|
|
|
+
|
|
|
|
|
+ vEventGroupDelete(wifi_event_group);
|
|
|
|
|
+}
|
|
|
|
|
+///////////////////////////////////////////////////////////
|
|
|
|
|
+///////////////////////////////////////////////////////////
|
|
|
|
|
+
|
|
|
|
|
+//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)
|
|
|
|
|
|
|
|
void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphrase, std::string &_hostname)
|
|
void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphrase, std::string &_hostname)
|
|
|
{
|
|
{
|
|
@@ -140,6 +234,8 @@ void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphra
|
|
|
fn = FormatFileName(fn);
|
|
fn = FormatFileName(fn);
|
|
|
pFile = fopen(fn.c_str(), "r");
|
|
pFile = fopen(fn.c_str(), "r");
|
|
|
|
|
|
|
|
|
|
+ printf("file loaded\n");
|
|
|
|
|
+
|
|
|
if (pFile == NULL)
|
|
if (pFile == NULL)
|
|
|
return;
|
|
return;
|
|
|
|
|
|
|
@@ -149,7 +245,7 @@ void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphra
|
|
|
|
|
|
|
|
while ((line.size() > 0) || !(feof(pFile)))
|
|
while ((line.size() > 0) || !(feof(pFile)))
|
|
|
{
|
|
{
|
|
|
-// printf("%s", line.c_str());
|
|
|
|
|
|
|
+ printf("%s", line.c_str());
|
|
|
zerlegt = ZerlegeZeile(line, "=");
|
|
zerlegt = ZerlegeZeile(line, "=");
|
|
|
zerlegt[0] = trim(zerlegt[0], " ");
|
|
zerlegt[0] = trim(zerlegt[0], " ");
|
|
|
|
|
|
|
@@ -174,6 +270,37 @@ void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphra
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/*
|
|
|
|
|
+ if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "IP")){
|
|
|
|
|
+ _ip = zerlegt[1];
|
|
|
|
|
+ if ((_ip[0] == '"') && (_ip[_ip.length()-1] == '"')){
|
|
|
|
|
+ _ip = _ip.substr(1, _ip.length()-2);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "GATEWAY")){
|
|
|
|
|
+ _gw = zerlegt[1];
|
|
|
|
|
+ if ((_gw[0] == '"') && (_gw[_gw.length()-1] == '"')){
|
|
|
|
|
+ _gw = _gw.substr(1, _gw.length()-2);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "NETMASK")){
|
|
|
|
|
+ _netmask = zerlegt[1];
|
|
|
|
|
+ if ((_netmask[0] == '"') && (_netmask[_netmask.length()-1] == '"')){
|
|
|
|
|
+ _netmask = _netmask.substr(1, _netmask.length()-2);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "DNS")){
|
|
|
|
|
+ _dns = zerlegt[1];
|
|
|
|
|
+ if ((_dns[0] == '"') && (_dns[_dns.length()-1] == '"')){
|
|
|
|
|
+ _dns = _dns.substr(1, _dns.length()-2);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+*/
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
if (fgets(zw, 1024, pFile) == NULL)
|
|
if (fgets(zw, 1024, pFile) == NULL)
|
|
|
{
|
|
{
|
|
|
line = "";
|
|
line = "";
|
|
@@ -204,3 +331,12 @@ std::string getIPAddress(){
|
|
|
std::string getSSID(){
|
|
std::string getSSID(){
|
|
|
return ssid;
|
|
return ssid;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+std::string getNetMask(){
|
|
|
|
|
+ return netmask;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+std::string getGW(){
|
|
|
|
|
+ return gw;
|
|
|
|
|
+}
|
|
|
|
|
+
|