mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-28 05:36:37 +00:00
Fix esp8266 wifi connection
This commit is contained in:
parent
9b6652ffce
commit
d2b82ba716
@ -16,26 +16,23 @@ uint16_t gpioConfig[HASP_NUM_GPIO_CONFIG];
|
|||||||
using namespace ace_button;
|
using namespace ace_button;
|
||||||
static AceButton * button[HASP_NUM_INPUTS];
|
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 pin;
|
||||||
const uint8_t io_mode;
|
const uint8_t group;
|
||||||
bool default_state;
|
const uint8_t io_mode;
|
||||||
|
bool default_state;
|
||||||
};
|
};
|
||||||
|
|
||||||
// An array of button pins, led pins, and the led states. Cannot be const
|
// An array of button pins, led pins, and the led states. Cannot be const
|
||||||
// because ledState is mutable.
|
// because ledState is mutable.
|
||||||
hasp_gpio_config_t gpioConfig2[HASP_NUM_GPIO_CONFIG] = {
|
hasp_gpio_config_t gpioConfig2[HASP_NUM_GPIO_CONFIG] = {
|
||||||
{2, 8, INPUT, LOW},
|
{2, 8, INPUT, LOW}, {3, 9, OUTPUT, LOW}, {4, 10, INPUT, HIGH}, {5, 11, OUTPUT, LOW}, {6, 12, INPUT, LOW},
|
||||||
{3, 9, OUTPUT, LOW},
|
|
||||||
{4, 10, INPUT, HIGH},
|
|
||||||
{5, 11, OUTPUT, LOW},
|
|
||||||
{6, 12, INPUT, LOW},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
class TouchConfig : public ButtonConfig {
|
class TouchConfig : public ButtonConfig {
|
||||||
public:
|
public:
|
||||||
TouchConfig();
|
TouchConfig();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@ -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;
|
uint8_t i;
|
||||||
for(i = 0; i < HASP_NUM_INPUTS; 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);
|
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;
|
uint8_t i;
|
||||||
for(i = 0; i < HASP_NUM_INPUTS; i++) {
|
for(i = 0; i < HASP_NUM_INPUTS; i++) {
|
||||||
@ -184,20 +180,20 @@ void gpioSetup()
|
|||||||
|
|
||||||
// gpioConfig[0] = PD15 * 256 + 5 + (INPUT << 3);
|
// gpioConfig[0] = PD15 * 256 + 5 + (INPUT << 3);
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
gpioAddButton( D2, INPUT_PULLUP, HIGH, 1);
|
gpioAddButton(D2, INPUT_PULLUP, HIGH, 1);
|
||||||
pinMode(D1, OUTPUT);
|
pinMode(D1, OUTPUT);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
gpioAddButton( D2, INPUT, HIGH, 1);
|
// gpioAddButton( D2, INPUT, HIGH, 1);
|
||||||
pinMode(D1, OUTPUT);
|
// pinMode(D1, OUTPUT);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for(uint8_t i = 0; i < HASP_NUM_GPIO_CONFIG; i++) {
|
for(uint8_t i = 0; i < HASP_NUM_GPIO_CONFIG; i++) {
|
||||||
uint8_t pin = (gpioConfig[i] >> 8) & 0xFF;
|
uint8_t pin = (gpioConfig[i] >> 8) & 0xFF;
|
||||||
uint8_t channel = gpioConfig[i] & 0b111; // 3bit
|
uint8_t channel = gpioConfig[i] & 0b111; // 3bit
|
||||||
uint8_t input_mode = (gpioConfig[i] >> 3) & 0b11; // 2bit gpio mode
|
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 gpiotype = (gpioConfig[i] >> 5) & 0b111; // 3bit
|
||||||
uint8_t default_state = gpioConfig[i] & 0b1; // 1bit: 0=LOW, 1=HIGH
|
uint8_t default_state = gpioConfig[i] & 0b1; // 1bit: 0=LOW, 1=HIGH
|
||||||
|
|
||||||
@ -220,7 +216,8 @@ void gpioSetup()
|
|||||||
switch(gpiotype) {
|
switch(gpiotype) {
|
||||||
case HASP_GPIO_SWITCH:
|
case HASP_GPIO_SWITCH:
|
||||||
case HASP_GPIO_BUTTON:
|
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;
|
break;
|
||||||
|
|
||||||
case HASP_GPIO_RELAY:
|
case HASP_GPIO_RELAY:
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#include "hasp_conf.h"
|
#include "hasp_conf.h"
|
||||||
#if HASP_USE_MQTT>0
|
#if HASP_USE_MQTT > 0
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "ArduinoJson.h"
|
#include "ArduinoJson.h"
|
||||||
@ -190,7 +190,7 @@ void mqtt_send_statusupdate()
|
|||||||
snprintf_P(data, sizeof(data), PSTR("{\"status\":\"available\",\"version\":\"%s\",\"uptime\":%lu,"),
|
snprintf_P(data, sizeof(data), PSTR("{\"status\":\"available\",\"version\":\"%s\",\"uptime\":%lu,"),
|
||||||
haspGetVersion().c_str(), long(millis() / 1000));
|
haspGetVersion().c_str(), long(millis() / 1000));
|
||||||
strcat(buffer, data);
|
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(),
|
snprintf_P(buffer, sizeof(buffer), PSTR("\"ssid\":\"%s\",\"rssi\":%i,\"ip\":\"%s\","), WiFi.SSID().c_str(),
|
||||||
WiFi.RSSI(), WiFi.localIP().toString().c_str());
|
WiFi.RSSI(), WiFi.localIP().toString().c_str());
|
||||||
strcat(data, buffer);
|
strcat(data, buffer);
|
||||||
@ -354,7 +354,7 @@ void mqttReconnect()
|
|||||||
{
|
{
|
||||||
String mac = halGetMacAddress(3, "");
|
String mac = halGetMacAddress(3, "");
|
||||||
mac.toLowerCase();
|
mac.toLowerCase();
|
||||||
memset(mqttClientId, 0 ,sizeof(mqttClientId));
|
memset(mqttClientId, 0, sizeof(mqttClientId));
|
||||||
snprintf_P(mqttClientId, sizeof(mqttClientId), PSTR("plate_%s"), mac.c_str());
|
snprintf_P(mqttClientId, sizeof(mqttClientId), PSTR("plate_%s"), mac.c_str());
|
||||||
Log.verbose(mqttClientId);
|
Log.verbose(mqttClientId);
|
||||||
}
|
}
|
||||||
@ -479,9 +479,9 @@ void mqttLoop()
|
|||||||
if(mqttEnabled) mqttClient.loop();
|
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()
|
String mqttGetNodename()
|
||||||
|
@ -136,6 +136,18 @@ bool wifiShowAP(char * ssid, char * pass)
|
|||||||
return true;
|
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()
|
void wifiSetup()
|
||||||
{
|
{
|
||||||
if(wifiShowAP()) {
|
if(wifiShowAP()) {
|
||||||
@ -148,17 +160,12 @@ void wifiSetup()
|
|||||||
gotIpEventHandler = WiFi.onStationModeGotIP(wifiSTAGotIP); // As soon WiFi is connected, start NTP Client
|
gotIpEventHandler = WiFi.onStationModeGotIP(wifiSTAGotIP); // As soon WiFi is connected, start NTP Client
|
||||||
disconnectedEventHandler = WiFi.onStationModeDisconnected(wifiSTADisconnected);
|
disconnectedEventHandler = WiFi.onStationModeDisconnected(wifiSTADisconnected);
|
||||||
WiFi.setSleepMode(WIFI_NONE_SLEEP);
|
WiFi.setSleepMode(WIFI_NONE_SLEEP);
|
||||||
#endif
|
#elif defined(ARDUINO_ARCH_ESP32)
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
|
||||||
WiFi.onEvent(wifi_callback);
|
WiFi.onEvent(wifi_callback);
|
||||||
WiFi.setSleep(false);
|
WiFi.setSleep(false);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
WiFi.disconnect(true);
|
wifiReconnect();
|
||||||
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
|
|
||||||
WiFi.setHostname(mqttGetNodename().c_str());
|
|
||||||
WiFi.begin(wifiSsid, wifiPassword);
|
|
||||||
|
|
||||||
Log.notice(F("WIFI: Connecting to : %s"), wifiSsid);
|
Log.notice(F("WIFI: Connecting to : %s"), wifiSsid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -177,10 +184,7 @@ bool wifiEvery5Seconds()
|
|||||||
}
|
}
|
||||||
Log.warning(F("WIFI: No Connection... retry %u"), wifiReconnectCounter);
|
Log.warning(F("WIFI: No Connection... retry %u"), wifiReconnectCounter);
|
||||||
if(wifiReconnectCounter % 6 == 0) {
|
if(wifiReconnectCounter % 6 == 0) {
|
||||||
WiFi.disconnect(true);
|
wifiReconnect();
|
||||||
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
|
|
||||||
WiFi.setHostname(mqttGetNodename().c_str());
|
|
||||||
WiFi.begin(wifiSsid, wifiPassword);
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user