Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secondary SSID Support and Wifi Stability Improvements. #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions OpenGarage/OpenGarage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ OptionStruct OpenGarage::options[] = {
{"atob", OG_AUTO_NONE,255, ""},
{"noto", OG_NOTIFY_DO|OG_NOTIFY_DC,255, ""},
{"usi", 0, 1, ""},
{"ssid", 0, 0, ""}, // string options have 0 max value
{"pass", 0, 0, ""},
{"ssid1", 0, 0, ""}, // string options have 0 max value
{"pass1", 0, 0, ""},
{"auth", 0, 0, ""},
{"bdmn", 0, 0, "blynk-cloud.com"},
{"bprt", 80,65535, ""},
Expand All @@ -74,7 +74,9 @@ OptionStruct OpenGarage::options[] = {
{"dvip", 0, 0, "-.-.-.-"},
{"gwip", 0, 0, "-.-.-.-"},
{"subn", 0, 0, "255.255.255.0"},
{"dns1", 0, 0, "8.8.8.8"}
{"dns1", 0, 0, "8.8.8.8"},
{"ssid2", 0, 0, ""}, // string options have 0 max value
{"pass2", 0, 0, ""}
};

/* Variables and functions for handling Ultrasonic Distance sensor */
Expand Down
6 changes: 4 additions & 2 deletions OpenGarage/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ typedef enum {
OPTION_ATOB, // automation options B
OPTION_NOTO, // notification options
OPTION_USI, // use static IP
OPTION_SSID, // wifi ssid
OPTION_PASS, // wifi password
OPTION_SSID1, // wifi ssid1
OPTION_PASS1, // wifi password1
OPTION_AUTH, // Blynk authentication token
OPTION_BDMN, // Blynk Domain
OPTION_BPRT, // Blynk Port
Expand All @@ -146,6 +146,8 @@ typedef enum {
OPTION_GWIP, // gateway IP
OPTION_SUBN, // subnet
OPTION_DNS1, // dns1 IP
OPTION_SSID2, // wifi ssid1
OPTION_PASS2, // wifi password1
NUM_OPTIONS // number of options
} OG_OPTION_enum;

Expand Down
21 changes: 14 additions & 7 deletions OpenGarage/espconnect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "espconnect.h"

ESP8266WiFiMulti wifiMulti;
//const char html_ap_redirect[] PROGMEM = "<h3>WiFi config saved. Now switching to station mode.</h3>";

String scan_network() {
Expand Down Expand Up @@ -62,8 +63,8 @@ void start_network_ap(const char *ssid, const char *pass) {
WiFi.disconnect(); // disconnect from router
}

void start_network_sta(const char *ssid, const char *pass, bool staonly) {
if(!ssid || !pass) return;
void start_network_sta(const char *ssid1, const char *pass1, const char *ssid2, const char *pass2, bool staonly) {
if(!ssid1 || !pass1) return;
DEBUG_PRINTLN(F("Sarting start_network_sta"));
if(staonly){
DEBUG_PRINTLN(F("Setting STA mode"));
Expand All @@ -74,14 +75,20 @@ void start_network_sta(const char *ssid, const char *pass, bool staonly) {
if(WiFi.getMode() != WIFI_AP_STA) WiFi.mode(WIFI_AP_STA);
DEBUG_PRINTLN(F("Setting to AP+STA mode"));
}
WiFi.begin(ssid, pass);
wifiMulti.addAP(ssid1, pass1);
if (ssid2 && pass2) {
wifiMulti.addAP(ssid2, pass2);
}
}

void start_network_sta_with_ap(const char *ssid, const char *pass) {
start_network_sta(ssid, pass, false);
void start_network_sta_with_ap(const char *ssid1, const char *pass1, const char *ssid2, const char *pass2) {
start_network_sta(ssid1, pass1, ssid2, pass2, false);
}

void start_network_sta(const char *ssid, const char *pass) {
start_network_sta(ssid, pass, true);
void start_network_sta(const char *ssid1, const char *pass1, const char *ssid2, const char *pass2) {
start_network_sta(ssid1, pass1, ssid2, pass2, true);
}

wl_status_t wifi_run() {
return wifiMulti.run();
}
6 changes: 4 additions & 2 deletions OpenGarage/espconnect.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiUdp.h>
#include <time.h>
#include "defines.h"
#include "htmls.h"

String scan_network();
void start_network_ap(const char *ssid, const char *pass);
void start_network_sta(const char *ssid, const char *pass);
void start_network_sta_with_ap(const char *ssid, const char *pass);
void start_network_sta(const char *ssid1, const char *pass1, const char *ssid2, const char *pass2);
void start_network_sta_with_ap(const char *ssid1, const char *pass1, const char *ssid2, const char *pass2);
wl_status_t wifi_run();

#endif
17 changes: 10 additions & 7 deletions OpenGarage/html/ap_home.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
</table>
<br><br>
<table cellspacing=16>
<tr><td><input type='text' name='ssid' id='ssid' style='font-size:14pt;height:28px;'></td><td>(Your WiFi SSID)</td></tr>
<tr><td><input type='password' name='pass' id='pass' style='font-size:14pt;height:28px;'></td><td>(Your WiFi Password)</td></tr>
<tr><td><input type='text' name='ssid1' id='ssid1' style='font-size:14pt;height:28px;'></td><td>(Your WiFi SSID)</td></tr>
<tr><td><input type='password' name='pass1' id='pass1' style='font-size:14pt;height:28px;'></td><td>(Your WiFi Password)</td></tr>
<tr><td><input type='text' name='ssid2' id='ssid2' style='font-size:14pt;height:28px;'></td><td>(Your Secondary WiFi SSID)</td></tr>
<tr><td><input type='password' name='pass2' id='pass2' style='font-size:14pt;height:28px;'></td><td>(Your Secondary WiFi Password)</td></tr>
<tr><td><input type='text' name='auth' id='auth' style='font-size:14pt;height:28px;'></td><td><label id='lbl_auth'>(Blynk Token, Optional)</label></td></tr>
<tr><td colspan=2><p id='msg'></p></td></tr>
<tr><td><button type='button' id='butt' onclick='sf();' style='height:36px;width:180px'>Submit</button></td><td></td></tr>
</table>
<script>
function id(s) {return document.getElementById(s);}
function sel(i) {id('ssid').value=id('rd'+i).value;}
function sel(i,a) {id('ssid'+a).value=id('rd'+i).value;}

var tci;
function tryConnect() {
var xhr=new XMLHttpRequest();
Expand All @@ -45,12 +48,12 @@
if(xhr.readyState==4 && xhr.status==200) {
var jd=JSON.parse(xhr.responseText);
if(jd.result==1) { id('butt').innerHTML='Connecting...'; id('msg').innerHTML='<font color=gray>Connecting, please wait...</font>'; tci=setInterval(tryConnect, 2000); return; }
id('msg').innerHTML='<b><font color=red>Error code: '+jd.result+', item: '+jd.item+'</font></b>'; id('butt').innerHTML='Submit'; id('butt').disabled=false;id('ssid').disabled=false;id('pass').disabled=false;id('auth').disabled=false;
id('msg').innerHTML='<b><font color=red>Error code: '+jd.result+', item: '+jd.item+'</font></b>'; id('butt').innerHTML='Submit'; id('butt').disabled=false;id('ssid1').disabled=false;id('pass1').disabled=false;id('ssid2').disabled=false;id('pass2').disabled=false;id('auth').disabled=false;
}
};
var comm='cc?ssid='+encodeURIComponent(id('ssid').value)+'&pass='+encodeURIComponent(id('pass').value)+'&auth='+id('auth').value;
var comm='cc?ssid1='+encodeURIComponent(id('ssid1').value)+'&pass1='+encodeURIComponent(id('pass1').value)+'&ssid2='+encodeURIComponent(id('ssid2').value)+'&pass2='+encodeURIComponent(id('pass2').value)+'&auth='+id('auth').value;
xhr.open('GET', comm, true); xhr.send();
id('butt').disabled=true;id('ssid').disabled=true;id('pass').disabled=true;id('auth').disabled=true;
id('butt').disabled=true;id('ssid1').disabled=true;id('pass1').disabled=true;id('ssid2').disabled=true;id('pass2').disabled=true;id('auth').disabled=true;
}
function loadSSIDs() {
var xhr=new XMLHttpRequest();
Expand All @@ -62,7 +65,7 @@
for(i=0;i<jd.ssids.length;i++) {
var signalstrength= jd.rssis[i]>-71?'Ok':(jd.rssis[i]>-81?'Weak':'Poor');
var row=id('rd').insertRow(-1);
row.innerHTML ="<tr><td><input name='ssids' id='rd"+i+"' onclick='sel(" + i + ")' type='radio' value='"+jd.ssids[i]+"'>" + jd.ssids[i] + "</td>" + "<td align='center'>"+signalstrength+"</td>" + "<td align='center'>("+jd.rssis[i] +" dbm)</td>" + "</tr>";
row.innerHTML ="<tr><td><input name='ssids' id='rd"+i+"' onclick='sel(" + i + ", 1)' type='radio' value='"+jd.ssids[i]+"'><input name='ssids' id='rd"+i+"' onclick='sel(" + i + ", 2)' type='radio' value='"+jd.ssids[i]+"'>" + jd.ssids[i] + "</td>" + "<td align='center'>"+signalstrength+"</td>" + "<td align='center'>("+jd.rssis[i] +" dbm)</td>" + "</tr>";
}
};
}
Expand Down
16 changes: 9 additions & 7 deletions OpenGarage/htmls.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ table#rd td { border: 1px solid black; border-collapse: collapse;}</style>
</table>
<br><br>
<table cellspacing=16>
<tr><td><input type='text' name='ssid' id='ssid' style='font-size:14pt;height:28px;'></td><td>(Your WiFi SSID)</td></tr>
<tr><td><input type='password' name='pass' id='pass' style='font-size:14pt;height:28px;'></td><td>(Your WiFi Password)</td></tr>
<tr><td><input type='text' name='ssid1' id='ssid1' style='font-size:14pt;height:28px;'></td><td>(Your WiFi SSID)</td></tr>
<tr><td><input type='password' name='pass1' id='pass1' style='font-size:14pt;height:28px;'></td><td>(Your WiFi Password)</td></tr>
<tr><td><input type='text' name='ssid2' id='ssid2' style='font-size:14pt;height:28px;'></td><td>(Your Secondary WiFi SSID)</td></tr>
<tr><td><input type='password' name='pass2' id='pass2' style='font-size:14pt;height:28px;'></td><td>(Your Secondary WiFi Password)</td></tr>
<tr><td><input type='text' name='auth' id='auth' style='font-size:14pt;height:28px;'></td><td><label id='lbl_auth'>(Blynk Token, Optional)</label></td></tr>
<tr><td colspan=2><p id='msg'></p></td></tr>
<tr><td><button type='button' id='butt' onclick='sf();' style='height:36px;width:180px'>Submit</button></td><td></td></tr>
</table>
<script>
function id(s) {return document.getElementById(s);}
function sel(i) {id('ssid').value=id('rd'+i).value;}
function sel(i,a) {id('ssid'+a).value=id('rd'+i).value;}
var tci;
function tryConnect() {
var xhr=new XMLHttpRequest();
Expand All @@ -45,12 +47,12 @@ xhr.onreadystatechange=function() {
if(xhr.readyState==4 && xhr.status==200) {
var jd=JSON.parse(xhr.responseText);
if(jd.result==1) { id('butt').innerHTML='Connecting...'; id('msg').innerHTML='<font color=gray>Connecting, please wait...</font>'; tci=setInterval(tryConnect, 2000); return; }
id('msg').innerHTML='<b><font color=red>Error code: '+jd.result+', item: '+jd.item+'</font></b>'; id('butt').innerHTML='Submit'; id('butt').disabled=false;id('ssid').disabled=false;id('pass').disabled=false;id('auth').disabled=false;
id('msg').innerHTML='<b><font color=red>Error code: '+jd.result+', item: '+jd.item+'</font></b>'; id('butt').innerHTML='Submit'; id('butt').disabled=false;id('ssid1').disabled=false;id('pass1').disabled=false;id('ssid2').disabled=false;id('pass2').disabled=false;id('auth').disabled=false;
}
};
var comm='cc?ssid='+encodeURIComponent(id('ssid').value)+'&pass='+encodeURIComponent(id('pass').value)+'&auth='+id('auth').value;
var comm='cc?ssid1='+encodeURIComponent(id('ssid1').value)+'&pass1='+encodeURIComponent(id('pass1').value)+'&ssid2='+encodeURIComponent(id('ssid2').value)+'&pass2='+encodeURIComponent(id('pass2').value)+'&auth='+id('auth').value;
xhr.open('GET', comm, true); xhr.send();
id('butt').disabled=true;id('ssid').disabled=true;id('pass').disabled=true;id('auth').disabled=true;
id('butt').disabled=true;id('ssid1').disabled=true;id('pass1').disabled=true;id('ssid2').disabled=true;id('pass2').disabled=true;id('auth').disabled=true;
}
function loadSSIDs() {
var xhr=new XMLHttpRequest();
Expand All @@ -62,7 +64,7 @@ var i, jd=JSON.parse(xhr.responseText);
for(i=0;i<jd.ssids.length;i++) {
var signalstrength= jd.rssis[i]>-71?'Ok':(jd.rssis[i]>-81?'Weak':'Poor');
var row=id('rd').insertRow(-1);
row.innerHTML ="<tr><td><input name='ssids' id='rd"+i+"' onclick='sel(" + i + ")' type='radio' value='"+jd.ssids[i]+"'>" + jd.ssids[i] + "</td>" + "<td align='center'>"+signalstrength+"</td>" + "<td align='center'>("+jd.rssis[i] +" dbm)</td>" + "</tr>";
row.innerHTML ="<tr><td><input name='ssids' id='rd"+i+"' onclick='sel(" + i + ", 1)' type='radio' value='"+jd.ssids[i]+"'><input name='ssids' id='rd"+i+"' onclick='sel(" + i + ", 2)' type='radio' value='"+jd.ssids[i]+"'>" + jd.ssids[i] + "</td>" + "<td align='center'>"+signalstrength+"</td>" + "<td align='center'>("+jd.rssis[i] +" dbm)</td>" + "</tr>";
}
};
}
Expand Down
38 changes: 24 additions & 14 deletions OpenGarage/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,9 @@ void sta_change_options_main(const char *command) {
for(i=0;i<NUM_OPTIONS;i++,o++) {
const char *key = o->name.c_str();
// these options cannot be modified here
if(i==OPTION_FWV || i==OPTION_MOD || i==OPTION_SSID ||
i==OPTION_PASS || i==OPTION_DKEY)
if(i==OPTION_FWV || i==OPTION_MOD || i==OPTION_SSID1 ||
i==OPTION_PASS1 || i==OPTION_SSID2 ||
i==OPTION_PASS2 || i==OPTION_DKEY)
continue;

if(o->max) { // integer options
Expand Down Expand Up @@ -579,8 +580,9 @@ void sta_change_options_main(const char *command) {
for(i=0;i<NUM_OPTIONS;i++,o++) {
const char *key = o->name.c_str();
// these options cannot be modified here
if(i==OPTION_FWV || i==OPTION_MOD || i==OPTION_SSID ||
i==OPTION_PASS || i==OPTION_DKEY)
if(i==OPTION_FWV || i==OPTION_MOD || i==OPTION_SSID1 ||
i==OPTION_PASS1 || i==OPTION_SSID2 ||
i==OPTION_PASS2 || i==OPTION_DKEY)
continue;

if(o->max) { // integer options
Expand Down Expand Up @@ -624,7 +626,7 @@ void sta_options_fill_json(String& json) {
OptionStruct *o = og.options;
for(byte i=0;i<NUM_OPTIONS;i++,o++) {
if(!o->max) {
if(i==OPTION_PASS || i==OPTION_DKEY) { // do not output password or device key
if(i==OPTION_PASS1 || i==OPTION_PASS2 || i==OPTION_DKEY) { // do not output password or device key
continue;
} else {
json += F("\"");
Expand Down Expand Up @@ -661,9 +663,12 @@ void on_ap_scan() {

void on_ap_change_config() {
if(curr_mode == OG_MOD_STA) return;
if(server->hasArg("ssid")&&server->arg("ssid").length()!=0) {
og.options[OPTION_SSID].sval = server->arg("ssid");
og.options[OPTION_PASS].sval = server->arg("pass");
if(server->hasArg("ssid1")&&server->arg("ssid1").length()!=0) {
og.options[OPTION_SSID1].sval = server->arg("ssid1");
og.options[OPTION_PASS1].sval = server->arg("pass1");
og.options[OPTION_SSID2].sval = server->arg("ssid2");
og.options[OPTION_PASS2].sval = server->arg("pass2");

// if cloud token is provided, save it
if(server->hasArg("auth")&&server->arg("auth").length()!=0)
og.options[OPTION_AUTH].sval = server->arg("auth");
Expand Down Expand Up @@ -1365,9 +1370,11 @@ void do_loop() {
} else {
led_blink_ms = LED_SLOW_BLINK;
DEBUG_PRINT(F("Attempting to connect to SSID: "));
DEBUG_PRINTLN(og.options[OPTION_SSID].sval.c_str());
DEBUG_PRINT(og.options[OPTION_SSID1].sval.c_str());
DEBUG_PRINT(F(" or "));
DEBUG_PRINTLN(og.options[OPTION_SSID2].sval.c_str());
WiFi.mode(WIFI_STA);
start_network_sta(og.options[OPTION_SSID].sval.c_str(), og.options[OPTION_PASS].sval.c_str());
start_network_sta(og.options[OPTION_SSID1].sval.c_str(), og.options[OPTION_PASS1].sval.c_str(), og.options[OPTION_SSID2].sval.c_str(), og.options[OPTION_PASS2].sval.c_str());
og.config_ip();
og.state = OG_STATE_CONNECTING;
connecting_timeout = millis() + 60000;
Expand All @@ -1377,14 +1384,17 @@ void do_loop() {
case OG_STATE_TRY_CONNECT:
led_blink_ms = LED_SLOW_BLINK;
DEBUG_PRINT(F("Attempting to connect to SSID: "));
DEBUG_PRINTLN(og.options[OPTION_SSID].sval.c_str());
start_network_sta_with_ap(og.options[OPTION_SSID].sval.c_str(), og.options[OPTION_PASS].sval.c_str());
DEBUG_PRINT(og.options[OPTION_SSID1].sval.c_str());
DEBUG_PRINT(F(" or "));
DEBUG_PRINTLN(og.options[OPTION_SSID2].sval.c_str());
start_network_sta_with_ap(og.options[OPTION_SSID1].sval.c_str(), og.options[OPTION_PASS1].sval.c_str(), og.options[OPTION_SSID2].sval.c_str(), og.options[OPTION_PASS2].sval.c_str());
wifi_run(); // First wifi_run needed for initual setup.
og.config_ip();
og.state = OG_STATE_CONNECTED;
break;

case OG_STATE_CONNECTING:
if(WiFi.status() == WL_CONNECTED) {
if(wifi_run() == WL_CONNECTED) {
DEBUG_PRINT(F("Wireless connected, IP: "));
DEBUG_PRINTLN(WiFi.localIP());

Expand Down Expand Up @@ -1465,7 +1475,7 @@ void do_loop() {
}

} else {
if(WiFi.status() == WL_CONNECTED) {
if(wifi_run() == WL_CONNECTED) {
//MDNS.update();
time_keeping();
check_status(); //This checks the door, sends info to services and processes the automation rules
Expand Down