Browse Source

Merge branch 'bssid_channel' into 'master'

Can store BSSID and CHANNEL

See merge request Mathieu/Domotique!1
bme680
Mathieu Maret 3 years ago
parent
commit
eab447abc6
  1. 3
      WifiControlSensor/EEPROM.h
  2. 8
      WifiControlSensor/EEPROM.ino
  3. 33
      WifiControlSensor/WebServer.ino
  4. 23
      WifiControlSensor/WifiControlSensor.ino

3
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);

8
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);
}

33
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 = "<a href=\"/setupPreConfig\">Optimize Config</a><br/>";
}
server.send(200, "text/html",
"<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
@ -41,7 +45,7 @@ void WebHandleRoot() {
"</fieldset>" + gpioControlHTML + gpioObserved + pwmControlHTML + "<fieldset>"
"<legend>Settings</legend>"
"<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/>"
#ifdef CONFIG_ENABLE_BMP180
"BMP 180 (Temp+Pression) Status: " + (BMP180IsConnected() ? "Connected" : "Disconnected") + "<br/>"
@ -79,6 +83,18 @@ void WebBuildSSIDList(String &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() {
String ssidlist;
WebBuildSSIDList(ssidlist);
@ -87,10 +103,12 @@ void WebHandleSetup() {
server.send(200, "text/html", "<form action=\"/save\" method=\"get\">"
"<fieldset>"
"<legend>Wifi configuration:</legend>"
"<div><label for=\"ssid\">Wifi SSID :</label> <br/><input list=\"scan_ssid\" type=\"text\" name=\"ssid\" value=\"" + String(conf.ssid) + "\" /></div>"
"<div><label for=\"ssid\">Wifi SSID: </label> <br/><input list=\"scan_ssid\" type=\"text\" name=\"ssid\" value=\"" + String(conf.ssid) + "\" /></div>"
"" + ssidlist + ""
"<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=\"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=\"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>"
"<legend>IP Configuration</legend>"
@ -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<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) {
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(){}

23
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");

Loading…
Cancel
Save