Skip to content

Commit dc37b65

Browse files
authored
Merge pull request #11 from bulislaw/feature_wifi
Port esp2866 driver to new WiFiInterface API
2 parents 69aa3c9 + 588eb44 commit dc37b65

File tree

4 files changed

+241
-23
lines changed

4 files changed

+241
-23
lines changed

ESP8266/ESP8266.cpp

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ bool ESP8266::disconnect(void)
7979
const char *ESP8266::getIPAddress(void)
8080
{
8181
if (!(_parser.send("AT+CIFSR")
82-
&& _parser.recv("+CIFSR:STAIP,\"%[^\"]\"", _ip_buffer)
82+
&& _parser.recv("+CIFSR:STAIP,\"%15[^\"]\"", _ip_buffer)
8383
&& _parser.recv("OK"))) {
8484
return 0;
8585
}
@@ -90,19 +90,84 @@ const char *ESP8266::getIPAddress(void)
9090
const char *ESP8266::getMACAddress(void)
9191
{
9292
if (!(_parser.send("AT+CIFSR")
93-
&& _parser.recv("+CIFSR:STAMAC,\"%[^\"]\"", _mac_buffer)
93+
&& _parser.recv("+CIFSR:STAMAC,\"%17[^\"]\"", _mac_buffer)
9494
&& _parser.recv("OK"))) {
9595
return 0;
9696
}
9797

9898
return _mac_buffer;
9999
}
100100

101+
const char *ESP8266::getGateway()
102+
{
103+
if (!(_parser.send("AT+CIPSTA?")
104+
&& _parser.recv("+CIPSTA:gateway:\"%15[^\"]\"", _gateway_buffer)
105+
&& _parser.recv("OK"))) {
106+
return 0;
107+
}
108+
109+
return _gateway_buffer;
110+
}
111+
112+
const char *ESP8266::getNetmask()
113+
{
114+
if (!(_parser.send("AT+CIPSTA?")
115+
&& _parser.recv("+CIPSTA:netmask:\"%15[^\"]\"", _netmask_buffer)
116+
&& _parser.recv("OK"))) {
117+
return 0;
118+
}
119+
120+
return _netmask_buffer;
121+
}
122+
123+
int8_t ESP8266::getRSSI()
124+
{
125+
int8_t rssi;
126+
char bssid[18];
127+
128+
if (!(_parser.send("AT+CWJAP?")
129+
&& _parser.recv("+CWJAP:\"%*[^\"]\",\"%17[^\"]\"", bssid)
130+
&& _parser.recv("OK"))) {
131+
return 0;
132+
}
133+
134+
if (!(_parser.send("AT+CWLAP=\"\",\"%s\",", bssid)
135+
&& _parser.recv("+CWLAP:(%*d,\"%*[^\"]\",%hhd,", &rssi)
136+
&& _parser.recv("OK"))) {
137+
return 0;
138+
}
139+
140+
return rssi;
141+
}
142+
101143
bool ESP8266::isConnected(void)
102144
{
103145
return getIPAddress() != 0;
104146
}
105147

148+
int ESP8266::scan(WiFiAccessPoint *res, unsigned limit)
149+
{
150+
unsigned cnt = 0;
151+
nsapi_wifi_ap_t ap;
152+
153+
if (!_parser.send("AT+CWLAP")) {
154+
return NSAPI_ERROR_DEVICE_ERROR;
155+
}
156+
157+
while (recv_ap(&ap)) {
158+
if (cnt < limit) {
159+
res[cnt] = WiFiAccessPoint(ap);
160+
}
161+
162+
cnt++;
163+
if (limit != 0 && cnt >= limit) {
164+
break;
165+
}
166+
}
167+
168+
return cnt;
169+
}
170+
106171
bool ESP8266::open(const char *type, int id, const char* addr, int port)
107172
{
108173
//IDs only 0-4
@@ -227,3 +292,15 @@ void ESP8266::attach(Callback<void()> func)
227292
{
228293
_serial.attach(func);
229294
}
295+
296+
bool ESP8266::recv_ap(nsapi_wifi_ap_t *ap)
297+
{
298+
int sec;
299+
bool ret = _parser.recv("+CWLAP:(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%d", &sec, ap->ssid,
300+
&ap->rssi, &ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4],
301+
&ap->bssid[5], &ap->channel);
302+
303+
ap->security = sec < 5 ? (nsapi_security_t)sec : NSAPI_SECURITY_UNKNOWN;
304+
305+
return ret;
306+
}

ESP8266/ESP8266.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,42 @@ class ESP8266
8181
*/
8282
const char *getMACAddress(void);
8383

84+
/** Get the local gateway
85+
*
86+
* @return Null-terminated representation of the local gateway
87+
* or null if no network mask has been recieved
88+
*/
89+
const char *getGateway();
90+
91+
/** Get the local network mask
92+
*
93+
* @return Null-terminated representation of the local network mask
94+
* or null if no network mask has been recieved
95+
*/
96+
const char *getNetmask();
97+
98+
/* Return RSSI for active connection
99+
*
100+
* @return Measured RSSI
101+
*/
102+
int8_t getRSSI();
103+
84104
/**
85105
* Check if ESP8266 is conenected
86106
*
87107
* @return true only if the chip has an IP address
88108
*/
89109
bool isConnected(void);
90110

111+
/** Scan for available networks
112+
*
113+
* @param ap Pointer to allocated array to store discovered AP
114+
* @param limit Size of allocated @a res array, or 0 to only count available AP
115+
* @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
116+
* see @a nsapi_error
117+
*/
118+
int scan(WiFiAccessPoint *res, unsigned limit);
119+
91120
/**
92121
* Open a socketed connection
93122
*
@@ -173,8 +202,11 @@ class ESP8266
173202
// data follows
174203
} *_packets, **_packets_end;
175204
void _packet_handler();
205+
bool recv_ap(nsapi_wifi_ap_t *ap);
176206

177207
char _ip_buffer[16];
208+
char _gateway_buffer[16];
209+
char _netmask_buffer[16];
178210
char _mac_buffer[18];
179211
};
180212

ESP8266Interface.cpp

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716

17+
#include <string.h>
1818
#include "ESP8266Interface.h"
1919

2020
// Various timeouts for different ESP8266 operations
@@ -23,7 +23,6 @@
2323
#define ESP8266_RECV_TIMEOUT 0
2424
#define ESP8266_MISC_TIMEOUT 500
2525

26-
2726
// ESP8266Interface implementation
2827
ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
2928
: _esp(tx, rx, debug)
@@ -34,10 +33,18 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
3433
_esp.attach(this, &ESP8266Interface::event);
3534
}
3635

37-
int ESP8266Interface::connect(
38-
const char *ssid,
39-
const char *pass,
40-
nsapi_security_t security)
36+
int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
37+
uint8_t channel)
38+
{
39+
if (channel != 0) {
40+
return NSAPI_ERROR_UNSUPPORTED;
41+
}
42+
43+
set_credentials(ssid, pass, security);
44+
return connect();
45+
}
46+
47+
int ESP8266Interface::connect()
4148
{
4249
_esp.setTimeout(ESP8266_CONNECT_TIMEOUT);
4350

@@ -49,17 +56,36 @@ int ESP8266Interface::connect(
4956
return NSAPI_ERROR_DHCP_FAILURE;
5057
}
5158

52-
if (!_esp.connect(ssid, pass)) {
59+
if (!_esp.connect(ap_ssid, ap_pass)) {
5360
return NSAPI_ERROR_NO_CONNECTION;
5461
}
5562

5663
if (!_esp.getIPAddress()) {
5764
return NSAPI_ERROR_DHCP_FAILURE;
5865
}
5966

67+
return NSAPI_ERROR_OK;
68+
}
69+
70+
int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
71+
{
72+
memset(ap_ssid, 0, sizeof(ap_ssid));
73+
strncpy(ap_ssid, ssid, sizeof(ap_ssid));
74+
75+
memset(ap_pass, 0, sizeof(ap_pass));
76+
strncpy(ap_pass, pass, sizeof(ap_pass));
77+
78+
ap_sec = security;
79+
6080
return 0;
6181
}
6282

83+
int ESP8266Interface::set_channel(uint8_t channel)
84+
{
85+
return NSAPI_ERROR_UNSUPPORTED;
86+
}
87+
88+
6389
int ESP8266Interface::disconnect()
6490
{
6591
_esp.setTimeout(ESP8266_MISC_TIMEOUT);
@@ -68,19 +94,39 @@ int ESP8266Interface::disconnect()
6894
return NSAPI_ERROR_DEVICE_ERROR;
6995
}
7096

71-
return 0;
97+
return NSAPI_ERROR_OK;
7298
}
7399

74-
const char* ESP8266Interface::get_ip_address()
100+
const char *ESP8266Interface::get_ip_address()
75101
{
76102
return _esp.getIPAddress();
77103
}
78104

79-
const char* ESP8266Interface::get_mac_address()
105+
const char *ESP8266Interface::get_mac_address()
80106
{
81107
return _esp.getMACAddress();
82108
}
83109

110+
const char *ESP8266Interface::get_gateway()
111+
{
112+
return _esp.getGateway();
113+
}
114+
115+
const char *ESP8266Interface::get_netmask()
116+
{
117+
return _esp.getNetmask();
118+
}
119+
120+
int8_t ESP8266Interface::get_rssi()
121+
{
122+
return _esp.getRSSI();
123+
}
124+
125+
int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count)
126+
{
127+
return _esp.scan(res, count);
128+
}
129+
84130
struct esp8266_socket {
85131
int id;
86132
nsapi_protocol_t proto;

0 commit comments

Comments
 (0)