UM BH1750 adapted to "MM style", 8266 build fixes

* UM BH1750 adjustments to use new MM features
* UM BH1750 check sensor status before tryig to read new value (avoids blocking LED updates)
* UM temperature: ensure that measurements continue with many LEDs running (strip.isUpdating() will be true all the time)
* all usermods: solved compile problems on 8266
This commit is contained in:
Frank
2023-05-10 20:14:10 +02:00
parent 3aad3a8ff7
commit 50061ddae8
4 changed files with 30 additions and 24 deletions

View File

@@ -48,11 +48,11 @@ private:
bool getLuminanceComplete = false;
// flag set at startup
bool enabled = true;
//bool enabled = true; //WLEDMM not needed as we use global attributes
// strings to reduce flash memory usage (used more than twice)
static const char _name[];
static const char _enabled[];
//static const char _name[]; //WLEDMM not needed as we use global attributes
//static const char _enabled[]; //WLEDMM not needed as we use global attributes
static const char _maxReadInterval[];
static const char _minReadInterval[];
static const char _offset[];
@@ -76,7 +76,7 @@ private:
bool sensorFound = false;
// Home Assistant and MQTT
String mqttLuminanceTopic = F("");
String mqttLuminanceTopic = FPSTR("");
bool mqttInitialized = false;
bool HomeAssistantDiscovery = true; // Publish Home Assistant Discovery messages
@@ -132,6 +132,8 @@ private:
}
public:
Usermod_BH1750(const char *name, bool enabled):Usermod(name, enabled) {} //WLEDMM
void setup()
{
#if 0
@@ -157,22 +159,22 @@ public:
if (!pinManager.joinWire()) { // WLEDMM - this allocates global I2C pins, then starts Wire - if not started previously
sensorFound = false;
//enabled = false;
USER_PRINTLN(F("BH1750: failed to join I2C bus."));
USER_PRINTLN(F("[BH1750]: failed to join I2C bus."));
return;
}
sensorFound = lightMeter.begin();
if (sensorFound) { USER_PRINTLN(F("BH1750 sensor found.")); }
else{ USER_PRINTLN(F("BH1750 sensor not found.")); }
sensorFound = lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE); // WLEDMM set mode explicitly
if (sensorFound) { USER_PRINTLN(F("[BH1750] sensor found.")); }
else{ USER_PRINTLN(F("[BH1750] sensor not found.")); }
initDone = true;
}
void loop()
{
if ((!enabled) || strip.isUpdating())
if (!sensorFound || !initDone) return; // WLEDMM bugfix
if ((!enabled) || (strip.isUpdating() && (millis() - lastMeasurement < 450))) // WLEDMM be nice, but not too nice
return;
if (!sensorFound) return; // WLEDMM bugfix
unsigned long now = millis();
// check to see if we are due for taking a measurement
@@ -184,10 +186,12 @@ public:
}
bool shouldUpdate = now - lastSend > maxReadingInterval;
float lux = lightMeter.readLightLevel();
lastMeasurement = millis();
getLuminanceComplete = true;
float lux = lastLux;
if (lightMeter.measurementReady()) { //WLEDMM do not block in case the sensor is still busy
lux = lightMeter.readLightLevel();
lastMeasurement = millis();
getLuminanceComplete = true;
}
if (shouldUpdate || checkBoundSensor(lux, lastLux, offset))
{
@@ -255,7 +259,7 @@ public:
{
// we add JSON object.
JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname
top[FPSTR(_enabled)] = enabled;
top[F("enabled")] = enabled;
top[FPSTR(_maxReadInterval)] = maxReadingInterval;
top[FPSTR(_minReadInterval)] = minReadingInterval;
top[FPSTR(_HomeAssistantDiscovery)] = HomeAssistantDiscovery;
@@ -269,7 +273,7 @@ public:
// top[F("help4Pins")] = F("SCL,SDA"); // help for Settings page
DEBUG_PRINTLN(F("BH1750 config saved."));
DEBUG_PRINTLN(F("[BH1750] config saved."));
}
// called before setup() to populate properties from values stored in cfg.json
@@ -283,13 +287,13 @@ public:
if (top.isNull())
{
DEBUG_PRINT(FPSTR(_name));
DEBUG_PRINT(F("BH1750"));
DEBUG_PRINT(F("[BH1750]"));
DEBUG_PRINTLN(F(": No config found. (Using defaults.)"));
return false;
}
bool configComplete = !top.isNull();
configComplete &= getJsonValue(top[FPSTR(_enabled)], enabled, false);
configComplete &= getJsonValue(top[F("enabled")], enabled, false);
configComplete &= getJsonValue(top[FPSTR(_maxReadInterval)], maxReadingInterval, 10000); //ms
configComplete &= getJsonValue(top[FPSTR(_minReadInterval)], minReadingInterval, 500); //ms
configComplete &= getJsonValue(top[FPSTR(_HomeAssistantDiscovery)], HomeAssistantDiscovery, false);
@@ -335,8 +339,8 @@ public:
};
// strings to reduce flash memory usage (used more than twice)
const char Usermod_BH1750::_name[] PROGMEM = "BH1750";
const char Usermod_BH1750::_enabled[] PROGMEM = "enabled";
//const char Usermod_BH1750::_name[] PROGMEM = "BH1750"; //WLEDMM not needed as we use global attributes
//const char Usermod_BH1750::_enabled[] PROGMEM = "enabled"; //WLEDMM not needed as we use global attributes
const char Usermod_BH1750::_maxReadInterval[] PROGMEM = "max-read-interval-ms";
const char Usermod_BH1750::_minReadInterval[] PROGMEM = "min-read-interval-ms";
const char Usermod_BH1750::_HomeAssistantDiscovery[] PROGMEM = "HomeAssistantDiscoveryLux";

View File

@@ -194,7 +194,9 @@ class UsermodTemperature : public Usermod {
}
void loop() {
if (!enabled || !sensorFound || strip.isUpdating()) return;
unsigned long last_runtime = 0; // WLEDMM ensure that strip.isUpdating() will not block longer that 4000ms
if (!enabled || !sensorFound || (strip.isUpdating() && (millis()-last_runtime < 4000))) return; // WLEDMM be nice, but not too nice
last_runtime = millis();
static uint8_t errorCount = 0;
unsigned long now = millis();

View File

@@ -390,7 +390,7 @@ void ShtUsermod::addToConfig(JsonObject &root)
{
JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname
top[FPSTR("enabled")] = enabled;
top[F("enabled")] = enabled;
top[FPSTR(_shtType)] = shtType;
top[FPSTR(_unitOfTemp)] = unitOfTemp;
top[FPSTR(_haMqttDiscovery)] = haMqttDiscovery;
@@ -421,7 +421,7 @@ bool ShtUsermod::readFromConfig(JsonObject &root)
byte oldUnitOfTemp = unitOfTemp;
bool oldHaMqttDiscovery = haMqttDiscovery;
getJsonValue(top[FPSTR("enabled")], enabled);
getJsonValue(top[F("enabled")], enabled);
getJsonValue(top[FPSTR(_shtType)], shtType);
getJsonValue(top[FPSTR(_unitOfTemp)], unitOfTemp);
getJsonValue(top[FPSTR(_haMqttDiscovery)], haMqttDiscovery);

View File

@@ -238,7 +238,7 @@ void registerUsermods()
#endif
#ifdef USERMOD_BH1750
usermods.add(new Usermod_BH1750());
usermods.add(new Usermod_BH1750("BH1750", false));
#endif
#ifdef USERMOD_BME280