Skip to content
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

The stuff to have more access to IP/wifi config from the Arduino. #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions ELClient/ELClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ typedef enum {
CMD_CB_ADD, /**< Add a custom callback */
CMD_CB_EVENTS, /**< ??? */
CMD_GET_TIME, /**< Get current time in seconds since the unix epoch */
CMD_GET_WIFI_INFO, /**< Get several bits of IP address info */
CMD_SET_WIFI_INFO, /**< Set several bits of IP address info */
//CMD_GET_INFO,

CMD_MQTT_SETUP = 10, /**< Register callback functions */
CMD_MQTT_PUBLISH, /**< Publish MQTT topic */
CMD_MQTT_SUBSCRIBE, /**< Subscribe to MQTT topic */
CMD_MQTT_LWT, /**< Define MQTT last will */
CMD_MQTT_GET_CLIENTID,

CMD_REST_SETUP = 20, /**< Setup REST connection */
CMD_REST_REQUEST, /**< Make request to REST server */
Expand All @@ -40,6 +43,14 @@ typedef enum {

CMD_SOCKET_SETUP = 40, /**< Setup socket connection */
CMD_SOCKET_SEND, /**< Send socket packet */

CMD_WIFI_GET_APCOUNT = 50, // Query number of access pointer
CMD_WIFI_GET_APNAME, // Get the name for an access point
CMD_WIFI_SELECT_SSID, // Connect to this network
CMD_WIFI_SIGNAL_STRENGTH, // Query RSSI
CMD_WIFI_GET_SSID, // Query SSID currently connected to
CMD_WIFI_START_SCAN, // Trigger a scan (takes a long time)

} CmdName; /**< Enumeration of commands supported by esp-link, this needs to match the definition in esp-link! */

enum WIFI_STATUS {
Expand Down
205 changes: 205 additions & 0 deletions ELClient/ELClientCmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,208 @@ uint32_t ELClientCmd::GetTime() {
return pkt ? pkt->value : 0;
}

/*! GetWifiInfo()
@brief Get IP address info from ESP
@details ip address, network mask, gateway ip
@return Three parameters allow returning the values looked up, specify pointer to <code>uint32_t</code> in them.
@par Example
@code
uint32_t ip, nm, gw;
cmd.GetWifiInfo(&ip, &nm, &gw);
@endcode
*/
void ELClientCmd::GetWifiInfo(uint32_t *ptr_ip, uint32_t *ptr_netmask, uint32_t *ptr_gateway) {
clientCmdCb.attach(this, &ELClientCmd::wifiInfoCmdCallback);
_elc->Request(CMD_GET_WIFI_INFO, (uint32_t)&clientCmdCb, 0);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}
if (ptr_ip)
*ptr_ip = ip;
if (ptr_netmask)
*ptr_netmask = netmask;
if (ptr_gateway)
*ptr_gateway = gateway;
}

/*! SetWifiInfo()
@brief Set IP address
@details ip address, network mask, gateway ip
@return
@par Example
@code
uint32_t ip, nm, gw;
cmd.SetWifiInfo(ip, nm, gw);
@endcode
*/
void ELClientCmd::SetWifiInfo(uint32_t ptr_ip, uint32_t ptr_netmask, uint32_t ptr_gateway) {
_elc->Request(CMD_SET_WIFI_INFO, 0, 0);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
// return pkt ? pkt->value : 0;
}


/*! wifiInfoCmdCallback()
@brief Helper function to decode the three bits of information from the packet
@details See GetWifiInfo()
@return none
*/
void ELClientCmd::wifiInfoCmdCallback(void *res) {
ELClientResponse *resp = (ELClientResponse *)res;

resp->popArg(&ip, sizeof(ip));
if (_elc->_debugEn) {
_elc->_debug->print("IP ");
_elc->_debug->println(ip);
}

resp->popArg(&netmask, sizeof(netmask));
if (_elc->_debugEn) {
_elc->_debug->print("NM ");
_elc->_debug->println(netmask);
}

resp->popArg(&gateway, sizeof(gateway));
if (_elc->_debugEn) {
_elc->_debug->print("GW ");
_elc->_debug->println(gateway);
}

resp->popArg(&mac, sizeof(mac));
}

/*
* FIXME this depends on having called getWifiInfo
*/
char *ELClientCmd::getMac() {
return (char *)mac;
}

/*
* Query the number of Access Points scanned.
* FIXME this relies on having triggered such a scan
*/
uint32_t ELClientCmd::GetWifiApCount() {
_elc->Request(CMD_WIFI_GET_APCOUNT, 0, 0);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
return pkt ? pkt->value : -1;
}

/*
* Query the SSID of a network. Range and FIXME as with the ApCount.
*/
char * ELClientCmd::GetWifiApName(int i) {
uint16_t ix = i;

clientCmdCb.attach(this, &ELClientCmd::wifiGetApNameCallback);
_elc->Request(CMD_WIFI_GET_APNAME, (uint32_t)&clientCmdCb, 1);
_elc->Request(&ix, 2);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}

return ssid;
}

void ELClientCmd::wifiGetApNameCallback(void *res) {
ELClientResponse *resp = (ELClientResponse *)res;

if (ssid == 0) ssid = (char *)malloc(33);
resp->popArg(ssid, 33);
ssid[32] = '\0';
}

/*
* Query the MQTT clientid
*/
void ELClientCmd:: mqttGetClientIdCallback(void *res) {
ELClientResponse *resp = (ELClientResponse *)res;

if (mqtt_clientid == 0) mqtt_clientid = (char *)malloc(33);
resp->popArg(mqtt_clientid, 32);
mqtt_clientid[32] = '\0';
}

char *ELClientCmd::mqttGetClientId() {
mqttCmdCb.attach(this, &ELClientCmd::mqttGetClientIdCallback);
_elc->Request(CMD_MQTT_GET_CLIENTID, (uint32_t)&mqttCmdCb, 0);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}

return mqtt_clientid;
}

/*
* Query RSSI (signal strength)
*/
int ELClientCmd::GetRSSI(int i) {
char x = i;
_elc->Request(CMD_WIFI_SIGNAL_STRENGTH, 0, 1);
_elc->Request(&x, 1);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
return pkt ? pkt->value : 0;
}

void ELClientCmd::SelectSSID(char *ssid, char *pass) {
_elc->Request(CMD_WIFI_SELECT_SSID, 0, 2);
_elc->Request(ssid, strlen(ssid));
_elc->Request(pass, strlen(pass));
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}
}

void ELClientCmd::SelectSSID(int xssid, char *pass) {
unsigned char x = xssid;
_elc->Request(CMD_WIFI_SELECT_SSID, 0, 2);
_elc->Request(&x, 1);
_elc->Request(pass, strlen(pass));
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}
}

char *ELClientCmd::GetSSID() {
clientCmdCb.attach(this, &ELClientCmd::wifiGetApNameCallback);
_elc->Request(CMD_WIFI_GET_SSID, (uint32_t)&clientCmdCb, 0);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}

return ssid;
}

void ELClientCmd::StartScan() {
_elc->Request(CMD_WIFI_START_SCAN, 0, 0);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}
}
22 changes: 22 additions & 0 deletions ELClient/ELClientCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,30 @@ class ELClientCmd {
ELClientCmd(ELClient* elc);
// Get the current time in seconds since the epoch, 0 if the time is unknown
uint32_t GetTime();
void GetWifiInfo(uint32_t *, uint32_t *, uint32_t *);
void SetWifiInfo(uint32_t, uint32_t, uint32_t);
uint32_t GetWifiApCount();
char * GetWifiApName(int);
char *getMac();
char *mqttGetClientId();
int GetRSSI(int); // Current signal strength if <0, or selected network's rssi
void SelectSSID(char *, char *);
void SelectSSID(int, char *);
char *GetSSID();
void StartScan();

private:
ELClient* _elc; /**< ELClient instance */
FP<void, void*> clientCmdCb; /**< Pointer to external callback function */
void wifiInfoCmdCallback(void *resp);
uint32_t ip, netmask, gateway;
uint8_t mac[6];

char *ssid;
void wifiGetApNameCallback(void *);

FP<void, void*> mqttCmdCb; /**< Pointer to external callback function */
void mqttGetClientIdCallback(void *);
char *mqtt_clientid;
};
#endif
82 changes: 82 additions & 0 deletions ELClient/examples/get-info/get-info.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <Arduino.h>
#include <ELClient.h>
#include <ELClientCmd.h>

// Forward declarations
void wifiCb(void *response);

// Initialize a connection to esp-link using the normal hardware serial port both for
// SLIP and for debug messages.
ELClient esp(&Serial, &Serial);
ELClientCmd cmd(&esp);

boolean wifiConnected = false;

#define IP(x, y) ((int)((x >> (8*y)) & 0xFF))
static char buffer[80];

void setup() {
Serial.begin(9600); // Match the esplink config (Arduino serial <-> ESP)
delay(3000);
Serial.println("ELClient test : show IP information");

esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired)
bool ok;
do {
ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds
if (!ok) Serial.println("EL-Client sync failed!");
} while(!ok);
Serial.println("EL-Client synced!");

uint32_t ip, nm, gw;
cmd.GetWifiInfo(&ip, &nm, &gw);

sprintf(buffer, "IP Address %d.%d.%d.%d\n", IP(ip, 0), IP(ip, 1), IP(ip, 2), IP(ip, 3));
Serial.print(buffer);
sprintf(buffer, "Network mask %d.%d.%d.%d\n", IP(nm, 0), IP(nm, 1), IP(nm, 2), IP(nm, 3));
Serial.print(buffer);
sprintf(buffer, "IP Gateway %d.%d.%d.%d\n", IP(gw, 0), IP(gw, 1), IP(gw, 2), IP(gw, 3));
Serial.print(buffer);

char *mac = cmd.getMac();
Serial.print("MAC Address : ");
for (int i=0; i<6; i++) {
char buf[4];
if (i < 5)
sprintf(buf, "%02X:", 0xFF & mac[i]);
else
sprintf(buf, "%02X", 0xFF & mac[i]);
Serial.print(buf);
}
Serial.println("");

// Query the MQTT clientid
Serial.print("MQTT client id : ");
char *mqtt_clientid = cmd.mqttGetClientId();
Serial.println(mqtt_clientid);

}

void loop() {
delay(100);
}

// Callback made from esp-link to notify of wifi status changes
// Here we print something out and set a global flag
void wifiCb(void *response) {
ELClientResponse *res = (ELClientResponse*)response;
if (res->argc() == 1) {
uint8_t status;
res->popArg(&status, 1);

if(status == STATION_GOT_IP) {
Serial.println("WIFI CONNECTED");
wifiConnected = true;
} else {
Serial.print("WIFI NOT READY: ");
Serial.println(status);
wifiConnected = false;
}
}
}

Loading