Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 7710d94

Browse files
committed
firebase: support quoted ssid and empty passwords
1 parent f259c16 commit 7710d94

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

firebase/firebase.ino

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct Credential {
2626
char ssid[32];
2727
char password[32];
2828
} cred;
29+
const int maxWifiConnectAttemps = 20;
2930

3031
String buffer;
3132

@@ -38,26 +39,34 @@ void setup() {
3839
Serial.println("!I #!/dev/fail");
3940
Serial.print("!I token: ");
4041
Serial.println(cred.token);
42+
bool connected = false;
4143
if (strlen(cred.ssid) > 0) {
42-
wifiConnect();
44+
connected = wifiConnect();
45+
}
46+
if (!connected) {
47+
Serial.println("!E no wifi connection");
4348
}
4449
}
4550

4651
bool wifiConnect() {
47-
Serial.print("!I connecting to wifi");
52+
Serial.println("!I connecting to wifi");
53+
Serial.print("!I ssid: ");
54+
Serial.println(cred.ssid);
55+
Serial.print("!I ");
4856
WiFi.begin(cred.ssid, cred.password);
49-
for (int i = 0; i < 20; i++) {
57+
for (int i = 0; i < maxWifiConnectAttemps; i++) {
5058
delay(500);
5159
Serial.print(".");
5260
if (WiFi.status() == WL_CONNECTED) {
5361
Serial.println("");
54-
Serial.print("!I wifi connected: ");
55-
Serial.println(WiFi.localIP());
62+
Serial.println("!I wifi connected");
63+
Serial.print("!I ip: ");
64+
Serial.println(WiFi.localIP());
5665
return true;
5766
}
5867
}
5968
Serial.println("");
60-
Serial.println("!I wifi connection failed");
69+
Serial.println("!E wifi connection failed");
6170
return false;
6271
}
6372

@@ -68,6 +77,13 @@ String buildRequest(String data) {
6877
"Connection: close\r\n";
6978
}
7079

80+
String unquote(String s) {
81+
if (s[0] == '"' && s[s.length()-1] == '"') {
82+
return s.substring(1, s.length()-1);
83+
}
84+
return s;
85+
}
86+
7187
void loop() {
7288
String request;
7389

@@ -96,13 +112,18 @@ void loop() {
96112
} else if (data.startsWith("AT+CWJAP=")) {
97113
String ssidPwd = data.substring(9);
98114
int i = ssidPwd.indexOf(",");
99-
ssidPwd.substring(0, i).toCharArray(cred.ssid, sizeof(cred.token));
100-
ssidPwd.substring(i+1).toCharArray(cred.password, sizeof(cred.password));
115+
unquote(ssidPwd.substring(0, i)).toCharArray(cred.ssid, sizeof(cred.token));
116+
if (i != -1) {
117+
unquote(ssidPwd.substring(i+1)).toCharArray(cred.password, sizeof(cred.password));
118+
} else {
119+
cred.password[0] = '\0';
120+
}
101121
Serial.println("!I ssid: " + String(cred.ssid));
102122
Serial.println("!I password: " + String(cred.password));
103123
if (wifiConnect()) {
104124
EEPROM.put(0, cred);
105-
EEPROM.commit();
125+
EEPROM.commit();
126+
Serial.println("OK");
106127
}
107128
} else {
108129
Serial.println("!E usage:");
@@ -111,6 +132,7 @@ void loop() {
111132
Serial.println("!E payload");
112133
Serial.println("!E TOKEN token");
113134
Serial.println("!E AT+CWJAP=ssid,passwd");
135+
Serial.println("!E AT+CWSAP=ssid");
114136
}
115137
}
116138

0 commit comments

Comments
 (0)