usermod updates: RTC, BH1750
RTC: smarter rules for updating RTC time BH1750 (light meter): allow to use without MQTT; updates for MM specific I2C handling.
This commit is contained in:
@@ -2,13 +2,12 @@
|
|||||||
// #warning **** Included USERMOD_BH1750 ****
|
// #warning **** Included USERMOD_BH1750 ****
|
||||||
|
|
||||||
#ifndef WLED_ENABLE_MQTT
|
#ifndef WLED_ENABLE_MQTT
|
||||||
#error "This user mod requires MQTT to be enabled."
|
#warning "This user mod expects MQTT to be enabled."
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <Arduino.h> // WLEDMM: make sure that I2C drivers have the "right" Wire Object
|
#include <Arduino.h> // WLEDMM: make sure that I2C drivers have the "right" Wire Object
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
#include "wled.h"
|
#include "wled.h"
|
||||||
#include <BH1750.h>
|
#include <BH1750.h>
|
||||||
@@ -20,7 +19,8 @@
|
|||||||
|
|
||||||
// the min frequency to check photoresistor, 500 ms
|
// the min frequency to check photoresistor, 500 ms
|
||||||
#ifndef USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL
|
#ifndef USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL
|
||||||
#define USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL 500
|
//#define USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL 500
|
||||||
|
#define USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL 2500 // WLEDMM this makes more sense
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// how many seconds after boot to take first measurement, 10 seconds
|
// how many seconds after boot to take first measurement, 10 seconds
|
||||||
@@ -30,7 +30,7 @@
|
|||||||
|
|
||||||
// only report if differance grater than offset value
|
// only report if differance grater than offset value
|
||||||
#ifndef USERMOD_BH1750_OFFSET_VALUE
|
#ifndef USERMOD_BH1750_OFFSET_VALUE
|
||||||
#define USERMOD_BH1750_OFFSET_VALUE 1
|
#define USERMOD_BH1750_OFFSET_VALUE 2 // WLEDMM this makes more sense
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class Usermod_BH1750 : public Usermod
|
class Usermod_BH1750 : public Usermod
|
||||||
@@ -91,14 +91,17 @@ private:
|
|||||||
// set up Home Assistant discovery entries
|
// set up Home Assistant discovery entries
|
||||||
void _mqttInitialize()
|
void _mqttInitialize()
|
||||||
{
|
{
|
||||||
|
#ifdef WLED_ENABLED_MQTT
|
||||||
mqttLuminanceTopic = String(mqttDeviceTopic) + F("/brightness");
|
mqttLuminanceTopic = String(mqttDeviceTopic) + F("/brightness");
|
||||||
|
|
||||||
if (HomeAssistantDiscovery) _createMqttSensor(F("Brightness"), mqttLuminanceTopic, F("Illuminance"), F(" lx"));
|
if (HomeAssistantDiscovery) _createMqttSensor(F("Brightness"), mqttLuminanceTopic, F("Illuminance"), F(" lx"));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create an MQTT Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop.
|
// Create an MQTT Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop.
|
||||||
void _createMqttSensor(const String &name, const String &topic, const String &deviceClass, const String &unitOfMeasurement)
|
void _createMqttSensor(const String &name, const String &topic, const String &deviceClass, const String &unitOfMeasurement)
|
||||||
{
|
{
|
||||||
|
#ifdef WLED_ENABLED_MQTT
|
||||||
String t = String(F("homeassistant/sensor/")) + mqttClientID + F("/") + name + F("/config");
|
String t = String(F("homeassistant/sensor/")) + mqttClientID + F("/") + name + F("/config");
|
||||||
|
|
||||||
StaticJsonDocument<600> doc;
|
StaticJsonDocument<600> doc;
|
||||||
@@ -125,11 +128,13 @@ private:
|
|||||||
DEBUG_PRINTLN(temp);
|
DEBUG_PRINTLN(temp);
|
||||||
|
|
||||||
mqtt->publish(t.c_str(), 0, true, temp.c_str());
|
mqtt->publish(t.c_str(), 0, true, temp.c_str());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
#if 0
|
||||||
bool HW_Pins_Used = (ioPin[0]==i2c_scl && ioPin[1]==i2c_sda); // note whether architecture-based hardware SCL/SDA pins used
|
bool HW_Pins_Used = (ioPin[0]==i2c_scl && ioPin[1]==i2c_sda); // note whether architecture-based hardware SCL/SDA pins used
|
||||||
PinOwner po = PinOwner::UM_BH1750; // defaults to being pinowner for SCL/SDA pins
|
PinOwner po = PinOwner::UM_BH1750; // defaults to being pinowner for SCL/SDA pins
|
||||||
if (HW_Pins_Used) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins
|
if (HW_Pins_Used) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins
|
||||||
@@ -147,13 +152,18 @@ public:
|
|||||||
#else
|
#else
|
||||||
//Wire.begin(); // WLEDMM - i2c pins on 8266 are fixed.
|
//Wire.begin(); // WLEDMM - i2c pins on 8266 are fixed.
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
if (!enabled) return;
|
||||||
if (!pinManager.joinWire()) { // WLEDMM - this allocates global I2C pins, then starts Wire - if not started previously
|
if (!pinManager.joinWire()) { // WLEDMM - this allocates global I2C pins, then starts Wire - if not started previously
|
||||||
sensorFound = false;
|
sensorFound = false;
|
||||||
enabled = false;
|
//enabled = false;
|
||||||
|
USER_PRINTLN(F("BH1750: failed to join I2C bus."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
sensorFound = lightMeter.begin();
|
sensorFound = lightMeter.begin();
|
||||||
|
if (sensorFound) { USER_PRINTLN(F("BH1750 sensor found.")); }
|
||||||
|
else{ USER_PRINTLN(F("BH1750 sensor not found.")); }
|
||||||
initDone = true;
|
initDone = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,7 +236,7 @@ public:
|
|||||||
lux_json.add(F(" sec until read"));
|
lux_json.add(F(" sec until read"));
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
lux_json.add(lastLux);
|
lux_json.add(round(lastLux)); // WLEDMM
|
||||||
lux_json.add(F(" lx"));
|
lux_json.add(F(" lx"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -295,6 +305,7 @@ public:
|
|||||||
} else {
|
} else {
|
||||||
DEBUG_PRINTLN(F(" config (re)loaded."));
|
DEBUG_PRINTLN(F(" config (re)loaded."));
|
||||||
// changing parameters from settings page
|
// changing parameters from settings page
|
||||||
|
#if 0
|
||||||
bool pinsChanged = false;
|
bool pinsChanged = false;
|
||||||
for (byte i=0; i<2; i++) if (ioPin[i] != newPin[i]) { pinsChanged = true; break; } // check if any pins changed
|
for (byte i=0; i<2; i++) if (ioPin[i] != newPin[i]) { pinsChanged = true; break; } // check if any pins changed
|
||||||
if (pinsChanged) { //if pins changed, deallocate old pins and allocate new ones
|
if (pinsChanged) { //if pins changed, deallocate old pins and allocate new ones
|
||||||
@@ -305,6 +316,9 @@ public:
|
|||||||
for (byte i=0; i<2; i++) ioPin[i] = newPin[i];
|
for (byte i=0; i<2; i++) ioPin[i] = newPin[i];
|
||||||
setup();
|
setup();
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
if (enabled && !sensorFound) setup();
|
||||||
|
#endif
|
||||||
// use "return !top["newestParameter"].isNull();" when updating Usermod with new features
|
// use "return !top["newestParameter"].isNull();" when updating Usermod with new features
|
||||||
return !top[F("pin")].isNull();
|
return !top[F("pin")].isNull();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
#include "src/dependencies/time/DS1307RTC.h"
|
#include "src/dependencies/time/DS1307RTC.h"
|
||||||
#include "wled.h"
|
#include "wled.h"
|
||||||
|
|
||||||
|
#define RTC_DELTA 2 // only modify RTC time if delta exceeds this number of seconds
|
||||||
|
|
||||||
//Connect DS1307 to standard I2C pins (ESP32: GPIO 21 (SDA)/GPIO 22 (SCL))
|
//Connect DS1307 to standard I2C pins (ESP32: GPIO 21 (SDA)/GPIO 22 (SCL))
|
||||||
|
|
||||||
class RTCUsermod : public Usermod {
|
class RTCUsermod : public Usermod {
|
||||||
@@ -36,6 +38,7 @@ class RTCUsermod : public Usermod {
|
|||||||
if (rtcTime) {
|
if (rtcTime) {
|
||||||
toki.setTime(rtcTime,TOKI_NO_MS_ACCURACY,TOKI_TS_RTC);
|
toki.setTime(rtcTime,TOKI_NO_MS_ACCURACY,TOKI_TS_RTC);
|
||||||
updateLocalTime();
|
updateLocalTime();
|
||||||
|
USER_PRINTLN(F("Localtime updated from RTC."));
|
||||||
} else {
|
} else {
|
||||||
if (!RTC.chipPresent()) disabled = true; //don't waste time if H/W error
|
if (!RTC.chipPresent()) disabled = true; //don't waste time if H/W error
|
||||||
}
|
}
|
||||||
@@ -45,7 +48,24 @@ class RTCUsermod : public Usermod {
|
|||||||
if (strip.isUpdating()) return;
|
if (strip.isUpdating()) return;
|
||||||
if (!disabled && toki.isTick()) {
|
if (!disabled && toki.isTick()) {
|
||||||
time_t t = toki.second();
|
time_t t = toki.second();
|
||||||
if (abs(t - RTC.get())> 2) RTC.set(t); //set RTC to NTP/UI-provided value - WLEDMM allow up to 3 sec deviation
|
|
||||||
|
if (abs(t - RTC.get())> RTC_DELTA) { // WLEDMM only consider time diffs > 2 seconds
|
||||||
|
if ( (toki.getTimeSource() == TOKI_TS_NTP)
|
||||||
|
||( (toki.getTimeSource() != TOKI_TS_NONE) && (toki.getTimeSource() != TOKI_TS_RTC)
|
||||||
|
&& (toki.getTimeSource() != TOKI_TS_BAD) && (toki.getTimeSource() != TOKI_TS_UDP_SEC) && (toki.getTimeSource() != TOKI_TS_UDP)))
|
||||||
|
{ // WLEMM update RTC if we have a reliable time source
|
||||||
|
RTC.set(t); //set RTC to NTP/UI-provided value - WLEDMM allow up to 3 sec deviation
|
||||||
|
USER_PRINTLN(F("RTC updated using localtime."));
|
||||||
|
} else {
|
||||||
|
// WLEDMM if no reliable time -> update from RTC
|
||||||
|
time_t rtcTime = RTC.get();
|
||||||
|
if (rtcTime) {
|
||||||
|
toki.setTime(rtcTime,TOKI_NO_MS_ACCURACY,TOKI_TS_RTC);
|
||||||
|
updateLocalTime();
|
||||||
|
USER_PRINTLN(F("Localtime updated from RTC."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user