Browse Source

wifi: refactoring / rewrite

- replace JustWifi with a custom WiFi module
- re-implemented Core methods that deal with scanning, directly work with the SDK callback
- re-implemented Core method for opmode to avoid dealing with the 2.7.x pseudo modes
- re-implemented Core method for forced-(modem)-sleep (with the intention to merge with OpmodeNull... todo)
- disable reconnect and autoconnect from SDK, avoid unintentionally storing ssid & pass for these and
  make connection routine watch for events instead. timeout is implemented as a local timer
- do not store runtime data that can be retrieved via SDK calls
- settings are loaded only when starting AP or STA, networks are no longer kept in memory
- settings STA ssidN must be unique
- remove char buffer from the event callback
- trigger station-disconnected event when actually disconnected, not when connecting or explicitly requesting it
- generic rssi sorting method for initial connection, do rssi checks while connected to find a better network

Drop (temporarily?) wps and smartconfig. Both are implementable with the new approach,
but come with some weird expectations from the SDK side (no extra heap with wps, broadcasting plain-text
passphrase with smartconfig, storing station config implicitly, etc.).

Both are sort-of fixed with RTOS SDK, but it is still a big question of whether to support them at all in the current state.
dev
Maxim Prokhorov 3 years ago
parent
commit
5a97329832
15 changed files with 2471 additions and 788 deletions
  1. +3
    -2
      code/espurna/alexa.cpp
  2. +1
    -11
      code/espurna/button.cpp
  3. +50
    -19
      code/espurna/config/general.h
  4. +0
    -10
      code/espurna/config/types.h
  5. +76
    -79
      code/espurna/led.cpp
  6. +3
    -3
      code/espurna/led.h
  7. +12
    -6
      code/espurna/mdns.cpp
  8. +3
    -3
      code/espurna/mqtt.cpp
  9. +2
    -5
      code/espurna/telnet.cpp
  10. +10
    -7
      code/espurna/tuya.cpp
  11. +2157
    -579
      code/espurna/wifi.cpp
  12. +47
    -17
      code/espurna/wifi.h
  13. +96
    -8
      code/espurna/wifi_config.h
  14. +11
    -8
      code/espurna/ws.cpp
  15. +0
    -31
      code/platformio.ini

+ 3
- 2
code/espurna/alexa.cpp View File

@ -208,8 +208,9 @@ void alexaSetup() {
#endif
// Register wifi callback
wifiRegister([](justwifi_messages_t code, char * parameter) {
if ((MESSAGE_CONNECTED == code) || (MESSAGE_DISCONNECTED == code)) {
wifiRegister([](wifi::Event event) {
if ((event == wifi::Event::StationConnected)
|| (event == wifi::Event::StationDisconnected)) {
_alexaConfigure();
}
});


+ 1
- 11
code/espurna/button.cpp View File

@ -453,11 +453,7 @@ void buttonEvent(size_t id, ButtonEvent event) {
#endif
case ButtonAction::AccessPoint:
if (wifiState() & WIFI_STATE_AP) {
wifiStartSTA();
} else {
wifiStartAP();
}
wifiToggleAp();
break;
case ButtonAction::Reset:
@ -469,15 +465,9 @@ void buttonEvent(size_t id, ButtonEvent event) {
break;
case ButtonAction::Wps:
#if defined(JUSTWIFI_ENABLE_WPS)
wifiStartWPS();
#endif
break;
case ButtonAction::SmartConfig:
#if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
wifiStartSmartConfig();
#endif
break;
case ButtonAction::BrightnessIncrease:


+ 50
- 19
code/espurna/config/general.h View File

@ -536,26 +536,46 @@
// WIFI
// -----------------------------------------------------------------------------
#ifndef WIFI_CONNECT_TIMEOUT
#define WIFI_CONNECT_TIMEOUT 60000 // Connecting timeout for WIFI in ms
#ifndef WIFI_CONNECT_RETRIES
#define WIFI_CONNECT_RETRIES 3 // Number of times before changing to the next configured network
#endif
#ifndef WIFI_CONNECT_INTERVAL
#define WIFI_CONNECT_INTERVAL 3000 // Time (ms) between connection attempts
#endif
#ifndef WIFI_RECONNECT_INTERVAL
#define WIFI_RECONNECT_INTERVAL 180000 // If could not connect to WIFI, retry after this time in ms
#define WIFI_RECONNECT_INTERVAL 120000 // When all retries on all networks are exhausted, wait for this time (ms) and start from the beginning
#endif
#ifndef WIFI_MAX_NETWORKS
#define WIFI_MAX_NETWORKS 5 // Max number of WIFI connection configurations
#define WIFI_MAX_NETWORKS 5 // Maximum number of WiFi configurations in settings
#endif
#ifndef WIFI_AP_CAPTIVE_SUPPORT
#define WIFI_AP_CAPTIVE_SUPPORT 1 // Captive portal for AP mode
#endif
#ifndef WIFI_AP_CAPTIVE_ENABLED
#define WIFI_AP_CAPTIVE_ENABLED 1 // Enabled by default
#endif
#ifndef WIFI_AP_CAPTIVE
#define WIFI_AP_CAPTIVE 1 // Captive portal enabled when in AP mode
#ifndef WIFI_STA_MODE
#define WIFI_STA_MODE wifi::StaMode::Enabled // By default, turn on STA interface and try to connect to configured networks
// - wifi::StaMode::Enabled (default)
// - wifi::StaMode::Disabled keeps STA disabled
#endif
#ifndef WIFI_AP_MODE
#define WIFI_AP_MODE WiFiApMode::Fallback // By default, fallback to AP mode if no STA connection
// Use WiFiApMode::Enabled to start it when the device boots
// Use WiFiApMode::Disabled to disable AP mode completely
#define WIFI_AP_MODE wifi::ApMode::Fallback // By default, enable AP if there is no STA connection
// - wifi::ApMode::Fallback (default)
// - wifi::ApMode::Enabled keeps AP enabled independent of STA
// - wifi::ApMode::Disabled keeps AP disabled
#endif
#ifndef WIFI_FALLBACK_TIMEOUT
#define WIFI_FALLBACK_TIMEOUT 60000 // When AP is in FALLBACK mode and STA is connected,
// how long to wait until stopping the AP
#endif
#ifndef WIFI_AP_SSID
@ -573,15 +593,34 @@
// Use `set wifiApLease# MAC`, where MAC is a valid 12-byte HEX number without colons
#endif
#ifndef WIFI_AP_CHANNEL
#define WIFI_AP_CHANNEL 1
#endif
#ifndef WIFI_SLEEP_MODE
#define WIFI_SLEEP_MODE WIFI_NONE_SLEEP // WIFI_NONE_SLEEP, WIFI_LIGHT_SLEEP or WIFI_MODEM_SLEEP
#endif
#ifndef WIFI_SCAN_NETWORKS
#define WIFI_SCAN_NETWORKS 1 // Perform a network scan before connecting
#define WIFI_SCAN_NETWORKS 1 // Perform a network scan before connecting and when RSSI threshold is reached
#endif
// Optional hardcoded configuration (up to 5 networks, depending on WIFI_MAX_NETWORKS and espurna/wifi_config.h)
#ifndef WIFI_SCAN_RSSI_THRESHOLD
#define WIFI_SCAN_RSSI_THRESHOLD -73 // Consider current network for a reconnection cycle
// when it's RSSI value is below the specified threshold
#endif
#ifndef WIFI_SCAN_RSSI_CHECKS
#define WIFI_SCAN_RSSI_CHECKS 3 // Amount of RSSI threshold checks before starting a scan
#endif
#ifndef WIFI_SCAN_RSSI_CHECK_INTERVAL
#define WIFI_SCAN_RSSI_CHECK_INTERVAL 60000 // Time (ms) between RSSI checks
#endif
// Optional hardcoded configuration
// NOTICE that these values become factory-defaults
#ifndef WIFI1_SSID
#define WIFI1_SSID ""
#endif
@ -702,14 +741,6 @@
#define WIFI5_DNS ""
#endif
#ifndef WIFI_RSSI_1M
#define WIFI_RSSI_1M -30 // Calibrate it with your router reading the RSSI at 1m
#endif
#ifndef WIFI_PROPAGATION_CONST
#define WIFI_PROPAGATION_CONST 4 // This is typically something between 2.7 to 4.3 (free space is 2)
#endif
// ref: https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/kconfig.html#config-lwip-esp-gratuitous-arp
// ref: https://github.com/xoseperez/espurna/pull/1877#issuecomment-525612546
//


+ 0
- 10
code/espurna/config/types.h View File

@ -5,16 +5,6 @@
#pragma once
// -----------------------------------------------------------------------------
// WIFI
// -----------------------------------------------------------------------------
#define WIFI_STATE_AP 1
#define WIFI_STATE_STA 2
#define WIFI_STATE_AP_STA 3
#define WIFI_STATE_WPS 4
#define WIFI_STATE_SMARTCONFIG 8
// -----------------------------------------------------------------------------
// GPIO
// -----------------------------------------------------------------------------


+ 76
- 79
code/espurna/led.cpp View File

@ -68,7 +68,6 @@ void LedPattern::stop() {
// For network-based modes, cycle ON & OFF (time in milliseconds)
// XXX: internals convert these to clock cycles, delay cannot be longer than 25000 / 50000 ms
static const LedDelay _ledDelays[] {
{100, 100}, // Autoconfig
{100, 4900}, // Connected
{4900, 100}, // Connected (inverse)
{100, 900}, // Config / AP
@ -76,19 +75,16 @@ static const LedDelay _ledDelays[] {
{500, 500} // Idle
};
enum class LedDelayName {
NetworkAutoconfig,
enum class LedDelayName : int {
NetworkConnected,
NetworkConnectedInverse,
NetworkConfig,
NetworkConfigInverse,
NetworkIdle,
None
NetworkIdle
};
bool _led_update { false };
std::vector<led_t> _leds;
bool _led_update { false };
// -----------------------------------------------------------------------------
@ -186,12 +182,17 @@ bool ledStatus(size_t id) {
return false;
}
const LedDelay& _ledDelayFromName(LedDelayName pattern) {
static_assert(
(sizeof(_ledDelays) / sizeof(_ledDelays[0])) <= static_cast<int>(LedDelayName::None),
"Out-of-bounds"
);
return _ledDelays[static_cast<int>(pattern)];
const LedDelay& _ledDelayFromName(LedDelayName name) {
switch (name) {
case LedDelayName::NetworkConnected:
case LedDelayName::NetworkConnectedInverse:
case LedDelayName::NetworkConfig:
case LedDelayName::NetworkConfigInverse:
case LedDelayName::NetworkIdle:
return _ledDelays[static_cast<int>(name)];
}
return _ledDelays[static_cast<int>(LedDelayName::NetworkIdle)];
}
void _ledPattern(led_t& led) {
@ -359,83 +360,77 @@ void ledUpdate(bool do_update) {
}
void ledLoop() {
const auto wifi_state = wifiState();
for (size_t id = 0; id < _leds.size(); ++id) {
auto& led = _leds[id];
switch (led.mode()) {
case LED_MODE_MANUAL:
break;
case LED_MODE_MANUAL:
break;
case LED_MODE_WIFI:
if (wifiConnected()) {
_ledBlink(led, LedDelayName::NetworkConnected);
} else if (wifiConnectable()) {
_ledBlink(led, LedDelayName::NetworkConfig);
} else {
_ledBlink(led, LedDelayName::NetworkIdle);
}
break;
case LED_MODE_WIFI:
if ((wifi_state & WIFI_STATE_WPS) || (wifi_state & WIFI_STATE_SMARTCONFIG)) {
_ledBlink(led, LedDelayName::NetworkAutoconfig);
} else if (wifi_state & WIFI_STATE_STA) {
#if RELAY_SUPPORT
case LED_MODE_FINDME_WIFI:
if (wifiConnected()) {
if (relayStatus(_led_relays[id])) {
_ledBlink(led, LedDelayName::NetworkConnected);
} else if (wifi_state & WIFI_STATE_AP) {
} else {
_ledBlink(led, LedDelayName::NetworkConnectedInverse);
}
} else if (wifiConnectable()) {
if (relayStatus(_led_relays[id])) {
_ledBlink(led, LedDelayName::NetworkConfig);
} else {
_ledBlink(led, LedDelayName::NetworkIdle);
_ledBlink(led, LedDelayName::NetworkConfigInverse);
}
} else {
_ledBlink(led, LedDelayName::NetworkIdle);
}
break;
#if RELAY_SUPPORT
case LED_MODE_FINDME_WIFI:
if ((wifi_state & WIFI_STATE_WPS) || (wifi_state & WIFI_STATE_SMARTCONFIG)) {
_ledBlink(led, LedDelayName::NetworkAutoconfig);
} else if (wifi_state & WIFI_STATE_STA) {
if (relayStatus(_led_relays[id])) {
_ledBlink(led, LedDelayName::NetworkConnected);
} else {
_ledBlink(led, LedDelayName::NetworkConnectedInverse);
}
} else if (wifi_state & WIFI_STATE_AP) {
if (relayStatus(_led_relays[id])) {
_ledBlink(led, LedDelayName::NetworkConfig);
} else {
_ledBlink(led, LedDelayName::NetworkConfigInverse);
}
case LED_MODE_RELAY_WIFI:
if (wifiConnected()) {
if (relayStatus(_led_relays[id])) {
_ledBlink(led, LedDelayName::NetworkConnected);
} else {
_ledBlink(led, LedDelayName::NetworkIdle);
_ledBlink(led, LedDelayName::NetworkConnectedInverse);
}
break;
case LED_MODE_RELAY_WIFI:
if ((wifi_state & WIFI_STATE_WPS) || (wifi_state & WIFI_STATE_SMARTCONFIG)) {
_ledBlink(led, LedDelayName::NetworkAutoconfig);
} else if (wifi_state & WIFI_STATE_STA) {
if (relayStatus(_led_relays[id])) {
_ledBlink(led, LedDelayName::NetworkConnected);
} else {
_ledBlink(led, LedDelayName::NetworkConnectedInverse);
}
} else if (wifi_state & WIFI_STATE_AP) {
if (relayStatus(_led_relays[id])) {
_ledBlink(led, LedDelayName::NetworkConfig);
} else {
_ledBlink(led, LedDelayName::NetworkConfigInverse);
}
} else if (wifiConnectable()) {
if (relayStatus(_led_relays[id])) {
_ledBlink(led, LedDelayName::NetworkConfig);
} else {
_ledBlink(led, LedDelayName::NetworkIdle);
_ledBlink(led, LedDelayName::NetworkConfigInverse);
}
break;
} else {
_ledBlink(led, LedDelayName::NetworkIdle);
}
break;
case LED_MODE_FOLLOW:
if (!_led_update) break;
case LED_MODE_FOLLOW:
if (_led_update) {
_ledStatus(led, relayStatus(_led_relays[id]));
break;
}
break;
case LED_MODE_FOLLOW_INVERSE:
if (!_led_update) break;
case LED_MODE_FOLLOW_INVERSE:
if (_led_update) {
led.status(!relayStatus(_led_relays[id]));
_ledStatus(led, !relayStatus(_led_relays[id]));
break;
}
break;
case LED_MODE_FINDME: {
if (!_led_update) break;
case LED_MODE_FINDME:
if (_led_update) {
bool status = true;
for (size_t relayId = 0; relayId < relayCount(); ++relayId) {
if (relayStatus(relayId)) {
@ -444,11 +439,11 @@ void ledLoop() {
}
}
_ledStatus(led, status);
break;
}
break;
case LED_MODE_RELAY: {
if (!_led_update) break;
case LED_MODE_RELAY:
if (_led_update) {
bool status = false;
for (size_t relayId = 0; relayId < relayCount(); ++relayId) {
if (relayStatus(relayId)) {
@ -457,20 +452,22 @@ void ledLoop() {
}
}
_ledStatus(led, status);
break;
}
break;
#endif // RELAY_SUPPORT == 1
#endif // RELAY_SUPPORT == 1
case LED_MODE_ON:
if (!_led_update) break;
case LED_MODE_ON:
if (_led_update) {
_ledStatus(led, true);
break;
}
break;
case LED_MODE_OFF:
if (!_led_update) break;
case LED_MODE_OFF:
if (_led_update) {
_ledStatus(led, false);
break;
}
break;
}


+ 3
- 3
code/espurna/led.h View File

@ -47,15 +47,15 @@ struct LedDelay {
LedDelay(on_ms, off_ms, 0)
{}
LedDelayMode mode() const {
constexpr LedDelayMode mode() const {
return _mode;
}
unsigned long on() const {
constexpr unsigned long on() const {
return _on;
}
unsigned long off() const {
constexpr unsigned long off() const {
return _off;
}


+ 12
- 6
code/espurna/mdns.cpp View File

@ -81,18 +81,24 @@ void mdnsServerSetup() {
return;
}
wifiRegister([](justwifi_messages_t code, char * parameter) {
if (code == MESSAGE_CONNECTED) {
_mdnsServerStart();
// 2.7.4 and older require MDNS.begin() when interface is UP
// 3.0.0 and newer only need to do MDNS.begin() once at setup()
// (TODO: this is techically a constexpr, but not in 2.7.4 :/)
const static bool OldCore { esp8266::coreVersionNumeric() <= 20704000 };
wifiRegister([](wifi::Event event) {
if (event == wifi::Event::StationConnected) {
#if MQTT_SUPPORT
_mdnsFindMQTT();
#endif
}
if (code == MESSAGE_ACCESSPOINT_CREATED) {
} else if (OldCore && (event == wifi::Event::Mode)) {
_mdnsServerStart();
}
});
if (!OldCore) {
_mdnsServerStart();
}
}
#endif // MDNS_SERVER_SUPPORT

+ 3
- 3
code/espurna/mqtt.cpp View File

@ -642,7 +642,7 @@ bool _mqttHeartbeat(heartbeat::Mask mask) {
mqttSend(MQTT_TOPIC_BSSID, WiFi.BSSIDstr().c_str());
if (mask & heartbeat::Report::Ip)
mqttSend(MQTT_TOPIC_IP, getIP().c_str());
mqttSend(MQTT_TOPIC_IP, wifiStaIp().toString().c_str());
if (mask & heartbeat::Report::Mac)
mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
@ -974,7 +974,7 @@ void mqttFlush() {
root[MQTT_TOPIC_HOSTNAME] = getSetting("hostname", getIdentifier());
#endif
#if MQTT_ENQUEUE_IP
root[MQTT_TOPIC_IP] = getIP();
root[MQTT_TOPIC_IP] = wifiStaIp().toString();
#endif
#if MQTT_ENQUEUE_MESSAGE_ID
root[MQTT_TOPIC_MESSAGE_ID] = (Rtcmem->mqtt)++;
@ -1148,7 +1148,7 @@ void _mqttConnect() {
if (_mqtt.connected() || (_mqtt_state != AsyncClientState::Disconnected)) return;
// Do not connect if disabled or no WiFi
if (!_mqtt_enabled || (WiFi.status() != WL_CONNECTED)) return;
if (!_mqtt_enabled || (!wifiConnected())) return;
// Check reconnect interval
if (millis() - _mqtt_last_connection < _mqtt_reconnect_delay) return;


+ 2
- 5
code/espurna/telnet.cpp View File

@ -169,8 +169,8 @@ static std::vector<char> _telnet_data_buffer;
void _telnetDisconnect(unsigned char clientId) {
_telnetClients[clientId]->stop();
_telnetClients[clientId] = nullptr;
wifiReconnectCheck();
DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), clientId);
wifiApCheck();
}
#elif TELNET_SERVER == TELNET_SERVER_ASYNC
@ -180,8 +180,8 @@ void _telnetCleanUp() {
for (unsigned char clientId=0; clientId < TELNET_MAX_CLIENTS; ++clientId) {
if (!_telnetClients[clientId]->connected()) {
_telnetClients[clientId] = nullptr;
wifiReconnectCheck();
DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), clientId);
wifiApCheck();
}
}
});
@ -363,7 +363,6 @@ void _telnetNotifyConnected(unsigned char i) {
// If there is no terminal support automatically dump info and crash data
#if DEBUG_SUPPORT
#if not TERMINAL_SUPPORT
wifiDebug();
crashDump(terminalDefaultStream());
crashClear();
#endif
@ -382,8 +381,6 @@ void _telnetNotifyConnected(unsigned char i) {
_telnetClientsAuth[i] = true;
}
wifiReconnectCheck();
}
#if TELNET_SERVER == TELNET_SERVER_WIFISERVER


+ 10
- 7
code/espurna/tuya.cpp View File

@ -166,11 +166,11 @@ namespace tuya {
// --------------------------------------------
uint8_t getWiFiState() {
uint8_t state = wifiState();
if (state & WIFI_STATE_SMARTCONFIG) return 0x00;
if (state & WIFI_STATE_AP) return 0x01;
if (state & WIFI_STATE_STA) return 0x04;
if (wifiConnected()) {
return 0x04;
} else if (wifiConnectable()) {
return 0x01;
}
return 0x02;
}
@ -651,9 +651,12 @@ error:
TUYA_SERIAL.begin(SerialSpeed);
::espurnaRegisterLoop(loop);
::wifiRegister([](justwifi_messages_t code, char * parameter) {
if ((MESSAGE_CONNECTED == code) || (MESSAGE_DISCONNECTED == code)) {
::wifiRegister([](wifi::Event event) {
switch (event) {
case wifi::Event::StationConnected:
case wifi::Event::StationDisconnected:
sendWiFiStatus();
break;
}
});
}


+ 2157
- 579
code/espurna/wifi.cpp
File diff suppressed because it is too large
View File


+ 47
- 17
code/espurna/wifi.h View File

@ -20,7 +20,7 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
// (HACK) allow us to use internal lwip struct.
// esp8266 re-defines enum values from tcp header... include them first
#define LWIP_INTERNAL
#include <JustWifi.h>
#include <ESP8266WiFi.h>
#include <Ticker.h>
#undef LWIP_INTERNAL
@ -40,35 +40,65 @@ extern "C" {
#define TCP_MSS (1460)
#endif
using wifi_callback_f = void(*)(justwifi_messages_t code, char * parameter);
namespace wifi {
enum class Event {
Initial, // aka boot
Mode, // when opmode changes
StationInit, // station initialized by the connetion routine
StationScan, // scanning before the connection
StationConnecting, // network was selected and connection is in progress
StationConnected, // successful connection
StationDisconnected, // disconnected from the current network
StationTimeout, // timeout after the previous connecting state
StationReconnect // timeout after all connection loops failed
};
using EventCallback = void(*)(Event event);
enum class StaMode {
Disabled,
Enabled
};
enum class WiFiApMode {
enum class ApMode {
Disabled,
Enabled,
Fallback
};
uint8_t wifiState();
void wifiReconnectCheck();
} // namespace wifi
// Note that 'connected' status is *only* for the WiFi STA.
// Overall connectivity depends on low-level network stack and it may be
// useful to check whether relevant interfaces are up and have a routable IP
// instead of exclusively depending on the WiFi API.
// (e.g. when we have injected ethernet, wireguard, etc. interfaces.
// esp8266 implementation specifically uses lwip, ref. `netif_list`)
bool wifiConnected();
String getNetwork();
String getIP();
// Whether the AP is up and running
bool wifiConnectable();
// Current STA connection
String wifiStaSsid();
IPAddress wifiStaIp();
void wifiDebug();
void wifiDebug(WiFiMode_t modes);
// Request to change the current STA / AP status
// Current state persists until reset or configuration reload
void wifiToggleAp();
void wifiToggleSta();
WiFiApMode wifiApMode();
void wifiStartAP();
void wifiStartSTA();
// Disconnects STA intefrace
// (and will immediatly trigger a reconnection)
void wifiDisconnect();
// Toggle WiFi modem
void wifiTurnOff();
void wifiTurnOn();
void wifiStartWPS();
void wifiStartSmartConfig();
void wifiRegister(wifi_callback_f callback);
// Trigger fallback check for the AP
void wifiApCheck();
void wifiRegister(wifi::EventCallback);
void wifiSetup();
void wifiLoop();

+ 96
- 8
code/espurna/wifi_config.h View File

@ -10,7 +10,92 @@ Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
#include "espurna.h"
constexpr bool _wifiHasSSID(unsigned char index) {
namespace wifi {
namespace build {
constexpr size_t NetworksMax { WIFI_MAX_NETWORKS };
constexpr unsigned long staReconnectionInterval() {
return WIFI_RECONNECT_INTERVAL;
}
constexpr unsigned long staConnectionInterval() {
return WIFI_CONNECT_INTERVAL;
}
constexpr int staConnectionRetries() {
return WIFI_CONNECT_RETRIES;
}
constexpr wifi::StaMode staMode() {
return WIFI_STA_MODE;
}
constexpr bool softApCaptive() {
return 1 == WIFI_AP_CAPTIVE_ENABLED;
}
constexpr wifi::ApMode softApMode() {
return WIFI_AP_MODE;
}
constexpr uint8_t softApChannel() {
return WIFI_AP_CHANNEL;
}
constexpr bool hasSoftApSsid() {
return strlen(WIFI_AP_SSID);
}
const __FlashStringHelper* softApSsid() {
return F(WIFI_AP_SSID);
}
constexpr bool hasSoftApPassphrase() {
return strlen(WIFI_AP_PASS);
}
const __FlashStringHelper* softApPassphrase() {
return F(WIFI_AP_PASS);
}
constexpr unsigned long softApFallbackTimeout() {
return WIFI_FALLBACK_TIMEOUT;
}
constexpr bool scanNetworks() {
return 1 == WIFI_SCAN_NETWORKS;
}
constexpr int8_t scanRssiThreshold() {
return WIFI_SCAN_RSSI_THRESHOLD;
}
constexpr unsigned long scanRssiCheckInterval() {
return WIFI_SCAN_RSSI_CHECK_INTERVAL;
}
constexpr int8_t scanRssiChecks() {
return WIFI_SCAN_RSSI_CHECKS;
}
constexpr unsigned long garpIntervalMin() {
return WIFI_GRATUITOUS_ARP_INTERVAL_MIN;
}
constexpr unsigned long garpIntervalMax() {
return WIFI_GRATUITOUS_ARP_INTERVAL_MAX;
}
constexpr WiFiSleepType_t sleep() {
return WIFI_SLEEP_MODE;
}
constexpr float outputDbm() {
return WIFI_OUTPUT_POWER_DBM;
}
constexpr bool hasSsid(size_t index) {
return (
(index == 0) ? (strlen(WIFI1_SSID) > 0) :
(index == 1) ? (strlen(WIFI2_SSID) > 0) :
@ -20,7 +105,7 @@ constexpr bool _wifiHasSSID(unsigned char index) {
);
}
constexpr bool _wifiHasIP(unsigned char index) {
constexpr bool hasIp(size_t index) {
return (
(index == 0) ? (strlen(WIFI1_IP) > 0) :
(index == 1) ? (strlen(WIFI2_IP) > 0) :
@ -30,7 +115,7 @@ constexpr bool _wifiHasIP(unsigned char index) {
);
}
const __FlashStringHelper* _wifiSSID(unsigned char index) {
const __FlashStringHelper* ssid(size_t index) {
return (
(index == 0) ? F(WIFI1_SSID) :
(index == 1) ? F(WIFI2_SSID) :
@ -40,7 +125,7 @@ const __FlashStringHelper* _wifiSSID(unsigned char index) {
);
}
const __FlashStringHelper* _wifiPass(unsigned char index) {
const __FlashStringHelper* passphrase(size_t index) {
return (
(index == 0) ? F(WIFI1_PASS) :
(index == 1) ? F(WIFI2_PASS) :
@ -50,7 +135,7 @@ const __FlashStringHelper* _wifiPass(unsigned char index) {
);
}
const __FlashStringHelper* _wifiIP(unsigned char index) {
const __FlashStringHelper* ip(size_t index) {
return (
(index == 0) ? F(WIFI1_IP) :
(index == 1) ? F(WIFI2_IP) :
@ -60,7 +145,7 @@ const __FlashStringHelper* _wifiIP(unsigned char index) {
);
}
const __FlashStringHelper* _wifiGateway(unsigned char index) {
const __FlashStringHelper* gateway(size_t index) {
return (
(index == 0) ? F(WIFI1_GW) :
(index == 1) ? F(WIFI2_GW) :
@ -70,7 +155,7 @@ const __FlashStringHelper* _wifiGateway(unsigned char index) {
);
}
const __FlashStringHelper* _wifiNetmask(unsigned char index) {
const __FlashStringHelper* mask(size_t index) {
return (
(index == 0) ? F(WIFI1_MASK) :
(index == 1) ? F(WIFI2_MASK) :
@ -80,7 +165,7 @@ const __FlashStringHelper* _wifiNetmask(unsigned char index) {
);
}
const __FlashStringHelper* _wifiDNS(unsigned char index) {
const __FlashStringHelper* dns(size_t index) {
return (
(index == 0) ? F(WIFI1_DNS) :
(index == 1) ? F(WIFI2_DNS) :
@ -89,3 +174,6 @@ const __FlashStringHelper* _wifiDNS(unsigned char index) {
(index == 4) ? F(WIFI5_DNS) : nullptr
);
}
} // namespace build
} // namespace wifi

+ 11
- 8
code/espurna/ws.cpp View File

@ -13,9 +13,10 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include <vector>
#include "system.h"
#include "web.h"
#include "ntp.h"
#include "utils.h"
#include "web.h"
#include "wifi.h"
#include "ws_internal.h"
#include "libs/WebSocketIncommingBuffer.h"
@ -243,9 +244,12 @@ void WsDebug::send(bool connected) {
}
bool wsDebugSend(const char* prefix, const char* message) {
if (!wsConnected()) return false;
_ws_debug.add(prefix, message);
return true;
if (wifiConnected() && wsConnected()) {
_ws_debug.add(prefix, message);
return true;
}
return false;
}
#endif
@ -480,8 +484,8 @@ void _wsOnConnected(JsonObject& root) {
root["channel"] = WiFi.channel();
root["hostname"] = getSetting("hostname");
root["desc"] = getSetting("desc");
root["network"] = getNetwork();
root["deviceip"] = getIP();
root["network"] = wifiStaSsid();
root["deviceip"] = wifiStaIp().toString();
root["sketch_size"] = ESP.getSketchSize();
root["free_size"] = ESP.getFreeSketchSpace();
root["sdk"] = ESP.getSdkVersion();
@ -530,7 +534,6 @@ void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTy
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u connected, ip: %d.%d.%d.%d, url: %s\n"), client->id(), ip[0], ip[1], ip[2], ip[3], server->url());
_wsConnected(client->id());
_wsResetUpdateTimer();
wifiReconnectCheck();
client->_tempObject = new WebSocketIncommingBuffer(_wsParse, true);
} else if(type == WS_EVT_DISCONNECT) {
@ -538,7 +541,7 @@ void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTy
if (client->_tempObject) {
delete (WebSocketIncommingBuffer *) client->_tempObject;
}
wifiReconnectCheck();
wifiApCheck();
} else if(type == WS_EVT_ERROR) {
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u error(%u): %s\n"), client->id(), *((uint16_t*)arg), (char*)data);


+ 0
- 31
code/platformio.ini View File

@ -136,7 +136,6 @@ lib_deps =
https://github.com/vintlabs/fauxmoESP#3.1.2
https://github.com/xoseperez/hlw8012.git#1.1.0
IRremoteESP8266@2.7.4
https://github.com/mcspr/justwifi.git#2cb9e769
https://github.com/xoseperez/my92xx#3.0.1
https://github.com/256dpi/arduino-mqtt#196556b6
https://bitbucket.org/xoseperez/nofuss.git#0.3.0
@ -255,36 +254,6 @@ src_build_flags = -DESPURNA_CORE
extends = env:esp8266-4m-base
src_build_flags = -DESPURNA_CORE
[env:espurna-core-smartconfig-1MB]
extends = env:esp8266-1m-base
src_build_flags = -DESPURNA_CORE
build_flags = ${common.build_flags} -DJUSTWIFI_ENABLE_SMARTCONFIG=1
[env:espurna-core-smartconfig-2MB]
extends = env:esp8266-2m-base
src_build_flags = -DESPURNA_CORE
build_flags = ${common.build_flags} -DJUSTWIFI_ENABLE_SMARTCONFIG=1
[env:espurna-core-smartconfig-4MB]
extends = env:esp8266-4m-base
src_build_flags = -DESPURNA_CORE
build_flags = ${common.build_flags} -DJUSTWIFI_ENABLE_SMARTCONFIG=1
[env:espurna-core-wps-1MB]
extends = env:esp8266-1m-base
src_build_flags = -DESPURNA_CORE
build_flags = ${common.build_flags} -DJUSTWIFI_ENABLE_WPS=1
[env:espurna-core-wps-2MB]
extends = env:esp8266-2m-base
src_build_flags = -DESPURNA_CORE
build_flags = ${common.build_flags} -DJUSTWIFI_ENABLE_WPS=1
[env:espurna-core-wps-4MB]
extends = env:esp8266-4m-base
src_build_flags = -DESPURNA_CORE
build_flags = ${common.build_flags} -DJUSTWIFI_ENABLE_WPS=1
# ------------------------------------------------------------------------------
# ESPURNA CORE with WebUI
# ------------------------------------------------------------------------------


Loading…
Cancel
Save