From fc3fa2fc45f8bf4560d4dc75f42e3316ae1fabce Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Wed, 19 Dec 2018 22:36:19 +0100 Subject: [PATCH] Can store BSSID and CHANNEL This allow faster network connection. Could be usefull for battery powered application --- WifiControlSensor/EEPROM.h | 3 +++ WifiControlSensor/EEPROM.ino | 8 ++++-- WifiControlSensor/WebServer.ino | 33 ++++++++++++++++++++----- WifiControlSensor/WifiControlSensor.ino | 23 +++++++++++++++-- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/WifiControlSensor/EEPROM.h b/WifiControlSensor/EEPROM.h index 663173d..ac7f36d 100644 --- a/WifiControlSensor/EEPROM.h +++ b/WifiControlSensor/EEPROM.h @@ -15,6 +15,9 @@ typedef struct productConfig_t { uint32_t mask; uint32_t dns; uint32_t dns2; + int32_t channel; + char *bssid; + } productConfig; int EepromSaveConfig(productConfig &config); diff --git a/WifiControlSensor/EEPROM.ino b/WifiControlSensor/EEPROM.ino index 0ae00a4..92cf7a6 100644 --- a/WifiControlSensor/EEPROM.ino +++ b/WifiControlSensor/EEPROM.ino @@ -1,5 +1,5 @@ /* EEPROM LAYOUT - "BOOTMODE;SSID;PASSWORD;HOSTNAME;MQTT_SERVER;MQTT_USERNAME;MQTT_PASSWD;MQTT_PORT;IP_CONFIG;IP;GATEWAY;NETMASK;DNS1;DNS2;" + "BOOTMODE;SSID;PASSWORD;HOSTNAME;MQTT_SERVER;MQTT_USERNAME;MQTT_PASSWD;MQTT_PORT;IP_CONFIG;IP;GATEWAY;NETMASK;DNS1;DNS2;WIFI_CHANNEL;WIFI_BSSID;" BOOTMODE could be 0 for Setup, 1 for normal use, 2 for OTA IP_CONFIG could be 0 for DHCP, 1 for static Setup mode is trigger by setting GPIO3 to ground or at first boot @@ -17,7 +17,8 @@ int EepromSaveConfig(productConfig &config) { + String(config.mqttPort) + ";" + String(config.ip_mode) + ";" + config.ip + ";" + config.gw + ";" + config.mask + ";" - + config.dns + ";" + config.dns2 + ";"; + + config.dns + ";" + config.dns2 + ";" + config.channel + ";" + + config.bssid + ";"; if (eeprom.length() > CONFIG_EEPROM_SIZE ) return -EMSGSIZE; @@ -95,5 +96,8 @@ void EepromReadConfig(productConfig &config) { config.dns = atoi(tmpString); readConfElement(&tmpString, i); config.dns2 = atoi(tmpString); + readConfElement(&tmpString, i); + config.channel = atoi(tmpString); + readConfElement(&config.bssid, i); } diff --git a/WifiControlSensor/WebServer.ino b/WifiControlSensor/WebServer.ino index 50eb32d..f998fc8 100644 --- a/WifiControlSensor/WebServer.ino +++ b/WifiControlSensor/WebServer.ino @@ -19,8 +19,12 @@ void WebBuildGpioObserved(String &html){ } void WebHandleRoot() { String gpioObserved = ""; + String optimiseConfig = ""; WebBuildGpioObserved(gpioObserved); + if(WiFi.status() == WL_CONNECTED){ + optimiseConfig = "Optimize Config
"; + } server.send(200, "text/html", "" @@ -41,7 +45,7 @@ void WebHandleRoot() { "" + gpioControlHTML + gpioObserved + pwmControlHTML + "
" "Settings" "Enter Setup
" - "Update firmware
" + "Update firmware
" + optimiseConfig + "MQTT Status: " + (MqttIsConnected() ? "Connected" : "Disconnected") + "
" #ifdef CONFIG_ENABLE_BMP180 "BMP 180 (Temp+Pression) Status: " + (BMP180IsConnected() ? "Connected" : "Disconnected") + "
" @@ -79,6 +83,18 @@ void WebBuildSSIDList(String &datalist){ datalist += ""; } +void WebHandleSetupPreConfig() { + conf.bssid =strdup( WiFi.BSSIDstr().c_str()); + conf.channel = WiFi.channel(); + conf.ip_mode = 1; + conf.ip = WiFi.localIP(); + conf.mask = WiFi.subnetMask(); + conf.gw = WiFi.gatewayIP(); + conf.dns = WiFi.dnsIP(); + conf.dns2 = WiFi.dnsIP(1); + WebHandleSetup(); +} + void WebHandleSetup() { String ssidlist; WebBuildSSIDList(ssidlist); @@ -87,10 +103,12 @@ void WebHandleSetup() { server.send(200, "text/html", "
" "
" "Wifi configuration:" - "

" + "

" "" + ssidlist + "" - "

" - "

" + "

" + "

" + "

" + "

" "
" "
" "IP Configuration" @@ -151,7 +169,7 @@ void WebHandleSave() { || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") || !server.hasArg("mqttPort") || !server.hasArg("ip_config") || !server.hasArg("ip") || !server.hasArg("gw") || !server.hasArg("mask") || !server.hasArg("dns") - || !server.hasArg("dns2")) { + || !server.hasArg("dns2") || !server.hasArg("channel") || ! server.hasArg("bssid")) { server.send(500, "text/plain", "Bad arguments\r\n"); return; } @@ -161,6 +179,7 @@ void WebHandleSave() { if (server.arg("ip_config").toInt() == 1) { if (!WebSetIp(ip, "ip", "Incorrect IP") || !WebSetIp(gw, "gw", "Incorrect Gateway") || !WebSetIp(mask, "mask", "Incorrect NetMask") || !WebSetIp(dns, "dns", "Incorrect DNS") || !WebSetIp(dns2, "dns2", "Incorrect DNS2")) { + server.send(500, "text/plain", "Bad arguments\r\n"); return; } } @@ -169,7 +188,7 @@ void WebHandleSave() { strdup(server.arg("host").c_str()), strdup(server.arg("mqttServer").c_str()), strdup(server.arg("mqttUser").c_str()), strdup(server.arg("mqttPasswd").c_str()), server.arg("mqttPort").toInt(), server.arg("ip_config").toInt(), static_cast(ip), static_cast(gw), - static_cast(mask), static_cast(dns), static_cast(dns2)}; + static_cast(mask), static_cast(dns), static_cast(dns2), server.arg("channel").toInt(), strdup(server.arg("bssid").c_str())}; if (EepromSaveConfig(newConf) < 0) { WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); return; @@ -286,6 +305,7 @@ void WebSetupServer(int ) { WebBuildPwmControl(); server.on("/", WebHandleRoot); + server.on("/setupPreConfig", WebHandleSetupPreConfig); server.on("/setup", WebHandleSetup); server.on("/save", WebHandleSave); server.on("/gpio", WebHandleGpio); @@ -302,6 +322,7 @@ void WebSetupServer(int ) { #else void WebHandleRoot(){} +void WebHandleSetupPreConfig(){} void WebHandleSetup(){} void WebHandleGpio(){} void WebHandleSave(){} diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index 155fc3a..a298376 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -64,7 +64,7 @@ float dhtTemp, dhtHumidity; int dryness; uint8_t mode; int reconfig = 0; -productConfig conf = {BOOTMODE_SETUP, "", "", "", "", "", "", 1883, 0, 0, 0, 0, 0, 0}; +productConfig conf = {BOOTMODE_SETUP, "", "", "", "", "", "", 1883, 0, 0, 0, 0, 0, 0, 0, ""}; // Should have less that MAXSUBSCRIPTIONS elements // MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h const int gpioControlled[] = CONFIG_CONTROLLED_GPIO; @@ -100,6 +100,7 @@ void onLongButtonPressed(uint8_t pin){ void WifiSetup(productConfig conf) { IPAddress myIP; + int connectionTry = 0; if (mode == BOOTMODE_SETUP) { SKETCH_DEBUG_PRINTLN("Configuring access point: " CONFIG_SSID_NAME); /* You can set a password to the AP here */ @@ -113,7 +114,20 @@ void WifiSetup(productConfig conf) { SKETCH_DEBUG_PRINTLN("Use static ip configuration"); WiFi.config(IPAddress(conf.ip), IPAddress(conf.gw), IPAddress(conf.mask), IPAddress(conf.dns), IPAddress(conf.dns2)); } - WiFi.begin(conf.ssid, conf.password); + uint8_t *bssidConf = NULL; + uint8_t bssid[6]; + if (conf.bssid[0] != '\0') { + String bssidStr = conf.bssid; + bssid[0] = strtoul(bssidStr.substring(0,2).c_str(), NULL, 16); + bssid[1] = strtoul(bssidStr.substring(3,5).c_str(), NULL, 16); + bssid[2] = strtoul(bssidStr.substring(6,8).c_str(), NULL, 16); + bssid[3] = strtoul(bssidStr.substring(9,11).c_str(), NULL, 16); + bssid[4] = strtoul(bssidStr.substring(12,14).c_str(), NULL, 16); + bssid[5] = strtoul(bssidStr.substring(15,17).c_str(), NULL, 16); + SKETCH_DEBUG_PRINTF("Using BSSID: %x:%x:%x:%x:%x:%x\n", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]); + bssidConf = bssid; + } + WiFi.begin(conf.ssid, conf.password, conf.channel, bssidConf); while (WiFi.status() != WL_CONNECTED) { delay(500); SKETCH_DEBUG_PRINT("."); @@ -121,6 +135,11 @@ void WifiSetup(productConfig conf) { reconfig = 0; return; } + if(connectionTry == 10){ + SKETCH_DEBUG_PRINTLN("Cannot connect to wifi. Try withour BSSID and channel"); + WiFi.begin(conf.ssid, conf.password); + } + connectionTry++; } SKETCH_DEBUG_PRINTF("\nWiFi connected\n");