Skip to content

Commit 12225ab

Browse files
Optimized WifiSerial API (#167)
Removed WifiSerial header, placed feature inside .ino file instead
1 parent 7227796 commit 12225ab

File tree

5 files changed

+74
-101
lines changed

5 files changed

+74
-101
lines changed

Arduino_package/hardware/libraries/WiFi/examples/ConnectWithWiFi/ConnectWithWEP/ConnectWithWEP.ino

+23-29
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,13 @@
2424
*/
2525

2626
#include <WiFi.h>
27-
#include "WifiSerial.h"
28-
2927

3028
// Set if user wants to key in ssid/pwd manually during operation
3129
//#define MANUAL_INPUT
3230

3331
#ifdef MANUAL_INPUT // initialise ssid string, pwd string, and serial_in object
3432
// Initialise strings
3533
String str_ssid, str_pass, str_key;
36-
// Create serial_in object
37-
WifiSerial wifiSerial;
3834
#endif
3935
// 0: Exactly 10 or 26 hexadecimal characters; 1:Exactly 5 or 13 ASCII characters
4036
#define password_type 0
@@ -77,35 +73,33 @@ void setup() {
7773
while (status != WL_CONNECTED) {
7874
#ifdef MANUAL_INPUT
7975
Serial.println("Enter your ssid");
80-
while (str_ssid.length() == 0) {
81-
str_ssid = wifiSerial.readInput();
82-
if (str_ssid.length() != 0) { // user has entered data
83-
Serial.print("SSID entered: ");
84-
Serial.println(str_ssid.c_str());
85-
}
86-
}
76+
while (Serial.available() == 0){}
77+
str_ssid = Serial.readString();
78+
str_ssid.trim();
79+
Serial.print("SSID entered: ");
80+
Serial.println(str_ssid);
8781

8882
Serial.println("Enter your network key index number");
89-
while (str_key.length() == 0) {
90-
str_key = wifiSerial.readInput();
91-
if (str_key.length() != 0) { // user has entered data
92-
Serial.print("key entered: ");
93-
Serial.println(str_key.c_str());
94-
}
95-
}
83+
while (Serial.available() == 0){}
84+
str_key = Serial.readString();
85+
str_key.trim();
86+
Serial.print("Key entered: ");
87+
Serial.println(str_key);
9688

9789
Serial.println("Enter your password");
98-
while (str_pass.length() == 0) {
99-
str_pass = wifiSerial.readInput();
100-
if (str_pass.length() != 0) { // user has entered data
101-
if (str_pass.length() <8) { // to catch password length < 8 exception
102-
Serial.println("Password cannot be less than 8 characters! Try again");
103-
str_pass = ""; // clear entered pwd and try again
104-
}
105-
Serial.print("Password entered: ");
106-
Serial.println(str_pass.c_str());
90+
while (Serial.available() == 0) {}
91+
str_pass = Serial.readString();
92+
str_pass.trim();
93+
if (str_pass.length() != 0) { // user has entered data
94+
while (str_pass.length() < 5 ) { // to catch pwd<5 exception
95+
Serial.println("Password cannot be less than 5 characters! Try again");
96+
while (Serial.available() == 0) {}
97+
str_pass = Serial.readString();
98+
str_pass.trim();
10799
}
108-
}
100+
Serial.print("Password entered: ");
101+
Serial.println(str_pass);
102+
}
109103
#endif
110104

111105
Serial.print("Attempting to connect to WEP network, SSID: ");
@@ -127,7 +121,7 @@ void setup() {
127121
strcpy(pass_cust, str_pass.c_str());
128122
Serial.println(str_ssid.c_str());
129123
status = WiFi.begin(ssid_cust,atoi(key_cust), pass_cust);
130-
str_ssid, str_key, str_pass = "";
124+
str_ssid = str_key = str_pass = "";
131125
#endif
132126
// wait 10 seconds for connection:
133127
delay(10000);

Arduino_package/hardware/libraries/WiFi/examples/ConnectWithWiFi/ConnectWithWPA/ConnectWithWPA.ino

+18-19
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@
1414
*/
1515

1616
#include <WiFi.h>
17-
#include "WifiSerial.h"
1817

1918
// Set if user wants to key in ssid/pwd manually during operation
2019
//#define MANUAL_INPUT
2120

2221
#ifdef MANUAL_INPUT // Initialise ssid string, pwd string, and serial_in object
2322
// Initialise strings
2423
String str_ssid, str_pass;
25-
// Create serial_in object
26-
WifiSerial wifiSerial;
2724
#endif
2825

2926
// If you are connecting to an iPhone WiFi hotspot, the default SSID uses Unicode (U+2019) Right Single Quotation Mark instead of ASCII apostrophe
@@ -55,28 +52,29 @@ void setup() {
5552
while (status != WL_CONNECTED) {
5653
#ifdef MANUAL_INPUT
5754
Serial.println("Enter your ssid");
58-
while (str_ssid.length() == 0) {
59-
str_ssid = wifiSerial.readInput();
60-
if (str_ssid.length() != 0) { // user has entered data
61-
Serial.print("SSID entered: ");
62-
Serial.println(str_ssid.c_str());
63-
}
64-
}
55+
while (Serial.available() == 0) {}
56+
str_ssid = Serial.readString();
57+
str_ssid.trim();
58+
Serial.print("SSID entered: ");
59+
Serial.println(str_ssid);
60+
6561
Serial.println("Enter your password");
66-
while (str_pass.length() == 0) {
67-
str_pass = wifiSerial.readInput();
68-
if (str_pass.length() != 0) { // user has entered data
69-
if (str_pass.length() <8) { // to catch pwd<8 exception
62+
while (Serial.available() == 0) {}
63+
str_pass = Serial.readString();
64+
str_pass.trim();
65+
if (str_pass.length() != 0) { // user has entered data
66+
while (str_pass.length() <8 ) { // to catch pwd<8 exception
7067
Serial.println("Password cannot be less than 8 characters! Try again");
71-
str_pass = ""; // clear entered pwd and try again
68+
while (Serial.available() == 0) {}
69+
str_pass = Serial.readString();
70+
str_pass.trim();
7271
}
73-
Serial.print("Password entered: ");
74-
Serial.println(str_pass.c_str());
72+
Serial.print("Password entered: ");
73+
Serial.println(str_pass);
7574
}
76-
}
7775
#endif
7876
Serial.print("Attempting to connect to WPA SSID: ");
79-
77+
8078
#ifndef MANUAL_INPUT
8179
Serial.println(ssid);
8280
// Connect to WPA/WPA2 network:
@@ -88,6 +86,7 @@ void setup() {
8886
strcpy(pass_cust, str_pass.c_str());
8987
Serial.println(str_ssid.c_str());
9088
status = WiFi.begin(ssid_cust, pass_cust);
89+
str_ssid = str_pass = "";
9190
#endif
9291
// wait 10 seconds for connection:
9392
delay(10000);

Arduino_package/hardware/libraries/WiFi/examples/WiFiAPMode/WiFiAPMode.ino

+33-19
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
*/
66

77
#include <WiFi.h>
8-
#include "WifiSerial.h"
9-
108
// Set if user wants to key in ssid/pwd manually during operation
119
//#define MANUAL_INPUT
1210

1311
#ifdef MANUAL_INPUT // Initialise ssid string, pwd string, and serial_in object
1412
// Initialise strings
15-
String str_ssid, str_pass;
16-
// Create serial_in object
17-
WifiSerial wifiSerial;
13+
String str_ssid, str_pass, str_channel;
1814
#endif
1915

2016
// UTF-8 encoding can also be used for SSID with emoji characters
@@ -44,25 +40,40 @@ void setup() {
4440
while (status != WL_CONNECTED) {
4541
#ifdef MANUAL_INPUT
4642
Serial.println("Enter your ssid");
47-
while (str_ssid.length() == 0) {
48-
str_ssid = wifiSerial.readInput();
49-
if (str_ssid.length() != 0) { //user has entered data
43+
while (Serial.available() == 0) {}
44+
str_ssid = Serial.readString();
45+
str_ssid.trim();
5046
Serial.print("SSID entered: ");
51-
Serial.println(str_ssid.c_str());
52-
}
53-
}
47+
Serial.println(str_ssid);
48+
5449
Serial.println("Enter your password");
55-
while (str_pass.length() == 0) {
56-
str_pass = wifiSerial.readInput();
57-
if (str_pass.length() != 0) { //user has entered data
58-
if (str_pass.length() <8) { //to catch pwd<8 exception
50+
while (Serial.available() == 0) {}
51+
str_pass = Serial.readString();
52+
str_pass.trim();
53+
if (str_pass.length() != 0) { // user has entered data
54+
while (str_pass.length() <8 ) { // to catch pwd<8 exception
5955
Serial.println("Password cannot be less than 8 characters! Try again");
60-
str_pass = ""; //clear entered pwd and try again
56+
while (Serial.available() == 0) {}
57+
str_pass = Serial.readString();
58+
str_pass.trim();
6159
}
62-
Serial.print("Password entered: ");
63-
Serial.println(str_pass.c_str());
60+
Serial.print("Password entered: ");
61+
Serial.println(str_pass);
62+
}
63+
64+
Serial.println("Enter your channel number");
65+
while (Serial.available() == 0) {}
66+
str_channel = Serial.readString();
67+
int checker = str_channel.toInt();
68+
while(str_channel != (String(checker))){
69+
Serial.println("channel should be a number!");
70+
while (Serial.available() == 0) {}
71+
str_channel = Serial.readString();
72+
checker = str_channel.toInt();
6473
}
65-
}
74+
str_channel.trim();
75+
Serial.print("channel entered: ");
76+
Serial.println(str_channel);
6677
#endif
6778
Serial.print("Attempting to start AP with SSID: ");
6879
#ifndef MANUAL_INPUT
@@ -71,10 +82,13 @@ void setup() {
7182
#else
7283
char ssid_cust[str_ssid.length() + 1];
7384
char pass_cust[str_pass.length() + 1];
85+
char channel_cust[str_channel.length() + 1];
7486
strcpy(ssid_cust, str_ssid.c_str());
7587
strcpy(pass_cust, str_pass.c_str());
88+
strcpy(channel_cust, str_channel.c_str());
7689
Serial.println(str_ssid.c_str());
7790
status = WiFi.apbegin(ssid_cust, pass_cust, channel, ssid_status);
91+
str_ssid = str_pass = str_channel = "";
7892
#endif
7993
delay(10000);
8094
}

Arduino_package/hardware/libraries/WiFi/src/WifiSerial.cpp

-21
This file was deleted.

Arduino_package/hardware/libraries/WiFi/src/WifiSerial.h

-13
This file was deleted.

0 commit comments

Comments
 (0)