Add reliable wifi connection #2 #3

This commit is contained in:
Aaron Fischer 2017-11-12 22:34:08 +01:00
parent 458f69ce5a
commit c9fd2ad2ac

View file

@ -1,5 +1,9 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include "config.h" #include "config.h"
/** /**
@ -10,10 +14,8 @@
* - Push sensor values to various 3rd party services * - Push sensor values to various 3rd party services
* - MQTT? * - MQTT?
* *
* - Auto-Reconnect for WIFI
* - Buffer sensor values if there is no WIFI connection * - Buffer sensor values if there is no WIFI connection
* - Configure weather station over http webinterface * - Configure weather station over http webinterface
* - If there are now WiFi credentials, open up an access point to configure it
* - OTA firmware update * - OTA firmware update
* *
* TODO: * TODO:
@ -23,31 +25,36 @@
float currentSensorData[4] = {0.0, 0.0, 0.0, 0.0}; float currentSensorData[4] = {0.0, 0.0, 0.0, 0.0};
void connectToWiFi() {
} WiFiManager wifiManager;
bool checkWiFiConnection() {
return true;
}
void setup() { void setup() {
#ifdef DEBUG #ifdef DEBUG
Serial.begin(115200); Serial.begin(115200);
#endif #endif
// Pin settings
pinMode(STATUS_LED_PIN, OUTPUT); pinMode(STATUS_LED_PIN, OUTPUT);
pinMode(ANEMOMETER_PIN, INPUT_PULLUP); pinMode(ANEMOMETER_PIN, INPUT_PULLUP);
connectToWiFi(); // Establish wifi connection
String wifiName = "oko-weather-" + String(ESP.getChipId());
wifiManager.setMinimumSignalQuality(15);
if (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) {
#ifdef DEBUG
Serial.println("WiFi connection failed, we reboot ...");
#endif
ESP.reset();
delay(1000);
}
#ifdef DEBUG
Serial.println("Connected!");
#endif
} }
void loop() { void loop() {
digitalWrite(STATUS_LED_PIN, LOW); digitalWrite(STATUS_LED_PIN, LOW);
while (!checkWiFiConnection()) {
connectToWiFi();
delay(5000);
}
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature(); currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
currentSensorData[SENSOR_HUMIDITY] = fetchHumidity(); currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
currentSensorData[SENSOR_LIGHT] = fetchLight(); currentSensorData[SENSOR_LIGHT] = fetchLight();
@ -64,6 +71,5 @@ void loop() {
pushToInfluxDB(currentSensorData); pushToInfluxDB(currentSensorData);
delay(UPDATE_INTERVAL*1000); delay(UPDATE_INTERVAL*1000);
} }