mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-28 13:46:36 +00:00
Fix esp8266 wifi connection
This commit is contained in:
parent
9b6652ffce
commit
d2b82ba716
@ -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;
|
||||
@ -25,12 +26,8 @@ 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},
|
||||
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},
|
||||
};
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
@ -113,10 +110,9 @@ void IRAM_ATTR gpioLoop(void)
|
||||
}
|
||||
}
|
||||
|
||||
void gpioAddButton( uint8_t pin, uint8_t input_mode, uint8_t default_state, uint8_t channel)
|
||||
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++) {
|
||||
|
||||
@ -147,7 +143,7 @@ void gpioAddButton( uint8_t pin, uint8_t input_mode, uint8_t default_state, uint
|
||||
channel, HASP_NUM_INPUTS);
|
||||
}
|
||||
|
||||
void gpioAddTouchButton( uint8_t pin, uint8_t input_mode, uint8_t default_state, uint8_t channel)
|
||||
void gpioAddTouchButton(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++) {
|
||||
@ -184,20 +180,20 @@ void gpioSetup()
|
||||
|
||||
// gpioConfig[0] = PD15 * 256 + 5 + (INPUT << 3);
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
gpioAddButton( D2, INPUT_PULLUP, HIGH, 1);
|
||||
gpioAddButton(D2, INPUT_PULLUP, HIGH, 1);
|
||||
pinMode(D1, OUTPUT);
|
||||
#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++) {
|
||||
uint8_t pin = (gpioConfig[i] >> 8) & 0xFF;
|
||||
uint8_t channel = gpioConfig[i] & 0b111; // 3bit
|
||||
uint8_t input_mode = (gpioConfig[i] >> 3) & 0b11; // 2bit gpio mode
|
||||
//uint8_t input_mode = gpioConfig[i].io_mode
|
||||
// uint8_t input_mode = gpioConfig[i].io_mode
|
||||
uint8_t gpiotype = (gpioConfig[i] >> 5) & 0b111; // 3bit
|
||||
uint8_t default_state = gpioConfig[i] & 0b1; // 1bit: 0=LOW, 1=HIGH
|
||||
|
||||
@ -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:
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "hasp_conf.h"
|
||||
#if HASP_USE_MQTT>0
|
||||
#if HASP_USE_MQTT > 0
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "ArduinoJson.h"
|
||||
@ -190,7 +190,7 @@ void mqtt_send_statusupdate()
|
||||
snprintf_P(data, sizeof(data), PSTR("{\"status\":\"available\",\"version\":\"%s\",\"uptime\":%lu,"),
|
||||
haspGetVersion().c_str(), long(millis() / 1000));
|
||||
strcat(buffer, data);
|
||||
#if HASP_USE_WIFI>0
|
||||
#if HASP_USE_WIFI > 0
|
||||
snprintf_P(buffer, sizeof(buffer), PSTR("\"ssid\":\"%s\",\"rssi\":%i,\"ip\":\"%s\","), WiFi.SSID().c_str(),
|
||||
WiFi.RSSI(), WiFi.localIP().toString().c_str());
|
||||
strcat(data, buffer);
|
||||
@ -354,7 +354,7 @@ void mqttReconnect()
|
||||
{
|
||||
String mac = halGetMacAddress(3, "");
|
||||
mac.toLowerCase();
|
||||
memset(mqttClientId, 0 ,sizeof(mqttClientId));
|
||||
memset(mqttClientId, 0, sizeof(mqttClientId));
|
||||
snprintf_P(mqttClientId, sizeof(mqttClientId), PSTR("plate_%s"), mac.c_str());
|
||||
Log.verbose(mqttClientId);
|
||||
}
|
||||
@ -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()
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user