Skip to content

Port esp2866 driver to new WiFiInterface API #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 79 additions & 2 deletions ESP8266/ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool ESP8266::disconnect(void)
const char *ESP8266::getIPAddress(void)
{
if (!(_parser.send("AT+CIFSR")
&& _parser.recv("+CIFSR:STAIP,\"%[^\"]\"", _ip_buffer)
&& _parser.recv("+CIFSR:STAIP,\"%15[^\"]\"", _ip_buffer)
Copy link
Contributor

@geky geky Sep 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this 👍

&& _parser.recv("OK"))) {
return 0;
}
Expand All @@ -90,19 +90,84 @@ const char *ESP8266::getIPAddress(void)
const char *ESP8266::getMACAddress(void)
{
if (!(_parser.send("AT+CIFSR")
&& _parser.recv("+CIFSR:STAMAC,\"%[^\"]\"", _mac_buffer)
&& _parser.recv("+CIFSR:STAMAC,\"%17[^\"]\"", _mac_buffer)
&& _parser.recv("OK"))) {
return 0;
}

return _mac_buffer;
}

const char *ESP8266::getGateway()
{
if (!(_parser.send("AT+CIPSTA?")
&& _parser.recv("+CIPSTA:gateway:\"%15[^\"]\"", _gateway_buffer)
&& _parser.recv("OK"))) {
return 0;
}

return _gateway_buffer;
}

const char *ESP8266::getNetmask()
{
if (!(_parser.send("AT+CIPSTA?")
&& _parser.recv("+CIPSTA:netmask:\"%15[^\"]\"", _netmask_buffer)
&& _parser.recv("OK"))) {
return 0;
}

return _netmask_buffer;
}

int8_t ESP8266::getRSSI()
{
int8_t rssi;
char bssid[18];

if (!(_parser.send("AT+CWJAP?")
&& _parser.recv("+CWJAP:\"%*[^\"]\",\"%17[^\"]\"", bssid)
&& _parser.recv("OK"))) {
return 0;
}

if (!(_parser.send("AT+CWLAP=\"\",\"%s\",", bssid)
&& _parser.recv("+CWLAP:(%*d,\"%*[^\"]\",%hhd,", &rssi)
&& _parser.recv("OK"))) {
return 0;
}

return rssi;
}

bool ESP8266::isConnected(void)
{
return getIPAddress() != 0;
}

int ESP8266::scan(WiFiAccessPoint *res, unsigned limit)
{
unsigned cnt = 0;
nsapi_wifi_ap_t ap;

if (!_parser.send("AT+CWLAP")) {
return NSAPI_ERROR_DEVICE_ERROR;
}

while (recv_ap(&ap)) {
if (cnt < limit) {
res[cnt] = WiFiAccessPoint(ap);
}

cnt++;
if (limit != 0 && cnt >= limit) {
break;
}
}

return cnt;
}

bool ESP8266::open(const char *type, int id, const char* addr, int port)
{
//IDs only 0-4
Expand Down Expand Up @@ -227,3 +292,15 @@ void ESP8266::attach(Callback<void()> func)
{
_serial.attach(func);
}

bool ESP8266::recv_ap(nsapi_wifi_ap_t *ap)
{
int sec;
bool ret = _parser.recv("+CWLAP:(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%d", &sec, ap->ssid,
&ap->rssi, &ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4],
&ap->bssid[5], &ap->channel);

ap->security = sec < 5 ? (nsapi_security_t)sec : NSAPI_SECURITY_UNKNOWN;

return ret;
}
32 changes: 32 additions & 0 deletions ESP8266/ESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,42 @@ class ESP8266
*/
const char *getMACAddress(void);

/** Get the local gateway
*
* @return Null-terminated representation of the local gateway
* or null if no network mask has been recieved
*/
const char *getGateway();

/** Get the local network mask
*
* @return Null-terminated representation of the local network mask
* or null if no network mask has been recieved
*/
const char *getNetmask();

/* Return RSSI for active connection
*
* @return Measured RSSI
*/
int8_t getRSSI();

/**
* Check if ESP8266 is conenected
*
* @return true only if the chip has an IP address
*/
bool isConnected(void);

/** Scan for available networks
*
* @param ap Pointer to allocated array to store discovered AP
* @param limit Size of allocated @a res array, or 0 to only count available AP
* @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
* see @a nsapi_error
*/
int scan(WiFiAccessPoint *res, unsigned limit);

/**
* Open a socketed connection
*
Expand Down Expand Up @@ -173,8 +202,11 @@ class ESP8266
// data follows
} *_packets, **_packets_end;
void _packet_handler();
bool recv_ap(nsapi_wifi_ap_t *ap);

char _ip_buffer[16];
char _gateway_buffer[16];
char _netmask_buffer[16];
char _mac_buffer[18];
};

Expand Down
66 changes: 56 additions & 10 deletions ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#include <string.h>
#include "ESP8266Interface.h"

// Various timeouts for different ESP8266 operations
Expand All @@ -23,7 +23,6 @@
#define ESP8266_RECV_TIMEOUT 0
#define ESP8266_MISC_TIMEOUT 500


// ESP8266Interface implementation
ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
: _esp(tx, rx, debug)
Expand All @@ -34,10 +33,18 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
_esp.attach(this, &ESP8266Interface::event);
}

int ESP8266Interface::connect(
const char *ssid,
const char *pass,
nsapi_security_t security)
int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
uint8_t channel)
{
if (channel != 0) {
return NSAPI_ERROR_UNSUPPORTED;
}

set_credentials(ssid, pass, security);
return connect();
}

int ESP8266Interface::connect()
{
_esp.setTimeout(ESP8266_CONNECT_TIMEOUT);

Expand All @@ -49,17 +56,36 @@ int ESP8266Interface::connect(
return NSAPI_ERROR_DHCP_FAILURE;
}

if (!_esp.connect(ssid, pass)) {
if (!_esp.connect(ap_ssid, ap_pass)) {
return NSAPI_ERROR_NO_CONNECTION;
}

if (!_esp.getIPAddress()) {
return NSAPI_ERROR_DHCP_FAILURE;
}

return NSAPI_ERROR_OK;
}

int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
{
memset(ap_ssid, 0, sizeof(ap_ssid));
strncpy(ap_ssid, ssid, sizeof(ap_ssid));

memset(ap_pass, 0, sizeof(ap_pass));
strncpy(ap_pass, pass, sizeof(ap_pass));

ap_sec = security;

return 0;
}

int ESP8266Interface::set_channel(uint8_t channel)
{
return NSAPI_ERROR_UNSUPPORTED;
}


int ESP8266Interface::disconnect()
{
_esp.setTimeout(ESP8266_MISC_TIMEOUT);
Expand All @@ -68,19 +94,39 @@ int ESP8266Interface::disconnect()
return NSAPI_ERROR_DEVICE_ERROR;
}

return 0;
return NSAPI_ERROR_OK;
}

const char* ESP8266Interface::get_ip_address()
const char *ESP8266Interface::get_ip_address()
{
return _esp.getIPAddress();
}

const char* ESP8266Interface::get_mac_address()
const char *ESP8266Interface::get_mac_address()
{
return _esp.getMACAddress();
}

const char *ESP8266Interface::get_gateway()
{
return _esp.getGateway();
}

const char *ESP8266Interface::get_netmask()
{
return _esp.getNetmask();
}

int8_t ESP8266Interface::get_rssi()
{
return _esp.getRSSI();
}

int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count)
{
return _esp.scan(res, count);
}

struct esp8266_socket {
int id;
nsapi_protocol_t proto;
Expand Down
Loading