Skip to content

Commit ea17475

Browse files
committed
bug fixing on Modem, plus setting soft AP ip address
Former-commit-id: bca036a
1 parent ebacbc0 commit ea17475

File tree

4 files changed

+82
-53
lines changed

4 files changed

+82
-53
lines changed

libraries/WiFiS3/examples/AP_SimpleWebServer/AP_SimpleWebServer.ino

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616
adapted to WiFi AP by Adafruit
1717
*/
1818

19-
#include "WiFi.h"
20-
#include "WiFiServer.h"
21-
22-
19+
#include "WiFiS3.h"
2320

2421
#include "arduino_secrets.h"
22+
2523
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
2624
char ssid[] = SECRET_SSID; // your network SSID (name)
27-
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
28-
int keyIndex = 0; // your network key index number (needed only for WEP)
25+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
26+
int keyIndex = 0; // your network key index number (needed only for WEP)
2927

3028
int led = LED_BUILTIN;
3129
int status = WL_IDLE_STATUS;
@@ -49,13 +47,13 @@ void setup() {
4947
}
5048

5149
String fv = WiFi.firmwareVersion();
52-
if (fv < WiFi_FIRMWARE_LATEST_VERSION) {
50+
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
5351
Serial.println("Please upgrade the firmware");
5452
}
5553

5654
// by default the local IP address will be 192.168.4.1
5755
// you can override it with the following:
58-
//WiFi.config(IPAddress(10, 10, 5, 1));
56+
WiFi.config(IPAddress(192,48,56,2));
5957

6058
// print the network name (SSID);
6159
Serial.print("Creating access point named: ");
@@ -81,17 +79,18 @@ void setup() {
8179

8280

8381
void loop() {
82+
8483
// compare the previous status to the current status
85-
if (status != WiFi.status()) {
84+
if (1/*status != WiFi.status()*/) {
8685
// it has changed update the variable
87-
status = WiFi.status();
86+
/*status = WiFi.status()*/
8887

8988
if (status == WL_AP_CONNECTED) {
9089
// a device has connected to the AP
91-
Serial.println("Device connected to AP");
90+
//Serial.println("Device connected to AP");
9291
} else {
9392
// a device has disconnected from the AP, and we are back in listening mode
94-
Serial.println("Device disconnected from AP");
93+
//Serial.println("Device disconnected from AP");
9594
}
9695
}
9796

@@ -117,8 +116,8 @@ void loop() {
117116
client.println();
118117

119118
// the content of the HTTP response follows the header:
120-
client.print("<p style=\"font-size:7vw;\">Click <a href=\"/H\">here</a> turn the LED off<br></p>");
121-
client.print("<p style=\"font-size:7vw;\">Click <a href=\"/L\">here</a> turn the LED on<br></p>");
119+
client.print("<p style=\"font-size:7vw;\">Click <a href=\"/H\">here</a> turn the LED on<br></p>");
120+
client.print("<p style=\"font-size:7vw;\">Click <a href=\"/L\">here</a> turn the LED off<br></p>");
122121

123122
// The HTTP response ends with another blank line:
124123
client.println();

libraries/WiFiS3/src/Modem.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ bool ModemClass::read_by_size_finished(string &rx) {
153153
int pos_space = rx.find(" ");
154154
if(pos != string::npos && pos_space != string::npos) {
155155
string n = rx.substr(pos_space,pos);
156-
data_to_be_received = atoi(n.c_str());
156+
/* add 4 because OK\r\n is always added at the end of data */
157+
data_to_be_received = atoi(n.c_str()) + 4;
157158
rx.clear();
158159
data_received = 0;
159160
st = WAIT_FOR_DATA;
@@ -178,6 +179,8 @@ bool ModemClass::read_by_size_finished(string &rx) {
178179
return rv;
179180
}
180181

182+
183+
181184
/* -------------------------------------------------------------------------- */
182185
bool ModemClass::buf_read(const string &prompt, string &data_res) {
183186
/* -------------------------------------------------------------------------- */
@@ -186,29 +189,15 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) {
186189

187190
unsigned long start_time = millis();
188191
while((millis() - start_time < _timeout) && !found){
189-
while(_serial->available()){
192+
while( _serial->available() ){
190193
char c = _serial->read();
191194
data_res += c;
192-
195+
193196
if(read_by_size) {
194197
if(read_by_size_finished(data_res)) {
195198
found = true;
196199
read_by_size = false;
197200
res = true;
198-
199-
unsigned long start = millis();
200-
bool ok_found = false;
201-
while(millis() - start < _timeout && !ok_found) {
202-
string ok = "";
203-
while(_serial->available()){
204-
char c = _serial->read();
205-
ok += c;
206-
if(ok.size() - ok.rfind("OK\r\n") == 4 && ok.rfind("OK\r\n") != string::npos) {
207-
ok_found = true;
208-
break;
209-
}
210-
}
211-
}
212201
}
213202
}
214203
else {

libraries/WiFiS3/src/WiFi.cpp

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ const char* CWifi::firmwareVersion() {
3131
/* -------------------------------------------------------------------------- */
3232
int CWifi::begin(const char* ssid) {
3333
/* -------------------------------------------------------------------------- */
34-
35-
return 0;
34+
return begin(ssid,nullptr);;
3635
}
3736

3837
/* -------------------------------------------------------------------------- */
@@ -42,9 +41,15 @@ int CWifi::begin(const char* ssid, const char *passphrase) {
4241
modem.begin();
4342
modem.write(string(PROMPT(_MODE)),res, "%s%d\r\n" , CMD_WRITE(_MODE), 1);
4443

45-
46-
if(!modem.write(string(PROMPT(_BEGINSTA)),res, "%s%s,%s\r\n" , CMD_WRITE(_BEGINSTA), ssid, passphrase)) {
47-
return WL_CONNECT_FAILED;
44+
if(passphrase == nullptr) {
45+
if(!modem.write(string(PROMPT(_BEGINSTA)),res, "%s%s\r\n" , CMD_WRITE(_BEGINSTA), ssid)) {
46+
return WL_CONNECT_FAILED;
47+
}
48+
}
49+
else {
50+
if(!modem.write(string(PROMPT(_BEGINSTA)),res, "%s%s,%s\r\n" , CMD_WRITE(_BEGINSTA), ssid, passphrase)) {
51+
return WL_CONNECT_FAILED;
52+
}
4853
}
4954

5055
unsigned long start_time = millis();
@@ -83,24 +88,21 @@ uint8_t CWifi::beginAP(const char *ssid, const char* passphrase, uint8_t channel
8388
modem.write(string(PROMPT(_MODE)),res, "%s%d\r\n" , CMD_WRITE(_MODE), 2);
8489

8590
if(!modem.write(string(PROMPT(_BEGINSOFTAP)),res, "%s%s,%s,%d\r\n" , CMD_WRITE(_BEGINSOFTAP), ssid, passphrase, channel)) {
86-
return WL_CONNECT_FAILED;
91+
return WL_AP_FAILED;
8792
}
8893

8994
if(atoi(res.c_str()) == 1) {
90-
return true;
95+
return WL_AP_LISTENING;
9196
}
9297

93-
return false;
98+
return WL_AP_FAILED;
9499
}
95100

96-
97-
98-
99101
/* -------------------------------------------------------------------------- */
100102
void CWifi::config(IPAddress local_ip) {
101103
/* -------------------------------------------------------------------------- */
104+
102105
IPAddress _gw(local_ip[0],local_ip[1], local_ip[2], 1);
103-
Serial.println(_gw);
104106
IPAddress _sm(255,255,255,0);
105107
IPAddress dns(0,0,0,0);
106108
return _config(local_ip, _gw, _sm,dns,dns);
@@ -136,7 +138,12 @@ void CWifi::_config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPA
136138
_dns2 += to_string(dns2[2]) + ".";
137139
_dns2 += to_string(dns2[3]);
138140

139-
modem.write(string(PROMPT(_SETIP)),res, "%s%s,%s,%s,%s,%s\r\n" , CMD_WRITE(_SETIP), ip.c_str(), gw.c_str(), nm.c_str(),_dns1.c_str(),_dns2.c_str());
141+
ip_ap = local_ip;
142+
gw_ap = gateway;
143+
nm_ap = subnet;
144+
145+
modem.write(PROMPT(_SOFTAPCONFIG),res, "%s%s,%s,%s\r\n" , CMD_WRITE(_SOFTAPCONFIG), ip.c_str(), ip.c_str(), nm.c_str());
146+
modem.write(string(PROMPT(_SETIP)),res, "%s%s,%s,%s,%s,%s\r\n" , CMD_WRITE(_SETIP), ip.c_str(), gw.c_str(), nm.c_str(),_dns1.c_str(),_dns2.c_str());
140147
}
141148

142149
/* -------------------------------------------------------------------------- */
@@ -152,6 +159,7 @@ void CWifi::config(IPAddress local_ip, IPAddress dns_server) {
152159
/* -------------------------------------------------------------------------- */
153160
void CWifi::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway) {
154161
/* -------------------------------------------------------------------------- */
162+
155163
IPAddress _sm(255,255,255,0);
156164
IPAddress dns(0,0,0,0);
157165
return _config(local_ip, gateway, _sm,dns_server,dns);
@@ -160,6 +168,7 @@ void CWifi::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
160168
/* -------------------------------------------------------------------------- */
161169
void CWifi::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) {
162170
/* -------------------------------------------------------------------------- */
171+
163172
IPAddress dns(0,0,0,0);
164173
return _config(local_ip, gateway, subnet,dns_server,dns);
165174
}
@@ -181,7 +190,7 @@ void CWifi::setDNS(IPAddress dns_server1, IPAddress dns_server2) {
181190
void CWifi::setHostname(const char* name) {
182191
/* -------------------------------------------------------------------------- */
183192
string res = "";
184-
modem.write(string(DO_NOT_CHECK_CMD),res, "%s%s\r\n" , CMD_WRITE(_BEGINSOFTAP), name);
193+
modem.write(string(_HOSTNAME),res, "%s%s\r\n" , CMD_WRITE(_HOSTNAME), name);
185194
}
186195

187196
/* -------------------------------------------------------------------------- */
@@ -323,11 +332,23 @@ IPAddress CWifi::localIP() {
323332
/* -------------------------------------------------------------------------- */
324333
modem.begin();
325334
string res = "";
326-
if(modem.write(string(PROMPT(_IPSTA)),res, "%s%d\r\n" , CMD_WRITE(_IPSTA), IP_ADDR)) {
327-
IPAddress local_IP;
328-
local_IP.fromString(res.c_str());
329-
return local_IP;
335+
if(modem.write(string(PROMPT(_MODE)),res, "%s" , CMD_READ(_MODE))) {
336+
if(atoi(res.c_str()) == 1) {
337+
if(modem.write(string(PROMPT(_IPSTA)),res, "%s%d\r\n" , CMD_WRITE(_IPSTA), IP_ADDR)) {
338+
IPAddress local_IP;
339+
local_IP.fromString(res.c_str());
340+
return local_IP;
341+
}
342+
}
343+
else if(atoi(res.c_str()) == 2) {
344+
if(modem.write(string(PROMPT(_IPSOFTAP)),res, CMD(_IPSOFTAP))) {
345+
IPAddress local_IP;
346+
local_IP.fromString(res.c_str());
347+
return local_IP;
348+
}
349+
}
330350
}
351+
331352
return IPAddress(0,0,0,0);
332353
}
333354

@@ -432,8 +453,17 @@ uint8_t CWifi::channel(uint8_t networkItem) {
432453
const char* CWifi::SSID() {
433454
/* -------------------------------------------------------------------------- */
434455
string res = "";
435-
if(modem.write(string(PROMPT(_GETSSID)), res, CMD_READ(_GETSSID))) {
436-
return res.c_str();
456+
if(modem.write(string(PROMPT(_MODE)),res, "%s" , CMD_READ(_MODE))) {
457+
if(atoi(res.c_str()) == 1) {
458+
if(modem.write(string(PROMPT(_GETSSID)), res, CMD_READ(_GETSSID))) {
459+
return res.c_str();
460+
}
461+
}
462+
else if(atoi(res.c_str()) == 2) {
463+
if(modem.write(string(PROMPT(_GETSOFTAPSSID)), res, CMD_READ(_GETSOFTAPSSID))) {
464+
return res.c_str();
465+
}
466+
}
437467
}
438468
return "";
439469
}
@@ -443,8 +473,6 @@ uint8_t* CWifi::BSSID(uint8_t* bssid) {
443473
/* -------------------------------------------------------------------------- */
444474
string res = "";
445475
if(modem.write(string(PROMPT(_GETBSSID)), res, CMD_READ(_GETBSSID))) {
446-
Serial.print("BSSID: ");
447-
Serial.println(res.c_str());
448476
macStr2macArray(bssid, res.c_str());
449477
return bssid;
450478
}

libraries/WiFiS3/src/WiFi.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
using namespace std;
1313

14+
#define DEFAULT_IP_AP_ADDRESS IPAddress(192,168,4,1)
15+
#define DEFAULT_GW_AP_ADDRESS IPAddress(192,168,1,1)
16+
#define DEFAULT_NM_AP_ADDRESS IPAddress(255,255,255,0)
17+
18+
1419
#define WIFI_FIRMWARE_LATEST_VERSION "0.1.0"
1520

1621
class CAccessPoint {
@@ -32,6 +37,14 @@ class CWifi {
3237
unsigned long _timeout;
3338
uint8_t mak[6];
3439
vector<CAccessPoint> access_points;
40+
41+
42+
IPAddress ip_ap = DEFAULT_IP_AP_ADDRESS;
43+
IPAddress gw_ap = DEFAULT_GW_AP_ADDRESS;
44+
IPAddress nm_ap = DEFAULT_NM_AP_ADDRESS;
45+
46+
47+
3548

3649
public:
3750
CWifi();

0 commit comments

Comments
 (0)