Minor usermod optimisations.

This commit is contained in:
Blaz Kristan 2021-04-29 22:39:08 +02:00
parent b56c1b956c
commit 1a520f8782
3 changed files with 33 additions and 29 deletions

View File

@ -87,8 +87,6 @@ private:
static const char _name[];
static const char _switchOffDelay[];
static const char _enabled[];
static const char _active[];
static const char _inactive[];
/**
* return or change if new PIR sensor state is available
@ -126,7 +124,7 @@ private:
void publishMqtt(const char* state)
{
//Check if MQTT Connected, otherwise it will crash the 8266
if (mqtt != nullptr){
if (WLED_MQTT_CONNECTED){
char subuf[64];
strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/motion"));
@ -229,12 +227,10 @@ public:
*/
void addToJsonInfo(JsonObject &root)
{
//this code adds "u":{"⏲ PIR sensor state":uiDomString} to the info object
// the value contains a button to toggle the sensor enabled/disabled
JsonObject user = root["u"];
if (user.isNull())
user = root.createNestedObject("u");
/*
JsonArray infoArr = user.createNestedArray(F("<i class=\"icons\">&#xe08f;</i> PIR sensor state")); //name
String uiDomString = F("<button class=\"btn infobtn\" onclick=\"requestJson({PIRenabled:");
String sensorStateInfo;
@ -243,7 +239,7 @@ public:
if (m_PIRenabled)
{
uiDomString += "false";
sensorStateInfo = (m_PIRsensorPinState != LOW ? FPSTR(_active) : FPSTR(_inactive)); //value
sensorStateInfo = (m_PIRsensorPinState != LOW ? FPSTR(F("active")) : FPSTR(F("inactive"))); //value
}
else
{
@ -254,18 +250,20 @@ public:
uiDomString += sensorStateInfo;
uiDomString += F("</button>");
infoArr.add(uiDomString); //value
*/
if (m_PIRenabled)
{
//this code adds "u":{"&#x23F2; switch off timer":uiDomString} to the info object
uiDomString = F("<i class=\"icons\">&#xe325;</i> switch off timer<span style=\"display:block;padding-left:25px;\">after <input type=\"number\" min=\"1\" max=\"720\" value=\"");
JsonArray infoArr = user.createNestedArray(F("PIR switch-off timer after")); //name
String uiDomString = F("<input type=\"number\" min=\"1\" max=\"720\" value=\"");
uiDomString += (m_switchOffDelay / 60000);
uiDomString += F("\" onchange=\"requestJson({PIRoffSec:parseInt(this.value)*60});\">min</span>");
infoArr = user.createNestedArray(uiDomString); //name
uiDomString += F("\" onchange=\"requestJson({PIRoffSec:parseInt(this.value)*60});\">min");
infoArr.add(uiDomString);
// off timer
if (m_offTimerStart > 0)
{
uiDomString = F("<i class=\"icons\">&#xe325;</i>");
infoArr = user.createNestedArray(uiDomString); // timer value
uiDomString = "";
unsigned int offSeconds = (m_switchOffDelay - (millis() - m_offTimerStart)) / 1000;
if (offSeconds >= 3600)
@ -290,10 +288,6 @@ public:
uiDomString += (offSeconds);
infoArr.add(uiDomString + F("s"));
}
else
{
infoArr.add(FPSTR(_inactive));
}
}
}
@ -340,7 +334,7 @@ public:
PIRsensorPin = pin;
}
}
*/
if (root[FPSTR(_enabled)] != nullptr) {
if (root[FPSTR(_enabled)] && !m_PIRenabled && PIRsensorPin >= 0) {
attachInterrupt(digitalPinToInterrupt(PIRsensorPin), ISR_PIRstateChange, CHANGE);
@ -350,6 +344,7 @@ public:
}
m_PIRenabled = root[FPSTR(_enabled)];
}
*/
}
/**
@ -467,5 +462,3 @@ PIRsensorSwitch *PIRsensorSwitch::PIRsensorSwitchInstance(PIRsensorSwitch *pInst
const char PIRsensorSwitch::_name[] PROGMEM = "PIRsensorSwitch";
const char PIRsensorSwitch::_switchOffDelay[] PROGMEM = "PIRoffSec";
const char PIRsensorSwitch::_enabled[] PROGMEM = "PIRenabled";
const char PIRsensorSwitch::_active[] PROGMEM = "active";
const char PIRsensorSwitch::_inactive[] PROGMEM = "inactive";

View File

@ -44,10 +44,10 @@ class UsermodTemperature : public Usermod {
float temperature = -100; // default to -100, DS18B20 only goes down to -50C
// indicates requestTemperatures has been called but the sensor measurement is not complete
bool waitingForConversion = false;
// flag to indicate we have finished the first getTemperature call
// flag to indicate we have finished the first readTemperature call
// allows this library to report to the user how long until the first
// measurement
bool getTemperatureComplete = false;
bool readTemperatureComplete = false;
// flag set at startup if DS18B20 sensor not found, avoids trying to keep getting
// temperature if flashed to a board without a sensor attached
bool disabled = false;
@ -84,12 +84,11 @@ class UsermodTemperature : public Usermod {
DEBUG_PRINTLN(F("Requested temperature."));
}
void getTemperature() {
void readTemperature() {
temperature = readDallas();
if (!degC) temperature = temperature * 1.8f + 32;
lastMeasurement = millis();
waitingForConversion = false;
getTemperatureComplete = true;
readTemperatureComplete = true;
DEBUG_PRINTF("Read temperature %2.1f.\n", temperature);
}
@ -154,8 +153,8 @@ class UsermodTemperature : public Usermod {
}
// we were waiting for a conversion to complete, have we waited log enough?
if (now - lastTemperaturesRequest >= 800 /* 93.75ms per the datasheet but can be up to 750ms */) {
getTemperature();
if (now - lastTemperaturesRequest >= 100 /* 93.75ms per the datasheet but can be up to 750ms */) {
readTemperature();
if (WLED_MQTT_CONNECTED) {
char subuf[64];
@ -166,6 +165,8 @@ class UsermodTemperature : public Usermod {
// reading the sensor
strcat_P(subuf, PSTR("/temperature"));
mqtt->publish(subuf, 0, true, String(temperature).c_str());
strcat_P(subuf, PSTR("/temperature_f"));
mqtt->publish(subuf, 0, true, String((float)temperature * 1.8f + 32).c_str());
} else {
// publish something else to indicate status?
}
@ -173,6 +174,16 @@ class UsermodTemperature : public Usermod {
}
}
/*
* API calls te enable data exchange between WLED modules
*/
inline float getTemperatureC() {
return (float)temperature;
}
inline float getTemperatureF() {
return (float)temperature * 1.8f + 32;
}
/*
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
* Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI.
@ -188,7 +199,7 @@ class UsermodTemperature : public Usermod {
JsonArray temp = user.createNestedArray(FPSTR(_name));
//temp.add(F("Loaded."));
if (!getTemperatureComplete) {
if (!readTemperatureComplete) {
// if we haven't read the sensor yet, let the user know
// that we are still waiting for the first measurement
temp.add((USERMOD_DALLASTEMPERATURE_FIRST_MEASUREMENT_AT - millis()) / 1000);
@ -202,7 +213,7 @@ class UsermodTemperature : public Usermod {
return;
}
temp.add(temperature);
temp.add(degC ? temperature : (float)temperature * 1.8f + 32);
if (degC) temp.add(F("°C"));
else temp.add(F("°F"));
}

View File

@ -8,7 +8,7 @@
*/
// version code in format yymmddb (b = daily build)
#define VERSION 2104291
#define VERSION 2104292
//uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG