Add code to query and set the current network (which access point/ssid to connect to).

pull/206/head
dannybackx 8 years ago
parent 5226d8b18f
commit 10e7816532
  1. 1
      cmd/cmd.h
  2. 41
      cmd/handlers.c
  3. 1
      esp-link/cgiservices.c
  4. 41
      esp-link/cgiwifi.c
  5. 2
      esp-link/cgiwifi.h
  6. 1
      esp-link/main.c
  7. 1
      esp-link/status.c

@ -65,6 +65,7 @@ typedef enum {
CMD_WIFI_GET_APNAME,
CMD_WIFI_SELECT_SSID,
CMD_WIFI_SIGNAL_STRENGTH, // Query RSSI
CMD_WIFI_GET_SSID, // Query SSID currently connected to
} CmdName;

@ -61,6 +61,7 @@ const CmdList commands[] = {
{CMD_WIFI_GET_APNAME, "WIFI_GET_APNAME", cmdWifiGetApName},
{CMD_WIFI_SELECT_SSID, "WIFI_SELECT_SSID", cmdWifiSelectSSID},
{CMD_WIFI_SIGNAL_STRENGTH, "WIFI_SIGNAL_STRENGTH", cmdWifiSignalStrength},
{CMD_WIFI_GET_SSID, "WIFI_GET_SSID", cmdWifiQuerySSID},
#ifdef MQTT
{CMD_MQTT_SETUP, "MQTT_SETUP", MQTTCMD_Setup},
@ -280,8 +281,48 @@ static void ICACHE_FLASH_ATTR cmdWifiGetApName(CmdPacket *cmd) {
/*
* Select a wireless network.
* This can be called in two ways :
* - with a pair of strings (SSID, password)
* - with a number and a string (index into network array, password)
*/
static void ICACHE_FLASH_ATTR cmdWifiSelectSSID(CmdPacket *cmd) {
CmdRequest req;
cmdRequest(&req, cmd);
int argc = cmdGetArgc(&req);
char *ssid, *pass;
if (argc != 2)
return;
int len = cmdArgLen(&req);
if (len == 1) {
// Assume this is the index
uint8_t ix;
cmdPopArg(&req, &ix, 1);
len = cmdArgLen(&req);
pass = (char *)os_malloc(len+2);
cmdPopArg(&req, pass, len);
pass[len] = 0;
os_printf("SelectSSID(%d,%s)", ix, pass);
char myssid[33];
wifiGetApName(ix, myssid);
myssid[32] = '\0';
connectToNetwork(myssid, pass);
} else {
ssid = os_malloc(len+2);
cmdPopArg(&req, ssid, len);
ssid[len] = 0;
len = cmdArgLen(&req);
pass = (char *)os_malloc(len+2);
cmdPopArg(&req, pass, len);
pass[len] = 0;
os_printf("SelectSSID(%s,%s)", ssid, pass);
connectToNetwork(ssid, pass);
}
}
/*

@ -1,4 +1,5 @@
#include <esp8266.h>
#include "cmd.h"
#include "cgiwifi.h"
#include "cgi.h"
#include "config.h"

@ -13,7 +13,9 @@ Cgi/template routines for the /wifi url.
* ----------------------------------------------------------------------------
*/
#include <esp8266.h>
#include "cmd.h"
#include "cgiwifi.h"
#include "cgi.h"
#include "status.h"
@ -34,7 +36,7 @@ bool mdns_started = false;
// ===== wifi status change callbacks
static WifiStateChangeCb wifi_state_change_cb[4];
// Temp store for new staion config
// Temp store for new station config
struct station_config stconf;
// Temp store for new ap config
@ -360,6 +362,18 @@ static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) {
os_timer_arm(&resetTimer, 4*RESET_TIMEOUT, 0);
}
// Kick off connection to some network
void ICACHE_FLASH_ATTR connectToNetwork(char *ssid, char *pass) {
os_strncpy((char*)stconf.ssid, ssid, 32);
os_strncpy((char*)stconf.password, pass, 64);
DBG("Wifi try to connect to AP %s pw %s\n", ssid, pass);
// Schedule disconnect/connect
os_timer_disarm(&reassTimer);
os_timer_setfn(&reassTimer, reassTimerCb, NULL);
os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it
}
// This cgi uses the routines above to connect to a specific access point with the
// given ESSID using the given password.
int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) {
@ -379,14 +393,8 @@ int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) {
if (el > 0 && pl >= 0) {
//Set to 0 if you want to disable the actual reconnecting bit
os_strncpy((char*)stconf.ssid, essid, 32);
os_strncpy((char*)stconf.password, passwd, 64);
DBG("Wifi try to connect to AP %s pw %s\n", essid, passwd);
//Schedule disconnect/connect
os_timer_disarm(&reassTimer);
os_timer_setfn(&reassTimer, reassTimerCb, NULL);
os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it
connectToNetwork(essid, passwd);
jsonHeader(connData, 200);
} else {
jsonHeader(connData, 400);
@ -982,3 +990,18 @@ ICACHE_FLASH_ATTR int wifiSignalStrength(int i) {
return rssi;
}
void ICACHE_FLASH_ATTR cmdWifiQuerySSID(CmdPacket *cmd) {
CmdRequest req;
cmdRequest(&req, cmd);
uint32_t callback = req.cmd->value;
struct station_config conf;
bool res = wifi_station_get_config(&conf);
os_printf("QuerySSID : %s\n", conf.ssid);
cmdResponseStart(CMD_RESP_CB, callback, 1);
cmdResponseBody(conf.ssid, strlen(conf.ssid)+1);
cmdResponseEnd();
}

@ -27,5 +27,7 @@ extern bool mdns_started;
int wifiGetApCount();
void wifiGetApName(int, char *);
int wifiSignalStrength(int);
void connectToNetwork(char *, char *);
void cmdWifiQuerySSID(CmdPacket *cmd);
#endif

@ -13,6 +13,7 @@
#include "httpd.h"
#include "httpdespfs.h"
#include "cgi.h"
#include "cmd.h"
#include "cgiwifi.h"
#include "cgipins.h"
#include "cgitcp.h"

@ -3,6 +3,7 @@
#include <esp8266.h>
#include "config.h"
#include "serled.h"
#include "cmd.h"
#include "cgiwifi.h"
#ifdef MQTT

Loading…
Cancel
Save