Reconnect to wifi if connection lost...

This commit is contained in:
Joe Tretter
2023-09-09 14:27:58 -05:00
parent 22783a6e58
commit 5a2c6de2f8

View File

@@ -16,6 +16,7 @@ const char* mqtt_server = "mqtt.ddns.kawomi.com";
const int buttonPin = 4; // Reed switch - don't forget the pull down resistor.
const int ledPin = 2; // Internal LED
int wifiState = WL_DISCONNECTED; // Wifi-Connection Status
boolean bForceResend = true; // send initial state to mqtt on startup.
int previousState;
@@ -174,21 +175,9 @@ void reconnect() {
/*
* setup function
*/
void setup(void) {
Serial.begin(9600);
delay(500);
Serial.println(">>>>>>>>>> Garage Door Monitor <<<<<<<<<<<<<<");
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// if we stat up we need to see what the status is...
digitalWrite(ledPin, previousState = digitalRead(buttonPin));
Serial.printf("Initial State: %d", previousState);
randomSeed(micros());
// Connect to WiFi network
void wifiSetup(void){
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.println("");
@@ -218,6 +207,23 @@ void setup(void) {
}
}
Serial.println("mDNS responder started");
}
void setup(void) {
Serial.begin(9600);
delay(500);
Serial.println(">>>>>>>>>> Garage Door Monitor <<<<<<<<<<<<<<");
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// if we stat up we need to see what the status is...
digitalWrite(ledPin, previousState = digitalRead(buttonPin));
Serial.printf("Initial State: %d", previousState);
randomSeed(micros());
wifiSetup();
mqttClient.setServer(mqtt_server, 1883);
Serial.println("mqtt server set.");
@@ -302,6 +308,19 @@ void setup(void) {
}
void loop(void) {
if (wifiState!=WiFi.status()) {
Serial.print("Wifi Status Change: ");
Serial.println(WiFi.status());
wifiState=WiFi.status();
}
// if we lost connection reconnect...
if (WL_CONNECTED != WiFi.status()){
WiFi.disconnect();
delay(1000);
wifiSetup();
}
server.handleClient();
delay(1000);