Fix esp8266 wifi connection

This commit is contained in:
fvanroie 2020-11-03 23:55:43 +01:00
parent 9b6652ffce
commit d2b82ba716
3 changed files with 40 additions and 39 deletions

View File

@ -16,7 +16,8 @@ uint16_t gpioConfig[HASP_NUM_GPIO_CONFIG];
using namespace ace_button;
static AceButton * button[HASP_NUM_INPUTS];
struct hasp_gpio_config_t {
struct hasp_gpio_config_t
{
const uint8_t pin;
const uint8_t group;
const uint8_t io_mode;
@ -26,11 +27,7 @@ struct hasp_gpio_config_t {
// An array of button pins, led pins, and the led states. Cannot be const
// because ledState is mutable.
hasp_gpio_config_t gpioConfig2[HASP_NUM_GPIO_CONFIG] = {
{2, 8, INPUT, LOW},
{3, 9, OUTPUT, LOW},
{4, 10, INPUT, HIGH},
{5, 11, OUTPUT, LOW},
{6, 12, INPUT, LOW},
{2, 8, INPUT, LOW}, {3, 9, OUTPUT, LOW}, {4, 10, INPUT, HIGH}, {5, 11, OUTPUT, LOW}, {6, 12, INPUT, LOW},
};
#if defined(ARDUINO_ARCH_ESP32)
@ -116,7 +113,6 @@ void IRAM_ATTR gpioLoop(void)
void gpioAddButton(uint8_t pin, uint8_t input_mode, uint8_t default_state, uint8_t channel)
{
uint8_t i;
for(i = 0; i < HASP_NUM_INPUTS; i++) {
@ -189,8 +185,8 @@ void gpioSetup()
#endif
#if defined(ARDUINO_ARCH_ESP32)
gpioAddButton( D2, INPUT, HIGH, 1);
pinMode(D1, OUTPUT);
// gpioAddButton( D2, INPUT, HIGH, 1);
// pinMode(D1, OUTPUT);
#endif
for(uint8_t i = 0; i < HASP_NUM_GPIO_CONFIG; i++) {
@ -220,7 +216,8 @@ void gpioSetup()
switch(gpiotype) {
case HASP_GPIO_SWITCH:
case HASP_GPIO_BUTTON:
// gpioAddButton(gpioConfig[i].io_mode.pin, input_mode, gpioConfig[i].default_state, gpioConfig[i].group);
// gpioAddButton(gpioConfig[i].io_mode.pin, input_mode, gpioConfig[i].default_state,
// gpioConfig[i].group);
break;
case HASP_GPIO_RELAY:

View File

@ -479,9 +479,9 @@ void mqttLoop()
if(mqttEnabled) mqttClient.loop();
}
void mqttEvery5Seconds(bool wifiIsConnected)
void mqttEvery5Seconds(bool networkIsConnected)
{
if(mqttEnabled && wifiIsConnected && !mqttClient.connected()) mqttReconnect();
if(mqttEnabled && networkIsConnected && !mqttClient.connected()) mqttReconnect();
}
String mqttGetNodename()

View File

@ -136,6 +136,18 @@ bool wifiShowAP(char * ssid, char * pass)
return true;
}
void wifiReconnect()
{
WiFi.disconnect(true);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
#if defined(ARDUINO_ARCH_ESP8266)
WiFi.hostname(mqttGetNodename().c_str());
#elif defined(ARDUINO_ARCH_ESP32)
WiFi.setHostname(mqttGetNodename().c_str());
#endif
WiFi.begin(wifiSsid, wifiPassword);
}
void wifiSetup()
{
if(wifiShowAP()) {
@ -148,17 +160,12 @@ void wifiSetup()
gotIpEventHandler = WiFi.onStationModeGotIP(wifiSTAGotIP); // As soon WiFi is connected, start NTP Client
disconnectedEventHandler = WiFi.onStationModeDisconnected(wifiSTADisconnected);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
#endif
#if defined(ARDUINO_ARCH_ESP32)
#elif defined(ARDUINO_ARCH_ESP32)
WiFi.onEvent(wifi_callback);
WiFi.setSleep(false);
#endif
WiFi.disconnect(true);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(mqttGetNodename().c_str());
WiFi.begin(wifiSsid, wifiPassword);
wifiReconnect();
Log.notice(F("WIFI: Connecting to : %s"), wifiSsid);
}
}
@ -177,10 +184,7 @@ bool wifiEvery5Seconds()
}
Log.warning(F("WIFI: No Connection... retry %u"), wifiReconnectCounter);
if(wifiReconnectCounter % 6 == 0) {
WiFi.disconnect(true);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(mqttGetNodename().c_str());
WiFi.begin(wifiSsid, wifiPassword);
wifiReconnect();
}
return false;
}