@@ -31,8 +31,7 @@ const char* CWifi::firmwareVersion() {
31
31
/* -------------------------------------------------------------------------- */
32
32
int CWifi::begin (const char * ssid) {
33
33
/* -------------------------------------------------------------------------- */
34
-
35
- return 0 ;
34
+ return begin (ssid,nullptr );;
36
35
}
37
36
38
37
/* -------------------------------------------------------------------------- */
@@ -42,9 +41,15 @@ int CWifi::begin(const char* ssid, const char *passphrase) {
42
41
modem.begin ();
43
42
modem.write (string (PROMPT (_MODE)),res, " %s%d\r\n " , CMD_WRITE (_MODE), 1 );
44
43
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
+ }
48
53
}
49
54
50
55
unsigned long start_time = millis ();
@@ -83,24 +88,21 @@ uint8_t CWifi::beginAP(const char *ssid, const char* passphrase, uint8_t channel
83
88
modem.write (string (PROMPT (_MODE)),res, " %s%d\r\n " , CMD_WRITE (_MODE), 2 );
84
89
85
90
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 ;
87
92
}
88
93
89
94
if (atoi (res.c_str ()) == 1 ) {
90
- return true ;
95
+ return WL_AP_LISTENING ;
91
96
}
92
97
93
- return false ;
98
+ return WL_AP_FAILED ;
94
99
}
95
100
96
-
97
-
98
-
99
101
/* -------------------------------------------------------------------------- */
100
102
void CWifi::config (IPAddress local_ip) {
101
103
/* -------------------------------------------------------------------------- */
104
+
102
105
IPAddress _gw (local_ip[0 ],local_ip[1 ], local_ip[2 ], 1 );
103
- Serial.println (_gw);
104
106
IPAddress _sm (255 ,255 ,255 ,0 );
105
107
IPAddress dns (0 ,0 ,0 ,0 );
106
108
return _config (local_ip, _gw, _sm,dns,dns);
@@ -136,7 +138,12 @@ void CWifi::_config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPA
136
138
_dns2 += to_string (dns2[2 ]) + " ." ;
137
139
_dns2 += to_string (dns2[3 ]);
138
140
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 ());
140
147
}
141
148
142
149
/* -------------------------------------------------------------------------- */
@@ -152,6 +159,7 @@ void CWifi::config(IPAddress local_ip, IPAddress dns_server) {
152
159
/* -------------------------------------------------------------------------- */
153
160
void CWifi::config (IPAddress local_ip, IPAddress dns_server, IPAddress gateway) {
154
161
/* -------------------------------------------------------------------------- */
162
+
155
163
IPAddress _sm (255 ,255 ,255 ,0 );
156
164
IPAddress dns (0 ,0 ,0 ,0 );
157
165
return _config (local_ip, gateway, _sm,dns_server,dns);
@@ -160,6 +168,7 @@ void CWifi::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
160
168
/* -------------------------------------------------------------------------- */
161
169
void CWifi::config (IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) {
162
170
/* -------------------------------------------------------------------------- */
171
+
163
172
IPAddress dns (0 ,0 ,0 ,0 );
164
173
return _config (local_ip, gateway, subnet,dns_server,dns);
165
174
}
@@ -181,7 +190,7 @@ void CWifi::setDNS(IPAddress dns_server1, IPAddress dns_server2) {
181
190
void CWifi::setHostname (const char * name) {
182
191
/* -------------------------------------------------------------------------- */
183
192
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);
185
194
}
186
195
187
196
/* -------------------------------------------------------------------------- */
@@ -323,11 +332,23 @@ IPAddress CWifi::localIP() {
323
332
/* -------------------------------------------------------------------------- */
324
333
modem.begin ();
325
334
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
+ }
330
350
}
351
+
331
352
return IPAddress (0 ,0 ,0 ,0 );
332
353
}
333
354
@@ -432,8 +453,17 @@ uint8_t CWifi::channel(uint8_t networkItem) {
432
453
const char * CWifi::SSID () {
433
454
/* -------------------------------------------------------------------------- */
434
455
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
+ }
437
467
}
438
468
return " " ;
439
469
}
@@ -443,8 +473,6 @@ uint8_t* CWifi::BSSID(uint8_t* bssid) {
443
473
/* -------------------------------------------------------------------------- */
444
474
string res = " " ;
445
475
if (modem.write (string (PROMPT (_GETBSSID)), res, CMD_READ (_GETBSSID))) {
446
- Serial.print (" BSSID: " );
447
- Serial.println (res.c_str ());
448
476
macStr2macArray (bssid, res.c_str ());
449
477
return bssid;
450
478
}
0 commit comments