From c9fd2ad2aca1d30e6bfdfaaaa353d98dfeca202a Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Sun, 12 Nov 2017 22:34:08 +0100 Subject: [PATCH] Add reliable wifi connection #2 #3 --- firmware/firmware.ino | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index abd58b5..a5a1a38 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -1,5 +1,9 @@ #include +#include +#include +#include + #include "config.h" /** @@ -10,10 +14,8 @@ * - Push sensor values to various 3rd party services * - MQTT? * - * - Auto-Reconnect for WIFI * - Buffer sensor values if there is no WIFI connection * - Configure weather station over http webinterface - * - If there are now WiFi credentials, open up an access point to configure it * - OTA firmware update * * TODO: @@ -23,30 +25,35 @@ float currentSensorData[4] = {0.0, 0.0, 0.0, 0.0}; -void connectToWiFi() { -} -bool checkWiFiConnection() { - return true; -} + +WiFiManager wifiManager; void setup() { #ifdef DEBUG Serial.begin(115200); #endif - + + // Pin settings pinMode(STATUS_LED_PIN, OUTPUT); 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() { digitalWrite(STATUS_LED_PIN, LOW); - - while (!checkWiFiConnection()) { - connectToWiFi(); - delay(5000); - } currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature(); currentSensorData[SENSOR_HUMIDITY] = fetchHumidity(); @@ -64,6 +71,5 @@ void loop() { pushToInfluxDB(currentSensorData); - delay(UPDATE_INTERVAL*1000); }