This commit is contained in:
jomjol
2021-02-03 21:26:00 +01:00
parent 87202115d0
commit 9dbad050fd
14 changed files with 660 additions and 207 deletions

View File

@@ -23,14 +23,13 @@
static const char *MAIN_TAG = "connect_wlan";
std::string ssid;
std::string passphrase;
std::string hostname;
std::string ipaddress;
std::string gw;
std::string netmask;
std::string dns;
std::string ssid = "";
std::string passphrase = "";
std::string hostname = "";
std::string ipaddress = "";
std::string gw = "";
std::string netmask = "";
std::string dns = "";
std::string std_hostname = "watermeter";
static EventGroupHandle_t wifi_event_group;
@@ -76,6 +75,8 @@ void wifi_connect(){
ESP_ERROR_CHECK( esp_wifi_connect() );
}
void blinkstatus(int dauer, int _anzahl)
{
gpio_reset_pin(BLINK_GPIO);
@@ -111,13 +112,11 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
return ESP_OK;
}
void initialise_wifi(std::string _ssid, std::string _passphrase, std::string _hostname)
void initialise_wifi()
{
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
wifi_event_group = xEventGroupCreate();
ssid = _ssid;
passphrase = _passphrase;
hostname = _hostname;
esp_log_level_set("wifi", ESP_LOG_NONE); // disable wifi driver logging
tcpip_adapter_init();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
@@ -147,14 +146,9 @@ void strinttoip4(std::string ip, int &a, int &b, int &c, int &d) {
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)
void initialise_wifi_fixed_ip()
{
ssid = _ssid;
passphrase = _passphrase;
hostname = _hostname;
dns = _dns;
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
@@ -168,13 +162,13 @@ void initialise_wifi_fixed_ip(std::string _ip, std::string _gw, std::string _net
int a, b, c, d;
strinttoip4(_ip, a, b, c, d);
strinttoip4(ipaddress, a, b, c, d);
IP4_ADDR(&ip_info.ip, a, b, c, d);
strinttoip4(_gw, 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);
strinttoip4(netmask, a, b, c, d);
IP4_ADDR(&ip_info.netmask, a, b, c, d);
esp_netif_set_ip_info(my_sta, &ip_info);
@@ -221,6 +215,23 @@ void initialise_wifi_fixed_ip(std::string _ip, std::string _gw, std::string _net
}
void ConnectToWLAN()
{
if (ipaddress.length() == 0 || gw.length() == 0 || netmask.length() == 0)
{
printf("Connect to WLAN with dyn. IP\n");
initialise_wifi();
}
else
{
printf("Connect to WLAN with fixed IP\n");
initialise_wifi_fixed_ip();
}
}
bool ChangeHostName(std::string fn, std::string _newhostname)
{
if (_newhostname == hostname)
@@ -290,16 +301,16 @@ bool ChangeHostName(std::string fn, std::string _newhostname)
}
void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphrase, std::string &_hostname)
void LoadWlanFromFile(std::string fn)
{
string line = "";
std::vector<string> zerlegt;
_hostname = std_hostname;
hostname = std_hostname;
FILE* pFile;
fn = FormatFileName(fn);
pFile = OpenFileAndWait(fn.c_str(), "r");
pFile = OpenFileAndWait(fn.c_str(), "r");
printf("file loaded\n");
if (pFile == NULL)
@@ -318,26 +329,55 @@ void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphra
zerlegt[i] = zerlegt[i-1] + zerlegt[i];
if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "HOSTNAME")){
_hostname = trim(zerlegt[1]);
if ((_hostname[0] == '"') && (_hostname[_hostname.length()-1] == '"')){
_hostname = _hostname.substr(1, _hostname.length()-2);
hostname = trim(zerlegt[1]);
if ((hostname[0] == '"') && (hostname[hostname.length()-1] == '"')){
hostname = hostname.substr(1, hostname.length()-2);
}
}
if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "SSID")){
_ssid = trim(zerlegt[1]);
if ((_ssid[0] == '"') && (_ssid[_ssid.length()-1] == '"')){
_ssid = _ssid.substr(1, _ssid.length()-2);
ssid = trim(zerlegt[1]);
if ((ssid[0] == '"') && (ssid[ssid.length()-1] == '"')){
ssid = ssid.substr(1, ssid.length()-2);
}
}
if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "PASSWORD")){
_passphrase = zerlegt[1];
if ((_passphrase[0] == '"') && (_passphrase[_passphrase.length()-1] == '"')){
_passphrase = _passphrase.substr(1, _passphrase.length()-2);
passphrase = zerlegt[1];
if ((passphrase[0] == '"') && (passphrase[passphrase.length()-1] == '"')){
passphrase = passphrase.substr(1, passphrase.length()-2);
}
}
if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "IP")){
ipaddress = zerlegt[1];
if ((ipaddress[0] == '"') && (ipaddress[ipaddress.length()-1] == '"')){
ipaddress = ipaddress.substr(1, ipaddress.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)
{
line = "";
@@ -351,9 +391,14 @@ void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphra
fclose(pFile);
// Check if Hostname was empty in .ini if yes set to std_hostname
if(_hostname.length() <= 0){
_hostname = std_hostname;
if(hostname.length() <= 0){
hostname = std_hostname;
}
printf("\nWLan: %s, %s\n", ssid.c_str(), passphrase.c_str());
printf("Hostename: %s\n", hostname.c_str());
printf("Fixed IP: %s, Gateway %s, Netmask %s, DNS %s\n", ipaddress.c_str(), gw.c_str(), netmask.c_str(), dns.c_str());
}
void LoadNetConfigFromFile(std::string fn, std::string &_ip, std::string &_gw, std::string &_netmask, std::string &_dns)