Prvni verze s podporou ESP32

This commit is contained in:
Pavel Brychta 2018-11-20 16:40:15 +01:00
parent 33f8f6a8b8
commit 81ceb9aca9
6 changed files with 187 additions and 44 deletions

View File

@ -2,4 +2,4 @@
![Screenshot](doc/images/Screenshot_20180507-100906.png)
Knihovna pro konfiguraci WiFi rozhraní modulu ESP8266
Knihovna pro konfiguraci WiFi rozhraní modulu ESP8266 a ESP32, obsahující Captive portal.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

After

Width:  |  Height:  |  Size: 56 KiB

View File

@ -1,6 +1,6 @@
{
"name":"WiFiConfig",
"description":"Web based WiFi part configuration for the ESP8266 SoC",
"description":"Web based WiFi part configuration for the ESP8266 and ESP32 SoC",
"keywords":"wifi, configuration",
"authors":
{
@ -12,10 +12,10 @@
"type": "git",
"url": "http://git.xpablo.cz/pablo2048/WiFiConfig.git"
},
"version": "5.9",
"version": "6.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "espressif8266",
"platforms": ["espressif8266", "espressif32"],
"build": {
"libCompatMode": 2
}

View File

@ -1,9 +1,9 @@
name=WiFiConfig
version=5.9
version=6.0
author=Pavel Brychta
maintainer=Pavel Brychta <Pablo@xpablo.cz>
sentence=Enables seamless module configuration.
paragraph=With this library you can enable your ESP8266 module to be configured over WiFi. Includes Captive Portal for easy configuration.
paragraph=With this library you can enable your ESP8266 or ESP32 module to be configured over WiFi. Includes Captive Portal for easy configuration.
category=Other
url=http://www.xpablo.cz
architectures=esp8266
architectures=esp8266,esp32

View File

@ -1,4 +1,6 @@
/*
* V6.0 - 20.11.2018- Verze i pro ESP32
*
* V5.9 - 18.7.2018 - Retezce, ukladane do EEPROM jsou primitivnim zpusobem sifrovany pro ztizeni odhaleni SSID a hesla pri dumpu pameti
*
* V5.8 - 5.7.2018 - Opraveno spatne zobrazovani sily signalu v prehledu WiFi siti (spatne barvy)
@ -67,9 +69,13 @@
* Prejit na pripojovani pomoci vnitrnich mechanizmu SDK ESP - zvysi se tim rychlost pripojeni kvuli bateriovym zarizenim
*/
#include "WiFiConfig.h"
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#if defined(ESP8266)
#include <ESP8266WebServer.h>
#else
#include <WebServer.h>
#endif
#include "WiFiConfig.h"
#include <EEPROM.h>
#include <Ticker.h>
#include "embHTML.h"
@ -80,9 +86,15 @@
#define DEBUG_MSG(...)
#endif
extern "C" {
#if defined(ESP8266)
extern "C" {
#include "user_interface.h"
}
}
#define ESP_getChipId() (ESP.getChipId())
#else
#include <esp_wifi.h>
#define ESP_getChipId() ((uint32_t)ESP.getEfuseMac())
#endif
#define DNS_PORT 53
@ -103,8 +115,12 @@ enum
static int configBase; // musi byt trvale ulozene, aby fungovaly metody pro ziskani retezcu z EEPROM
static std::unique_ptr<DNSServer> dnsServer;
static std::unique_ptr<ESP8266WebServer> server;
static std::unique_ptr<DNSServer> dnsServer;
#if defined(ESP8266)
static std::unique_ptr<ESP8266WebServer> server;
#else
static std::unique_ptr<WebServer> server;
#endif
static std::unique_ptr<Ticker> rsttick;
const char VALUE[] PROGMEM = "value='";
@ -320,20 +336,32 @@ void WiFiConfig::_handleInfo(void)
reply.concat(F("<form><table><TR><TH>System Info<TH>"));
reply.concat(F("<TR><TD>Core Version:<TD>"));
reply.concat(ESP.getCoreVersion());
#if defined(ESP8266)
reply.concat(ESP.getFullVersion());
#else
// TODO
reply.concat(ESP.getSdkVersion());
#endif
reply.concat(F("<TR><TD>Flash Size:<TD>"));
#if defined(ESP8266)
reply.concat(ESP.getFlashChipRealSize() / 1024);
#else
// TODO
reply.concat(ESP.getFlashChipSize());
#endif
reply.concat(F(" kB"));
#if defined(ESP8266)
reply.concat(F("<TR><TD>Sketch Size/Free:<TD>"));
reply.concat(ESP.getSketchSize() / 1024);
reply.concat(F(" kB / "));
reply.concat(ESP.getFreeSketchSpace() / 1024);
reply.concat(F(" kB"));
#endif
reply.concat(F("<TR><TD>STA MAC:<TD>"));
uint8_t mac[] = {0, 0, 0, 0, 0, 0};
uint8_t mac[6];
uint8_t* macread = WiFi.macAddress(mac);
char macaddress[20];
sprintf_P(macaddress, PSTR("%02x:%02x:%02x:%02x:%02x:%02x"), macread[0], macread[1], macread[2], macread[3], macread[4], macread[5]);
@ -345,12 +373,26 @@ void WiFiConfig::_handleInfo(void)
reply.concat(macaddress);
reply.concat(F("<TR><TD>ESP Chip ID:<TD>"));
sprintf_P(buff, PSTR("%06X"), ESP.getChipId());
sprintf_P(buff, PSTR("%06X"), ESP_getChipId());
reply.concat(buff);
#if defined(ESP8266)
reply.concat(F("<TR><TD>Flash Chip ID:<TD>"));
sprintf_P(buff, PSTR("%08X"), ESP.getFlashChipId());
reply.concat(buff);
#endif
reply.concat(F("<TR><TD>Timeout:<TD>"));
reply.concat(_timeout);
reply.concat(F("/"));
reply.concat(_time);
#if defined(ESP8266)
uint8_t stations = wifi_softap_get_station_num();
reply.concat(F("<TR><TD>Clients:<TD>"));
reply.concat(stations);
#endif
reply.concat(F("</table></form>"));
server->send(200, FPSTR(TEXTHTML), reply);
@ -364,6 +406,7 @@ void WiFiConfig::_handleRoot(void)
content = FPSTR(PAGE_CAPTIVEPORTALCATCH); // 1. cast stranky
// pridame informaci o stavu pokusu o pripojeni
content.concat(F("<div class=\"vl-info\">Pokus o připojení: "));
#if defined(ESP8266)
switch (_status)
{
case STATION_IDLE:
@ -402,6 +445,9 @@ void WiFiConfig::_handleRoot(void)
content.concat(F("Neznámý"));
break;
}
#else
content.concat(F("Prozatím nedostupné"));
#endif
content.concat(F("</div>"));
content.concat(FPSTR(PAGE_CAPTIVEPORTALCATCH2)); // 2. cast stranky (ukonceni)
server->send(200, FPSTR(TEXTHTML), content);
@ -438,7 +484,11 @@ void WiFiConfig::_handleDisplayAP(void)
s = FPSTR(SSID_ITEM);
s.replace(F("{v}"), WiFi.SSID(i));
s.replace(F("{a}"), String(quality));
#if defined(ESP8266)
s.replace(F("{s}"), (ENC_TYPE_NONE == WiFi.encryptionType(i)) ? F("") : F("l"));
#else
s.replace(F("{s}"), (WIFI_AUTH_OPEN == WiFi.encryptionType(i)) ? F("") : F("l"));
#endif
content.concat(s);
}
}
@ -582,10 +632,12 @@ void WiFiConfig::_handleSetAP(void)
case WIFIMODE_STA:
{
DEBUG_MSG("STA mode.\r\n");
#if defined(ESP8266)
if (strlen(WiFiDeviceName))
{
WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni
}
#endif
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip)))
{
DEBUG_MSG("Static configuration.\r\n");
@ -595,8 +647,19 @@ void WiFiConfig::_handleSetAP(void)
}
WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta
WiFi.begin(s.c_str(), pass.c_str());
#if !defined(ESP8266)
if (strlen(WiFiDeviceName))
{
WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni
}
#endif
#if defined(ESP8266)
wifi_station_set_auto_connect(true);
delay(1000);
#else
//esp_wifi_set_auto_connect(true);
#endif
delay(1000);
}
break;
@ -636,7 +699,11 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
WiFi.persistent(false); // chranime flash pred zbytecnymi prepisy
dnsServer.reset(new DNSServer());
#if defined(ESP8266)
server.reset(new ESP8266WebServer(80));
#else
server.reset(new WebServer(80));
#endif
/* Soft AP network parameters */
IPAddress apIP(192, 168, 4, 1);
@ -654,7 +721,7 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
//WiFi.softAPmacAddress(amac);
//sprintf_P(lmac, PSTR("%02X%02X%02X"), amac[3], amac[4], amac[5]);
sprintf_P(lmac, PSTR("%06X"), ESP.getChipId());
sprintf_P(lmac, PSTR("%06X"), ESP_getChipId());
ssid.replace(F("?"), String(lmac));
}
WiFi.softAP(ssid.c_str(), NULL, SETUP_CHANNEL);
@ -698,14 +765,17 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
String WiFiConfig::_readString(unsigned int start, size_t maxlen)
{
uint8_t mac[WL_MAC_ADDR_LENGTH];
uint8_t key[4];
unsigned int enciphidx = 0;
String result;
unsigned int top = EEPROM.read(start);
unsigned int i;
result.reserve(maxlen);
WiFi.softAPmacAddress(mac);
uint32_t cid = ESP_getChipId();
uint32_t *pkey = (uint32_t *)&key[0];
*pkey = cid;
DEBUG_MSG("Key = %02X:%02X:%02X:%02X\r\n", key[0], key[1], key[2], key[3]);
if ((top > 0) && (top < (maxlen - 2)))
{
uint8_t b;
@ -713,22 +783,25 @@ String WiFiConfig::_readString(unsigned int start, size_t maxlen)
{
b = EEPROM.read(i);
b ^= mac[enciphidx];
b ^= key[enciphidx];
++enciphidx;
enciphidx %= WL_MAC_ADDR_LENGTH;
enciphidx %= sizeof(key);
result.concat(char(b));
}
b = EEPROM.read(i) ^ mac[enciphidx];
b = EEPROM.read(i) ^ key[enciphidx];
if (b != 0)
{
DEBUG_MSG("_readString error! (%s)\r\n", result.c_str());
result = F(""); // spatna ukoncovaci nula - neplatny retezec
}
}
return result;
}
bool WiFiConfig::_storeString(unsigned int start, size_t maxlen, String &string)
{
uint8_t mac[WL_MAC_ADDR_LENGTH];
uint8_t key[4];
unsigned int enciphidx = 0;
unsigned int si = 0;
unsigned int top;
@ -744,18 +817,21 @@ bool WiFiConfig::_storeString(unsigned int start, size_t maxlen, String &string)
top = string.length();
}
WiFi.softAPmacAddress(mac);
uint32_t cid = ESP_getChipId();
uint32_t *pkey = (uint32_t *)&key[0];
*pkey = cid;
DEBUG_MSG("Key = %02X:%02X:%02X:%02X\r\n", key[0], key[1], key[2], key[3]);
EEPROM.write(start, (uint8_t)top); // ulozime delku retezce (pouzite pr kontrolu pri vycitani)
for (i = start + 1; i < start + top + 1; ++i)
{
c = string[si];
c ^= mac[enciphidx];
c ^= key[enciphidx];
++enciphidx;
enciphidx %= WL_MAC_ADDR_LENGTH;
enciphidx %= sizeof(key);
EEPROM.write(i, c);
++si;
}
c = 0 ^ mac[enciphidx];
c = 0 ^ key[enciphidx];
EEPROM.write(i, c); // ukoncovaci a kontrolni nula
return result;
@ -766,6 +842,63 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
{
uint32_t startt = millis();
#if !defined(ESP8266)
// nakonfigurujeme ESP dle nove nastavenych parametru
WiFi.disconnect(); // vsechno odpojime
String s = _readString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
strcpy(WiFiDeviceName, s.c_str());
s = _readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
String pass = _readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)))
{
case WIFIMODE_STA:
{
DEBUG_MSG("STA mode.\r\n");
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip)))
{
DEBUG_MSG("Static configuration.\r\n");
WiFi.config(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))),
IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns))));
delay(100);
}
WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta
WiFi.begin(s.c_str(), pass.c_str());
DEBUG_MSG("STA params: %s, %s\r\n", s.c_str(), pass.c_str());
if (strlen(WiFiDeviceName))
{
WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni
}
}
break;
case WIFIMODE_AP:
DEBUG_MSG("AP mode.\r\n");
WiFi.mode(WIFI_AP); // startujeme AP
if (pass.length())
{
// je zadane heslo do AP
WiFi.softAP(s.c_str(), pass.c_str(), EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)));
}
else
{
// otevreny AP
WiFi.softAP(s.c_str(), NULL, EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)));
}
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip)))
{
WiFi.softAPConfig(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))),
IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))));
}
break;
default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???)
DEBUG_MSG("Mode Error!!\r\n");
break;
}
#endif
DEBUG_MSG("Trying to connect.\r\n");
while ((millis() - startt) < WIFI_STA_CONNECT_TIMEOUT)
{
@ -781,11 +914,15 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
cb(WCS_CONNECTING); // signalizujeme pokracujici pokus o spojeni
}
DEBUG_MSG("Not connected!\r\n");
#if defined(ESP8266)
_status = wifi_station_get_connect_status();
#else
_status = WiFi.status();
#endif
return false; // pripojeni se nezdarilo
}
wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, wificonfig_cb cb)
wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificonfig_cb cb)
{
wificonfigresult_t result = WCR_OK; // predpokladame, ze se pripojeni podari
@ -800,13 +937,15 @@ wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, wif
}
else
{
#if defined(ESP8266)
if (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)) != WiFi.getMode())
{ // neshoduje se rezim - musime spustit konfiguracni AP (poskozena konfigurace)
DEBUG_MSG("Wrong config\r\n");
DEBUG_MSG("Wrong config (%d, %d)\r\n", EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)), WiFi.getMode());
_status = 0xfe;
result = _setupAP(cb);
}
else
#endif
{
String s = _readString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
strcpy(WiFiDeviceName, s.c_str());
@ -815,7 +954,11 @@ wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, wif
{
case WIFIMODE_STA:
{
#if defined(ESP8266)
WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni
#else
WiFi.setHostname(WiFiDeviceName);
#endif
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip)))
{
DEBUG_MSG("Static configuration SET.\r\n");
@ -848,18 +991,14 @@ wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, wif
return result; // mame vyreseno
}
wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, int timeout, wificonfig_cb cb = NULL)
wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb = NULL)
{
if (forceConfigure)
{ // pouze pokud nemame vynucenou konfiguraci merime cas - to nam umozni dostat se ze spatne zadaneho timeoutu, ktery se neda zvladnout
if ((timeout > 0) && (40 > timeout))
timeout = 40; // timeout musi byt minimalne 40 sekund
_timeout = timeout;
if (_timeout > 0)
_time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat
}
else
_timeout = 0; // pri vynucene konfiguraci se parametr timeout neuplatni
if ((timeout > 0) && (40 > timeout))
timeout = 40; // timeout musi byt minimalne 40 sekund
_timeout = timeout;
if (_timeout > 0)
_time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat
return begin(configarea, forceConfigure, cb); // spustime WiFi
}
// EOF

View File

@ -30,7 +30,11 @@
#ifndef __WiFiConfig_h__
#define __WiFiConfig_h__
#include <ESP8266WiFi.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <memory>
#define elementSize(type, element) sizeof(((type *)0)->element)
@ -169,8 +173,8 @@ class WiFiConfig
{
public:
WiFiConfig(): _params(NULL), _timeout(0) {}
wificonfigresult_t begin(int configarea, uint8_t forceConfigure, wificonfig_cb cb);
wificonfigresult_t begin(int configarea, uint8_t forceConfigure, int timeout, wificonfig_cb cb);
wificonfigresult_t begin(int configarea, int forceConfigure, wificonfig_cb cb);
wificonfigresult_t begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb);
void addParameter(WiFiConfigUsrParameter *p);
private:
wificonfigresult_t _setupAP(wificonfig_cb cb);