Can store BSSID and CHANNEL

This allow faster network connection.
Could be usefull for battery powered application
This commit is contained in:
Mathieu Maret 2018-12-19 22:36:19 +01:00
parent 4121965de7
commit fc3fa2fc45
4 changed files with 57 additions and 10 deletions

View File

@ -15,6 +15,9 @@ typedef struct productConfig_t {
uint32_t mask; uint32_t mask;
uint32_t dns; uint32_t dns;
uint32_t dns2; uint32_t dns2;
int32_t channel;
char *bssid;
} productConfig; } productConfig;
int EepromSaveConfig(productConfig &config); int EepromSaveConfig(productConfig &config);

View File

@ -1,5 +1,5 @@
/* EEPROM LAYOUT /* 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 BOOTMODE could be 0 for Setup, 1 for normal use, 2 for OTA
IP_CONFIG could be 0 for DHCP, 1 for static IP_CONFIG could be 0 for DHCP, 1 for static
Setup mode is trigger by setting GPIO3 to ground or at first boot 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.mqttPort) + ";"
+ String(config.ip_mode) + ";" + String(config.ip_mode) + ";"
+ config.ip + ";" + config.gw + ";" + config.mask + ";" + config.ip + ";" + config.gw + ";" + config.mask + ";"
+ config.dns + ";" + config.dns2 + ";"; + config.dns + ";" + config.dns2 + ";" + config.channel + ";"
+ config.bssid + ";";
if (eeprom.length() > CONFIG_EEPROM_SIZE ) if (eeprom.length() > CONFIG_EEPROM_SIZE )
return -EMSGSIZE; return -EMSGSIZE;
@ -95,5 +96,8 @@ void EepromReadConfig(productConfig &config) {
config.dns = atoi(tmpString); config.dns = atoi(tmpString);
readConfElement(&tmpString, i); readConfElement(&tmpString, i);
config.dns2 = atoi(tmpString); config.dns2 = atoi(tmpString);
readConfElement(&tmpString, i);
config.channel = atoi(tmpString);
readConfElement(&config.bssid, i);
} }

View File

@ -19,8 +19,12 @@ void WebBuildGpioObserved(String &html){
} }
void WebHandleRoot() { void WebHandleRoot() {
String gpioObserved = ""; String gpioObserved = "";
String optimiseConfig = "";
WebBuildGpioObserved(gpioObserved); WebBuildGpioObserved(gpioObserved);
if(WiFi.status() == WL_CONNECTED){
optimiseConfig = "<a href=\"/setupPreConfig\">Optimize Config</a><br/>";
}
server.send(200, "text/html", server.send(200, "text/html",
"<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIODE_MS / 1000) + "\" ></head>" "<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
@ -41,7 +45,7 @@ void WebHandleRoot() {
"</fieldset>" + gpioControlHTML + gpioObserved + pwmControlHTML + "<fieldset>" "</fieldset>" + gpioControlHTML + gpioObserved + pwmControlHTML + "<fieldset>"
"<legend>Settings</legend>" "<legend>Settings</legend>"
"<a href=\"/setup\">Enter Setup</a><br/>" "<a href=\"/setup\">Enter Setup</a><br/>"
"<a href=\"/upload\">Update firmware</a><br/>" "<a href=\"/upload\">Update firmware</a><br/>" + optimiseConfig +
"MQTT Status: " + (MqttIsConnected() ? "Connected" : "Disconnected") + "<br/>" "MQTT Status: " + (MqttIsConnected() ? "Connected" : "Disconnected") + "<br/>"
#ifdef CONFIG_ENABLE_BMP180 #ifdef CONFIG_ENABLE_BMP180
"BMP 180 (Temp+Pression) Status: " + (BMP180IsConnected() ? "Connected" : "Disconnected") + "<br/>" "BMP 180 (Temp+Pression) Status: " + (BMP180IsConnected() ? "Connected" : "Disconnected") + "<br/>"
@ -79,6 +83,18 @@ void WebBuildSSIDList(String &datalist){
datalist += "</datalist>"; 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() { void WebHandleSetup() {
String ssidlist; String ssidlist;
WebBuildSSIDList(ssidlist); WebBuildSSIDList(ssidlist);
@ -91,6 +107,8 @@ void WebHandleSetup() {
"" + ssidlist + "" "" + ssidlist + ""
"<div><label for=\"password\">Wifi Password: </label><br/><input type=\"password\" name=\"password\" style=\"border-color:red\" /> </div>" "<div><label for=\"password\">Wifi Password: </label><br/><input type=\"password\" name=\"password\" style=\"border-color:red\" /> </div>"
"<div><label for=\"host\">Hostname: </label><br/><input type=\"text\" name=\"host\" value=\"" + String(conf.host) + "\" /> </div>" "<div><label for=\"host\">Hostname: </label><br/><input type=\"text\" name=\"host\" value=\"" + String(conf.host) + "\" /> </div>"
"<div><label for=\"channel\">Channel (0 for auto): </label><br/><input type=\"text\" name=\"channel\" value=\"" + String(conf.channel) + "\" /> </div>"
"<div><label for=\"bssid\">BSSID (Empty for auto): </label><br/><input type=\"text\" name=\"bssid\" value=\"" + String(conf.bssid) + "\" /> </div>"
"</fieldset>" "</fieldset>"
"<fieldset>" "<fieldset>"
"<legend>IP Configuration</legend>" "<legend>IP Configuration</legend>"
@ -151,7 +169,7 @@ void WebHandleSave() {
|| !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd")
|| !server.hasArg("mqttPort") || !server.hasArg("ip_config") || !server.hasArg("ip") || !server.hasArg("mqttPort") || !server.hasArg("ip_config") || !server.hasArg("ip")
|| !server.hasArg("gw") || !server.hasArg("mask") || !server.hasArg("dns") || !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"); server.send(500, "text/plain", "Bad arguments\r\n");
return; return;
} }
@ -161,6 +179,7 @@ void WebHandleSave() {
if (server.arg("ip_config").toInt() == 1) { if (server.arg("ip_config").toInt() == 1) {
if (!WebSetIp(ip, "ip", "Incorrect IP") || !WebSetIp(gw, "gw", "Incorrect Gateway") || !WebSetIp(mask, "mask", "Incorrect NetMask") || 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")) { !WebSetIp(dns, "dns", "Incorrect DNS") || !WebSetIp(dns2, "dns2", "Incorrect DNS2")) {
server.send(500, "text/plain", "Bad arguments\r\n");
return; 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("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(), strdup(server.arg("mqttPasswd").c_str()), server.arg("mqttPort").toInt(),
server.arg("ip_config").toInt(), static_cast<uint32_t>(ip), static_cast<uint32_t>(gw), server.arg("ip_config").toInt(), static_cast<uint32_t>(ip), static_cast<uint32_t>(gw),
static_cast<uint32_t>(mask), static_cast<uint32_t>(dns), static_cast<uint32_t>(dns2)}; static_cast<uint32_t>(mask), static_cast<uint32_t>(dns), static_cast<uint32_t>(dns2), server.arg("channel").toInt(), strdup(server.arg("bssid").c_str())};
if (EepromSaveConfig(newConf) < 0) { if (EepromSaveConfig(newConf) < 0) {
WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
return; return;
@ -286,6 +305,7 @@ void WebSetupServer(int ) {
WebBuildPwmControl(); WebBuildPwmControl();
server.on("/", WebHandleRoot); server.on("/", WebHandleRoot);
server.on("/setupPreConfig", WebHandleSetupPreConfig);
server.on("/setup", WebHandleSetup); server.on("/setup", WebHandleSetup);
server.on("/save", WebHandleSave); server.on("/save", WebHandleSave);
server.on("/gpio", WebHandleGpio); server.on("/gpio", WebHandleGpio);
@ -302,6 +322,7 @@ void WebSetupServer(int ) {
#else #else
void WebHandleRoot(){} void WebHandleRoot(){}
void WebHandleSetupPreConfig(){}
void WebHandleSetup(){} void WebHandleSetup(){}
void WebHandleGpio(){} void WebHandleGpio(){}
void WebHandleSave(){} void WebHandleSave(){}

View File

@ -64,7 +64,7 @@ float dhtTemp, dhtHumidity;
int dryness; int dryness;
uint8_t mode; uint8_t mode;
int reconfig = 0; 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 // Should have less that MAXSUBSCRIPTIONS elements
// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h // MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h
const int gpioControlled[] = CONFIG_CONTROLLED_GPIO; const int gpioControlled[] = CONFIG_CONTROLLED_GPIO;
@ -100,6 +100,7 @@ void onLongButtonPressed(uint8_t pin){
void WifiSetup(productConfig conf) { void WifiSetup(productConfig conf) {
IPAddress myIP; IPAddress myIP;
int connectionTry = 0;
if (mode == BOOTMODE_SETUP) { if (mode == BOOTMODE_SETUP) {
SKETCH_DEBUG_PRINTLN("Configuring access point: " CONFIG_SSID_NAME); SKETCH_DEBUG_PRINTLN("Configuring access point: " CONFIG_SSID_NAME);
/* You can set a password to the AP here */ /* You can set a password to the AP here */
@ -113,7 +114,20 @@ void WifiSetup(productConfig conf) {
SKETCH_DEBUG_PRINTLN("Use static ip configuration"); 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.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) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(500);
SKETCH_DEBUG_PRINT("."); SKETCH_DEBUG_PRINT(".");
@ -121,6 +135,11 @@ void WifiSetup(productConfig conf) {
reconfig = 0; reconfig = 0;
return; 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"); SKETCH_DEBUG_PRINTF("\nWiFi connected\n");