Skip to content

Commit 59c7ec4

Browse files
committed
fix modem
Former-commit-id: e73d127
1 parent 501ac20 commit 59c7ec4

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

libraries/WiFiS3/src/Modem.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,18 @@ void ModemClass::end(){
3030
}
3131

3232
bool ModemClass::write(const string &prompt, string &data_res, char * fmt, ...){
33+
34+
data_res.clear();
35+
3336
memset(tx_buff,0x00,MAX_BUFF_SIZE);
3437
va_list va;
3538
va_start (va, fmt);
3639
vsprintf ((char *)tx_buff, fmt, va);
3740
va_end (va);
3841
#ifdef MODEM_DEBUG
39-
Serial.print("tx_buff: ");
40-
for(int i =0; i<MAX_BUFF_SIZE; i++) {
41-
Serial.print(tx_buff[i], HEX);
42-
Serial.print(" ");
43-
}
44-
Serial.println();
42+
Serial.print("Write Call, command sent: ");
4543
Serial.write(tx_buff,strlen((char *)tx_buff));
46-
Serial.println("ciao");
44+
Serial.println();
4745
#endif
4846
_serial->write(tx_buff,strlen((char *)tx_buff));
4947
return buf_read(prompt,data_res);;
@@ -52,20 +50,24 @@ bool ModemClass::write(const string &prompt, string &data_res, char * fmt, ...){
5250

5351
bool ModemClass::buf_read(const string &prompt, string &data_res) {
5452
bool res = false;
53+
bool found = false;
5554
unsigned long start_time = millis();
56-
while(millis() - start_time < _timeout){
55+
while(millis() - start_time < _timeout && !found){
5756
while(_serial->available()){
5857
char c = _serial->read();
5958
data_res += c;
60-
Serial.print(c);
59+
//Serial.print(c);
6160
if(string::npos != data_res.rfind(PROMPT_OK)){
62-
data_res.substr(0, data_res.length() - sizeof(PROMPT_OK));
61+
found = true;
62+
data_res = data_res.substr(0, data_res.length() - sizeof(PROMPT_OK));
6363
if(prompt != DO_NOT_CHECK_CMD) {
6464
if(removeAtBegin(data_res, prompt)) {
6565
res = true;
6666
}
6767
}
68-
res = true;
68+
else {
69+
res = true;
70+
}
6971
break;
7072
}
7173
else if (string::npos != data_res.rfind(PROMPT_ERROR)) {
@@ -75,10 +77,19 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) {
7577
}
7678
}
7779
}
80+
trim(data_res);
7881
#ifdef MODEM_DEBUG
79-
Serial.print("data_res: ");
80-
Serial.println(data_res.c_str());
82+
Serial.print("Write Call, response rx |>>");
83+
Serial.print(data_res.c_str());
84+
Serial.println("<<|");
85+
if(res) {
86+
Serial.println("Result: OK");
87+
}
88+
else {
89+
Serial.println("Result: FAILED");
90+
}
8191
#endif
92+
8293
return res;
8394
}
8495

libraries/WiFiS3/src/WiFi.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,26 @@ int CWifi::begin(const char* ssid) {
2323
/* -------------------------------------------------------------------------- */
2424
int CWifi::begin(const char* ssid, const char *passphrase) {
2525
/* -------------------------------------------------------------------------- */
26-
modem.begin();
2726
string res = "";
28-
if(modem.write(string(PROMPT(_BEGINSTA)),res, "%s%s,%s\r\n" , CMD_WRITE(_BEGINSTA), ssid, passphrase)) {
29-
return atoi(res.c_str());
27+
modem.begin();
28+
if(!modem.write(string(PROMPT(_MODE)),res, "%s%d\r\n" , CMD_WRITE(_MODE), 1)) {
29+
return WL_CONNECT_FAILED;
3030
}
31-
return 0;
31+
32+
if(!modem.write(string(PROMPT(_BEGINSTA)),res, "%s%s,%s\r\n" , CMD_WRITE(_BEGINSTA), ssid, passphrase)) {
33+
return WL_CONNECT_FAILED;
34+
}
35+
36+
unsigned long start_time = millis();
37+
while(millis() - start_time < 10000){
38+
if(modem.write(string(PROMPT(_GETSTATUS)),res,CMD(_GETSTATUS))) {
39+
if(atoi(res.c_str()) == WL_CONNECTED) {
40+
return WL_CONNECTED;
41+
}
42+
}
43+
}
44+
45+
return WL_CONNECT_FAILED;
3246
}
3347

3448
/* passphrase is needed so a default one will be set */

0 commit comments

Comments
 (0)