Reconnect to wifi if connection lost...
This commit is contained in:
45
src/main.cpp
45
src/main.cpp
@@ -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 buttonPin = 4; // Reed switch - don't forget the pull down resistor.
|
||||||
const int ledPin = 2; // Internal LED
|
const int ledPin = 2; // Internal LED
|
||||||
|
int wifiState = WL_DISCONNECTED; // Wifi-Connection Status
|
||||||
|
|
||||||
boolean bForceResend = true; // send initial state to mqtt on startup.
|
boolean bForceResend = true; // send initial state to mqtt on startup.
|
||||||
int previousState;
|
int previousState;
|
||||||
@@ -174,20 +175,8 @@ void reconnect() {
|
|||||||
/*
|
/*
|
||||||
* setup function
|
* 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());
|
|
||||||
|
|
||||||
|
void wifiSetup(void){
|
||||||
// Connect to WiFi network
|
// Connect to WiFi network
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
@@ -218,6 +207,23 @@ void setup(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Serial.println("mDNS responder started");
|
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);
|
mqttClient.setServer(mqtt_server, 1883);
|
||||||
Serial.println("mqtt server set.");
|
Serial.println("mqtt server set.");
|
||||||
@@ -302,6 +308,19 @@ void setup(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop(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();
|
server.handleClient();
|
||||||
delay(1000);
|
delay(1000);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user