Moved Homebridge webstat to webUpdater logic. Fixed Windspeed calculation. Added a interval firmware reset functionality.

This commit is contained in:
Kai Lauterbach 2022-05-15 22:34:36 +02:00
parent 116dfa658f
commit 5f0614ce4f
7 changed files with 92 additions and 55 deletions

10
firmware/config.h Executable file → Normal file
View file

@ -24,6 +24,13 @@
#define POWERSAVING_SLEEP_S 600 #define POWERSAVING_SLEEP_S 600
#define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover #define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover
#define ENERGY_SAVING_ITERATIONS 30 #define ENERGY_SAVING_ITERATIONS 30
#define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent
#define RESET_ESP_TIME_INTERVAL_MS 3600000
#define ROTOR_LENGTH_KM 0.000105
#define WIND_SENSOR_MEAS_TIME_S 5.0
#define SEC_TO_HOUR_FACTOR (60.0 * 60.0)
#define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR)
#define BAT_LOW_VOLTAGE 3.6 #define BAT_LOW_VOLTAGE 3.6
#define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5 #define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5
@ -47,6 +54,7 @@
#define BME_CS 10 #define BME_CS 10
#define BME_ADDRESS 0x76 #define BME_ADDRESS 0x76
#define INITIAL_WEBSERVER_TIME 20 #define INITIAL_WEBSERVER_TIME 20
#define WEB_UPDATER_HTTP_PORT 8080
#endif #endif

View file

@ -5,8 +5,8 @@
#include <DNSServer.h> #include <DNSServer.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <WiFiClient.h> // WiFiClient #include <WiFiClient.h> // WiFiClient
#include <WiFiManager.h> // WiFiManager from bib manager #include <WiFiManager.h> // WiFiManager from bib manager
// Project includes // Project includes
#include "config.h" #include "config.h"
@ -19,21 +19,22 @@ const uint8_t VALUES = 8;
float currentSensorData[VALUES] = {nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value")}; float currentSensorData[VALUES] = {nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value")};
float (*sensors[VALUES])() = {}; float (*sensors[VALUES])() = {};
uint16_t update_sensor_cnt = 0; uint16_t update_sensor_cnt = 0;
uint16_t update_webserver_cnt = 0; uint16_t update_webserver_cnt = 0;
WiFiManager wifiManager; WiFiManager wifiManager;
//*************************************************************************// //*************************************************************************//
void debug(String x) { void debug(String x)
{
#ifdef DEBUG #ifdef DEBUG
Serial.println(x); Serial.println(x);
#endif #endif
} }
void setup()
void setup() { {
#if defined(DEBUG) || defined(SERIAL_FEATURE) #if defined(DEBUG) || defined(SERIAL_FEATURE)
Serial.begin(115200); Serial.begin(115200);
@ -60,16 +61,29 @@ void setup() {
// Establish WiFi connection // Establish WiFi connection
String wifiName = "oko-weather-" + DEVICE_NAME; String wifiName = "oko-weather-" + DEVICE_NAME;
wifiManager.setMinimumSignalQuality(16); wifiManager.setMinimumSignalQuality(WIFI_MINIMUM_SIGNAL_QUALITY);
wifiManager.setConnectTimeout(WIFI_AUTOCONNECT_TIMEOUT_S); // the time in seconds to wait for the known wifi connection // the time in seconds to wait for the known wifi connection
wifiManager.setTimeout(WIFI_CONFIG_PORTAL_TIMEOUT_S); // the time in seconds to wait for the user to configure the device wifiManager.setConnectTimeout(WIFI_AUTOCONNECT_TIMEOUT_S);
// the time in seconds to wait for the user to configure the device
wifiManager.setTimeout(WIFI_CONFIG_PORTAL_TIMEOUT_S);
while (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) #ifdef SLEEP_IF_NO_WLAN_CONNECTION
while
#endif
#ifndef SLEEP_IF_NO_WLAN_CONNECTION
if
#endif
(!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF"))
{ {
debug("WiFi connection failed, try again in 5 seconds..."); debug("WiFi connection failed, try again in 5 seconds...");
// If autoconnect to WLAN failed and no client connected, go to deep sleep // If autoconnect to WLAN failed and no client connected, go to deep sleep
//ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT); #ifdef SLEEP_IF_NO_WLAN_CONNECTION
ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
delay(100);
#endif
#ifndef SLEEP_IF_NO_WLAN_CONNECTION
delay(5000); delay(5000);
#endif
} }
debug("Connected!"); debug("Connected!");
@ -125,6 +139,15 @@ void setup() {
//It's magic! leave in //It's magic! leave in
delay(100); delay(100);
#ifdef RESET_ESP_TIMEINTERVAL
// if millis() reached interval (1h) restart ESP
if (millis() >= RESET_ESP_TIME_INTERVAL_MS)
{
// TODO test
ESP.restart();
}
#endif
#ifdef BATTERY_POWERED #ifdef BATTERY_POWERED
debug("battery powered"); debug("battery powered");
_loop(); _loop();

View file

@ -1,30 +0,0 @@
// Port des Web Servers auf 80 setzen
#ifdef HOMEBRIDGE_WEBSTAT
WiFiServer hb_webstat_server(80);
const char *hb_ws_msg_start = "{\"temperature\": ";
const char *hb_ws_msg_mid = "\"humidity\": ";
const char *hb_ws_msg_end = "}";
void hb_webstat_setup()
{
hb_webstat_server.begin()
}
void hb_webstat_send(floar currentSensorDfloar currentSensorData[]ata[])
{
WiFiClient client = server.available();
if (client.available()) {
client.print(hb_ws_msg_start);
client.print(String(currentSensorData[0],2));
client.print(hb_ws_msg_mid);
client.print(hb_ws_msg_end);
}
}
#endif

View file

@ -1,7 +1,9 @@
#include <InfluxDbClient.h> // from bib manager #include <InfluxDbClient.h> // from bib manager
#include "config_user.h"
// Data point // Data point
Point sensor("weatherstation"); Point sensor(DEVICE_NAME);
// Init variables to influxdb config - doesn't talk to database // Init variables to influxdb config - doesn't talk to database
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN); InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);

15
firmware/sensor_wind.ino Executable file → Normal file
View file

@ -1,9 +1,11 @@
#include "config_user.h" #include "config_user.h"
#include "config.h"
int anemometerRotations = 0; unsigned int anemometerRotations = 0;
unsigned long currentTime = 0;
ICACHE_RAM_ATTR void _anemometerInterrupt() { ICACHE_RAM_ATTR void _anemometerInterrupt()
{
anemometerRotations++; anemometerRotations++;
#ifdef DEBUG #ifdef DEBUG
Serial.print("*"); Serial.print("*");
@ -12,12 +14,13 @@ ICACHE_RAM_ATTR void _anemometerInterrupt() {
float wind_speed() { float wind_speed() {
anemometerRotations = 0; anemometerRotations = 0;
currentTime = millis();
int interruptNumber = digitalPinToInterrupt(ANEMOMETER_PIN); int interruptNumber = digitalPinToInterrupt(ANEMOMETER_PIN);
attachInterrupt(interruptNumber, _anemometerInterrupt, RISING); attachInterrupt(interruptNumber, _anemometerInterrupt, RISING);
delay(1000 * 5); delay(1000 * WIND_SENSOR_MEAS_TIME_S); // time to measure
detachInterrupt(interruptNumber); detachInterrupt(interruptNumber);
return (float)anemometerRotations / 5.0 * 2.4; return (float)anemometerRotations * COUNT_TO_KMH;
} }

View file

@ -13,12 +13,22 @@
#include <WiFiClient.h> #include <WiFiClient.h>
#include <WiFiManager.h> // WiFiManager from bib manager #include <WiFiManager.h> // WiFiManager from bib manager
ESP8266WebServer httpServer(8080); #include "config.h"
#include "config_user.h"
ESP8266WebServer httpServer(WEB_UPDATER_HTTP_PORT);
ESP8266HTTPUpdateServer httpUpdater; ESP8266HTTPUpdateServer httpUpdater;
String _webUpdater_ip = "127.0.0.1"; String _webUpdater_ip = "127.0.0.1";
String _webUpdater_dev = "unknown"; String _webUpdater_dev = "unknown";
float _webUpdater_sensValues[VALUES]; float _webUpdater_sensValues[VALUES];
String hb_ws_msg_start = "{";
String hb_ws_msg_temp = "\"temperature\": ";
String hb_ws_msg_humi = "\"humidity\": ";
String hb_ws_msg_light = "\"light\": ";
String hb_ws_msg_windspeed = "\"windspeed\": ";
String hb_ws_msg_end = "}";
void setupWebUpdater(String device, String ip) void setupWebUpdater(String device, String ip)
{ {
@ -30,6 +40,9 @@ void setupWebUpdater(String device, String ip)
httpServer.on("/", showHTMLMain); httpServer.on("/", showHTMLMain);
httpServer.on("/resetWifiManager", resetWifiManager); httpServer.on("/resetWifiManager", resetWifiManager);
#ifdef HOMEBRIDGE_WEBSTAT
httpServer.on("/hbWebstat", hb_webstat_send);
#endif
httpServer.begin(); httpServer.begin();
@ -42,13 +55,15 @@ void doWebUpdater(void)
httpServer.handleClient(); httpServer.handleClient();
} }
void setSensorData(float sensorValues[]) { void setSensorData(float sensorValues[])
{
for (uint8_t i = 0; i < VALUES; i++) { for (uint8_t i = 0; i < VALUES; i++) {
_webUpdater_sensValues[i] = sensorValues[i]; _webUpdater_sensValues[i] = sensorValues[i];
} }
} }
void showHTMLMain(void) { void showHTMLMain(void)
{
String message = "<html><head><title>OKO Weatherstation - " + String(_webUpdater_dev) + "</title>" String message = "<html><head><title>OKO Weatherstation - " + String(_webUpdater_dev) + "</title>"
"<meta http-equiv=\"refresh\" content=\"20\">" "<meta http-equiv=\"refresh\" content=\"20\">"
"</head><body>" "</head><body>"
@ -66,6 +81,20 @@ void showHTMLMain(void) {
httpServer.send(200, "text/html", message); httpServer.send(200, "text/html", message);
} }
void hb_webstat_send(void)
{
httpServer.send(200, "text/html", hb_ws_msg_start +
hb_ws_msg_temp +
String(_webUpdater_sensValues[SENSOR_TEMPERATURE], 2) +
hb_ws_msg_humi +
String(_webUpdater_sensValues[SENSOR_HUMIDITY], 2) +
hb_ws_msg_light +
String(_webUpdater_sensValues[SENSOR_LIGHT], 2) +
hb_ws_msg_windspeed +
String(_webUpdater_sensValues[SENSOR_WINDSPEED], 2) +
hb_ws_msg_end);
}
void resetWifiManager() void resetWifiManager()
{ {
@ -79,9 +108,11 @@ void resetWifiManager()
httpServer.send(200, "text/html", message); httpServer.send(200, "text/html", message);
// Erase WiFi Credentials, enable, compile, flash, disable and reflash. // Erase WiFi Credentials, enable, compile, flash, disable and reflash.
WiFiManager.resetSettings(); WiFiManager wifiManager;
wifiManager.resetSettings();
delay(5000); delay(5000);
// manual reset after restart is required
ESP.restart(); ESP.restart();
} }

View file