Merge branch 'wifi-improvements'
This commit is contained in:
commit
5cc36bbedf
@ -1,4 +0,0 @@
|
||||
struct WifiCredentials {
|
||||
const char* ssid;
|
||||
const char* password;
|
||||
} credentials[] = {"foo", "bar"};
|
||||
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#define MAIN_DEVICE_ID "esp_clock"
|
||||
#define SENSOR_ID "bme280"
|
||||
|
||||
#include "ha.h"
|
||||
@ -28,10 +27,9 @@ namespace Devices {
|
||||
.addSecondary(Builder<PressureSensor>::instance(SENSOR_ID).withValueTemplate("{{ value_json.pressure }}").build())
|
||||
.build();
|
||||
|
||||
Switch* ledMqtt = Builder<Switch>::instance(new Switch{ "Led", "led",
|
||||
auto ledMqtt = Builder<Switch>::instance(new Switch{ "Led", "led",
|
||||
[](const char* msg) {
|
||||
strcmp("ON", msg) == 0 ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH);
|
||||
ledMqtt->updateState(!digitalRead(LED_BUILTIN));
|
||||
turnLed(strcmp("ON", msg) == 0);
|
||||
}
|
||||
}).withStateTopic().restoreFromState().build();
|
||||
|
||||
@ -59,6 +57,7 @@ namespace Devices {
|
||||
[](const char* msg) {
|
||||
if (strcmp("PRESS", msg) == 0 && !Display::tDisplaySensor.isEnabled()) {
|
||||
Bme::data.readAll();
|
||||
Display::tDisplaySensor.setIterations(DISPLAY_SENSOR_ITERATIONS);
|
||||
Display::tDisplaySensor.restart();
|
||||
};
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ namespace Display {
|
||||
tDisplayTime.disable();
|
||||
return true;
|
||||
}, []() {
|
||||
tDisplaySensor.setIterations(DISPLAY_SENSOR_ITERATIONS);
|
||||
tDisplaySensor.setIterations(DISPLAY_SENSOR_ITERATIONS*2);
|
||||
tDisplayTime.enableDelayed(tDisplaySensor.getInterval());
|
||||
});
|
||||
|
||||
@ -50,7 +50,7 @@ namespace Display {
|
||||
void set(uint8_t value) {
|
||||
current = value % (BRIGHTNESS_MAX + 1);
|
||||
clockDisplay.setBrightness(current);
|
||||
brightnessChangedCallback();
|
||||
if (brightnessChangedCallback) brightnessChangedCallback();
|
||||
}
|
||||
|
||||
void change(bool increase) {
|
||||
@ -82,9 +82,12 @@ namespace Display {
|
||||
static int currentHour = -1;
|
||||
if (currentHour != hour()) {
|
||||
currentHour = hour();
|
||||
if (currentHour == 4) {
|
||||
Ntp::tUpdateTime.restart();
|
||||
}
|
||||
if (currentHour == 8) {
|
||||
Brightness::set(BRIGHTNESS_DAY);
|
||||
Wifi::reconnect();
|
||||
Wifi::tReconnect.enable();
|
||||
}
|
||||
if (currentHour == 17) {
|
||||
Brightness::set(BRIGHTNESS_NIGHT);
|
||||
@ -139,7 +142,7 @@ namespace Display {
|
||||
void changeHourFormat24(bool format24) {
|
||||
hourFormat24 = format24;
|
||||
drawTime();
|
||||
hourFormatChangedCallback();
|
||||
if (hourFormatChangedCallback) hourFormatChangedCallback();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
@ -14,18 +14,16 @@ namespace Ntp {
|
||||
TimeChangeRule CET = { "CET ", Last, Sun, Oct, 3, 60 }; // Central European Standard Time
|
||||
Timezone CE(CEST, CET);
|
||||
|
||||
time_t updateTime() {
|
||||
Task tUpdateTime(TASK_IMMEDIATE, TASK_ONCE, []{
|
||||
if (timeClient.forceUpdate()) {
|
||||
time_t newTime = CE.toLocal(timeClient.getEpochTime());
|
||||
setTime(newTime);
|
||||
return newTime;
|
||||
} else {
|
||||
return 0;
|
||||
Serial.println(asctime(localtime(&newTime)));
|
||||
}
|
||||
}
|
||||
}, &ts);
|
||||
|
||||
void setup() {
|
||||
timeClient.begin();
|
||||
Serial.println("NTP setup");
|
||||
timeClient.begin();
|
||||
}
|
||||
}
|
||||
@ -1,35 +0,0 @@
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
namespace Ota {
|
||||
|
||||
void loop();
|
||||
Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true);
|
||||
|
||||
void setup() {
|
||||
ArduinoOTA.onStart([]() {
|
||||
Serial.println("Starting OTA");
|
||||
Mqtt::publishCleanupConfig();
|
||||
delay(2000);
|
||||
Mqtt::disconnect();
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
Serial.println("\nOTA Finished");
|
||||
});
|
||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
||||
});
|
||||
ArduinoOTA.onError([](ota_error_t error) {
|
||||
Serial.printf("Error[%u]: ", error);
|
||||
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
|
||||
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
|
||||
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
|
||||
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
|
||||
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
||||
});
|
||||
ArduinoOTA.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
ArduinoOTA.handle();
|
||||
}
|
||||
}
|
||||
@ -1,74 +0,0 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WiFiMulti.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
#include "credentials.h"
|
||||
|
||||
namespace Wifi {
|
||||
|
||||
WiFiEventHandler stationConnectedHandler;
|
||||
WiFiEventHandler stationDisconnectedHandler;
|
||||
void reconnect();
|
||||
|
||||
Task tWifiReconnect(1 * TASK_MINUTE, TASK_FOREVER, reconnect, &ts);
|
||||
|
||||
String currentSSID;
|
||||
String currentPsk;
|
||||
|
||||
void setup() {
|
||||
stationConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& e) {
|
||||
Serial.println("Reconnected to network.");
|
||||
tWifiConnected.restart();
|
||||
tWifiReconnect.cancel();
|
||||
});
|
||||
|
||||
stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) {
|
||||
Serial.println("Disconnected from network.");
|
||||
tWifiReconnect.restartDelayed();
|
||||
});
|
||||
|
||||
|
||||
ESP8266WiFiMulti wifiMulti;
|
||||
for (uint32_t i = 0; i < sizeof(credentials) / sizeof(WifiCredentials); i++) {
|
||||
wifiMulti.addAP(credentials[i].ssid, credentials[i].password);
|
||||
}
|
||||
|
||||
Serial.println("Connecting to WiFi netowrk.");
|
||||
while (wifiMulti.run() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
}
|
||||
WiFi.setHostname("esp-clock");
|
||||
currentSSID = WiFi.SSID();
|
||||
currentPsk = WiFi.psk();
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
WiFi.forceSleepWake();
|
||||
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
|
||||
Serial.println("Reconnecting to WiFi netowrk...");
|
||||
}
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
Serial.println("Disconnecting WiFi");
|
||||
WiFi.disconnect();
|
||||
WiFi.forceSleepBegin();
|
||||
}
|
||||
|
||||
void printStatus() {
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
|
||||
// print the received signal strength:
|
||||
long rssi = WiFi.RSSI();
|
||||
Serial.print("signal strength (RSSI):");
|
||||
Serial.print(rssi);
|
||||
Serial.println(" dBm");
|
||||
}
|
||||
}
|
||||
@ -20,7 +20,8 @@ lib_deps =
|
||||
jchristensen/Timezone@^1.2.4
|
||||
adafruit/Adafruit Unified Sensor @ ^1.1.4
|
||||
adafruit/Adafruit BME280 Library@^2.2.4
|
||||
https://git.hodos.ro/arduino/ha-mqtt.git@^1.0.0
|
||||
https://git.hodos.ro/libraries/wifi.git@^1.0.1
|
||||
https://git.hodos.ro/libraries/ha-mqtt.git@^1.0.0
|
||||
build_flags = -D WIFI_ALWAYS_ON=1
|
||||
|
||||
[env:laptop_home]
|
||||
|
||||
@ -1,18 +1,21 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
void checkWifiCallback();
|
||||
void onWifiConnected();
|
||||
void onButtonPressed();
|
||||
void onLed();
|
||||
void onButtonCallback();
|
||||
|
||||
#define MQTT_HOST IPAddress(192, 168, 5, 11)
|
||||
#define MQTT_PORT 1883
|
||||
#define MAIN_DEVICE_ID "esp-clock"
|
||||
|
||||
#include <TaskScheduler.h>
|
||||
Scheduler ts;
|
||||
Task tCheckWifi(5 * TASK_MINUTE, TASK_ONCE, checkWifiCallback, &ts);
|
||||
Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts);
|
||||
|
||||
void turnLed(bool on = true) {
|
||||
on ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH);
|
||||
}
|
||||
|
||||
#include "bme.h"
|
||||
#include "ntp_time.h"
|
||||
@ -26,6 +29,7 @@ Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts);
|
||||
Task tButton(TASK_IMMEDIATE, TASK_ONCE, []() {
|
||||
if (Display::tDisplaySensor.isEnabled()) return;
|
||||
Bme::data.readAll();
|
||||
Display::tDisplaySensor.setIterations(DISPLAY_SENSOR_ITERATIONS);
|
||||
Display::tDisplaySensor.restart();
|
||||
}, &ts);
|
||||
Task tLed(TASK_IMMEDIATE, TASK_ONCE, []() {
|
||||
@ -40,7 +44,6 @@ Task tReadBme(TASK_MINUTE, TASK_FOREVER, []() {
|
||||
Devices::publishBme280();
|
||||
if (abs(lastTemp - Bme::data.temp) > 0.2) {
|
||||
lastTemp = Bme::data.temp;
|
||||
Display::tDisplaySensor.setIterations(DISPLAY_SENSOR_ITERATIONS*2);
|
||||
Display::tDisplaySensor.restart();
|
||||
}
|
||||
}, &ts);
|
||||
@ -48,18 +51,32 @@ Task tReadBme(TASK_MINUTE, TASK_FOREVER, []() {
|
||||
void setup() {
|
||||
|
||||
Serial.begin(9600);
|
||||
Serial.println();
|
||||
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
|
||||
Display::setup();
|
||||
Ota::setup();
|
||||
Ota::setup(
|
||||
[] {
|
||||
Display::displayText("load");
|
||||
Mqtt::publishCleanupConfig();
|
||||
delay(2000);
|
||||
Mqtt::disconnect();
|
||||
});
|
||||
Ntp::setup();
|
||||
Bme::setup();
|
||||
Mqtt::setup(&ts);
|
||||
Mqtt::setup(&ts,
|
||||
[] {turnLed(false);},
|
||||
[] {turnLed();}
|
||||
);
|
||||
Devices::setup();
|
||||
|
||||
Wifi::setup();
|
||||
Wifi::setup(ts, []{
|
||||
Ota::tLoop.enable();
|
||||
Mqtt::tReConnect.enable();
|
||||
Ntp::tUpdateTime.enable();
|
||||
});
|
||||
|
||||
pinMode(BUTTON, INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(BUTTON), onButtonPressed, FALLING);
|
||||
@ -72,16 +89,6 @@ void loop() {
|
||||
ts.execute();
|
||||
}
|
||||
|
||||
void onWifiConnected() {
|
||||
Serial.println("Wifi connected event");
|
||||
Wifi::printStatus();
|
||||
Ota::tLoop.enable();
|
||||
Mqtt::tReConnect.enable();
|
||||
if (time_t newTime = Ntp::updateTime()) {
|
||||
Serial.println(asctime(localtime(&newTime)));
|
||||
}
|
||||
}
|
||||
|
||||
void checkWifiCallback() {
|
||||
#if !WIFI_ALWAYS_ON
|
||||
Serial.println("Wifi connection timed out");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user