Skip to content

Commit

Permalink
Support ESP8266 core 2.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieromon committed Jan 20, 2021
1 parent eb31180 commit 974067c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
77 changes: 45 additions & 32 deletions ESPShaker/ESPShaker.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
* they can be executed with interactive commands.
* @file ESPShaker.ino
* @author [email protected]
* @version 1.3.3
* @date 2018-11-28
* @version 1.4.1
* @date 2020-01-20
* @copyright MIT license.
*/

#include <functional>
#include <stdio.h>
#include <ctype.h>
#include <core_version.h>
#include <core_esp8266_version.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
Expand All @@ -31,7 +31,7 @@ extern "C" {
extern "C" uint32_t _SPIFFS_start;
extern "C" uint32_t _SPIFFS_end;

#define _VERSION "1.4"
#define _VERSION "1.4.1"

class httpHandler : public RequestHandler {
public:
Expand Down Expand Up @@ -101,7 +101,7 @@ bool inSmartConfig = false;
int eepromAddress = 0;

DNSServer DnsServer;
ESP8266WebServer *WebServer = nullptr;
ESP8266WebServer *webServer = nullptr;
HTTPClient httpClient;
httpHandler* webHandler = nullptr;
WiFiClientSecure *wifiClientSec = nullptr;
Expand All @@ -117,6 +117,11 @@ String mqttTopic;

String coreVersion;

#if HAS_ESP8266_VERSION_NUMERIC
WiFiEventHandler stationConnectedHandler;
WiFiEventHandler stationDisconnectedHandler;
#endif

bool isNumber(String str) {
uint8_t varLength = (uint8_t)str.length();
if (varLength > 0) {
Expand Down Expand Up @@ -1073,6 +1078,20 @@ void smartConfig() {
Serial.print("> ");
}

#if HAS_ESP8266_VERSION_NUMERIC
void onStationConnected(const WiFiEventSoftAPModeStationConnected& e) {
Serial.println();
Serial.println("Station:<" + toMacAddress(e.mac) + "> connected");
Serial.print("> ");
}

void onStationDisconnected(const WiFiEventSoftAPModeStationDisconnected& e) {
Serial.println();
Serial.println("Station:<" + toMacAddress(e.mac) + "> disconnected");
Serial.print("> ");
}
#endif

void softAP() {
char* c_ssid = Cmd.next();
String ssid(c_ssid);
Expand Down Expand Up @@ -1102,17 +1121,11 @@ void softAP() {
yield();
delay(100);
}
#if defined(ARDUINO_ESP8266_RELEASE_2_4_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_1) || defined(ARDUINO_ESP8266_RELEASE_2_4_2)
// This callback is available only esp8266 core 2.4.x
WiFi.onSoftAPModeStationConnected([](const WiFiEventSoftAPModeStationConnected& e) {
Serial.println();
Serial.println("Station:<" + toMacAddress(e.mac) + "> connected");
Serial.print("> "); });
// This callback is available only esp8266 core 2.4.x
WiFi.onSoftAPModeStationDisconnected([](const WiFiEventSoftAPModeStationDisconnected& e) {
Serial.println();
Serial.println("Station:<" + toMacAddress(e.mac) + "> disconnected");
Serial.print("> "); });
#if HAS_ESP8266_VERSION_NUMERIC
if (esp8266::coreVersionNumeric() >= 20500000) {
stationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);
stationDisconnectedHandler = WiFi.onSoftAPModeStationDisconnected(&onStationDisconnected);
}
#endif
Serial.print("[info] softAPIP:");
Serial.println(WiFi.softAPIP());
Expand Down Expand Up @@ -1150,15 +1163,15 @@ void startServer() {
String sType = String(s_type);
sType.toLowerCase();
if (sType == "web") {
Serial.println("WebServer.begin");
if (WebServer == nullptr) {
WebServer = new ESP8266WebServer(80);
Serial.println("webServer.begin");
if (webServer == nullptr) {
webServer = new ESP8266WebServer(80);
}
if (webHandler == nullptr) {
webHandler = new httpHandler(WebServer);
WebServer->addHandler(webHandler);
webHandler = new httpHandler(webServer);
webServer->addHandler(webHandler);
}
WebServer->begin();
webServer->begin();
Serial.println("OK");
}
else if (sType == "dns") {
Expand Down Expand Up @@ -1244,10 +1257,10 @@ void stopServer() {
String type = String(c_type);
type.toLowerCase();
if (type == "web") {
if (WebServer != nullptr) {
if (webServer != nullptr) {
Serial.println("Stop web server");
delete WebServer;
WebServer = nullptr;
delete webServer;
webServer = nullptr;
webHandler = nullptr;
Serial.println("OK");
}
Expand Down Expand Up @@ -1363,10 +1376,10 @@ void http() {
}
else if (req == "on") {
if (webHandler == nullptr) {
if (WebServer == nullptr)
WebServer = new ESP8266WebServer(80);
webHandler = new httpHandler(WebServer);
WebServer->addHandler(webHandler);
if (webServer == nullptr)
webServer = new ESP8266WebServer(80);
webHandler = new httpHandler(webServer);
webServer->addHandler(webHandler);
}
String uri = String(Cmd.next());
Serial.print("ON ");
Expand All @@ -1391,7 +1404,7 @@ void http() {
page = new PageBuilder();
page->setUri(c_uriPool);
page->addElement(*content);
page->insert(*WebServer);
page->insert(*webServer);
Serial.print(' ');
Serial.println(c_contentPool);
}
Expand Down Expand Up @@ -1579,8 +1592,8 @@ void loop() {

if (onDnsserver)
DnsServer.processNextRequest();
if (WebServer != nullptr)
WebServer->handleClient();
if (webServer != nullptr)
webServer->handleClient();

if (mqttTopic.length())
_mqttIncomming();
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ ESP8266 UART RX buffer size is 128 bytes also ESPShaker has 128 bytes command bu

### Change log

#### [1.4.1] 2021-01-20
- Suppors ESP8266 Arduino core release 2.5.0 later

#### [1.4] 2019-09-03
- Suppors **erase** operand with **config** command.

Expand Down

0 comments on commit 974067c

Please sign in to comment.