Minor usermod optimisations.
This commit is contained in:
parent
b56c1b956c
commit
1a520f8782
@ -87,8 +87,6 @@ private:
|
|||||||
static const char _name[];
|
static const char _name[];
|
||||||
static const char _switchOffDelay[];
|
static const char _switchOffDelay[];
|
||||||
static const char _enabled[];
|
static const char _enabled[];
|
||||||
static const char _active[];
|
|
||||||
static const char _inactive[];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return or change if new PIR sensor state is available
|
* return or change if new PIR sensor state is available
|
||||||
@ -126,7 +124,7 @@ private:
|
|||||||
void publishMqtt(const char* state)
|
void publishMqtt(const char* state)
|
||||||
{
|
{
|
||||||
//Check if MQTT Connected, otherwise it will crash the 8266
|
//Check if MQTT Connected, otherwise it will crash the 8266
|
||||||
if (mqtt != nullptr){
|
if (WLED_MQTT_CONNECTED){
|
||||||
char subuf[64];
|
char subuf[64];
|
||||||
strcpy(subuf, mqttDeviceTopic);
|
strcpy(subuf, mqttDeviceTopic);
|
||||||
strcat_P(subuf, PSTR("/motion"));
|
strcat_P(subuf, PSTR("/motion"));
|
||||||
@ -229,12 +227,10 @@ public:
|
|||||||
*/
|
*/
|
||||||
void addToJsonInfo(JsonObject &root)
|
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"];
|
JsonObject user = root["u"];
|
||||||
if (user.isNull())
|
if (user.isNull())
|
||||||
user = root.createNestedObject("u");
|
user = root.createNestedObject("u");
|
||||||
|
/*
|
||||||
JsonArray infoArr = user.createNestedArray(F("<i class=\"icons\"></i> PIR sensor state")); //name
|
JsonArray infoArr = user.createNestedArray(F("<i class=\"icons\"></i> PIR sensor state")); //name
|
||||||
String uiDomString = F("<button class=\"btn infobtn\" onclick=\"requestJson({PIRenabled:");
|
String uiDomString = F("<button class=\"btn infobtn\" onclick=\"requestJson({PIRenabled:");
|
||||||
String sensorStateInfo;
|
String sensorStateInfo;
|
||||||
@ -243,7 +239,7 @@ public:
|
|||||||
if (m_PIRenabled)
|
if (m_PIRenabled)
|
||||||
{
|
{
|
||||||
uiDomString += "false";
|
uiDomString += "false";
|
||||||
sensorStateInfo = (m_PIRsensorPinState != LOW ? FPSTR(_active) : FPSTR(_inactive)); //value
|
sensorStateInfo = (m_PIRsensorPinState != LOW ? FPSTR(F("active")) : FPSTR(F("inactive"))); //value
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -254,18 +250,20 @@ public:
|
|||||||
uiDomString += sensorStateInfo;
|
uiDomString += sensorStateInfo;
|
||||||
uiDomString += F("</button>");
|
uiDomString += F("</button>");
|
||||||
infoArr.add(uiDomString); //value
|
infoArr.add(uiDomString); //value
|
||||||
|
*/
|
||||||
if (m_PIRenabled)
|
if (m_PIRenabled)
|
||||||
{
|
{
|
||||||
//this code adds "u":{"⏲ switch off timer":uiDomString} to the info object
|
JsonArray infoArr = user.createNestedArray(F("PIR switch-off timer after")); //name
|
||||||
uiDomString = F("<i class=\"icons\"></i> switch off timer<span style=\"display:block;padding-left:25px;\">after <input type=\"number\" min=\"1\" max=\"720\" value=\"");
|
String uiDomString = F("<input type=\"number\" min=\"1\" max=\"720\" value=\"");
|
||||||
uiDomString += (m_switchOffDelay / 60000);
|
uiDomString += (m_switchOffDelay / 60000);
|
||||||
uiDomString += F("\" onchange=\"requestJson({PIRoffSec:parseInt(this.value)*60});\">min</span>");
|
uiDomString += F("\" onchange=\"requestJson({PIRoffSec:parseInt(this.value)*60});\">min");
|
||||||
infoArr = user.createNestedArray(uiDomString); //name
|
infoArr.add(uiDomString);
|
||||||
|
|
||||||
// off timer
|
// off timer
|
||||||
if (m_offTimerStart > 0)
|
if (m_offTimerStart > 0)
|
||||||
{
|
{
|
||||||
|
uiDomString = F("<i class=\"icons\"></i>");
|
||||||
|
infoArr = user.createNestedArray(uiDomString); // timer value
|
||||||
uiDomString = "";
|
uiDomString = "";
|
||||||
unsigned int offSeconds = (m_switchOffDelay - (millis() - m_offTimerStart)) / 1000;
|
unsigned int offSeconds = (m_switchOffDelay - (millis() - m_offTimerStart)) / 1000;
|
||||||
if (offSeconds >= 3600)
|
if (offSeconds >= 3600)
|
||||||
@ -290,10 +288,6 @@ public:
|
|||||||
uiDomString += (offSeconds);
|
uiDomString += (offSeconds);
|
||||||
infoArr.add(uiDomString + F("s"));
|
infoArr.add(uiDomString + F("s"));
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
infoArr.add(FPSTR(_inactive));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -340,7 +334,7 @@ public:
|
|||||||
PIRsensorPin = pin;
|
PIRsensorPin = pin;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
if (root[FPSTR(_enabled)] != nullptr) {
|
if (root[FPSTR(_enabled)] != nullptr) {
|
||||||
if (root[FPSTR(_enabled)] && !m_PIRenabled && PIRsensorPin >= 0) {
|
if (root[FPSTR(_enabled)] && !m_PIRenabled && PIRsensorPin >= 0) {
|
||||||
attachInterrupt(digitalPinToInterrupt(PIRsensorPin), ISR_PIRstateChange, CHANGE);
|
attachInterrupt(digitalPinToInterrupt(PIRsensorPin), ISR_PIRstateChange, CHANGE);
|
||||||
@ -350,6 +344,7 @@ public:
|
|||||||
}
|
}
|
||||||
m_PIRenabled = root[FPSTR(_enabled)];
|
m_PIRenabled = root[FPSTR(_enabled)];
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -467,5 +462,3 @@ PIRsensorSwitch *PIRsensorSwitch::PIRsensorSwitchInstance(PIRsensorSwitch *pInst
|
|||||||
const char PIRsensorSwitch::_name[] PROGMEM = "PIRsensorSwitch";
|
const char PIRsensorSwitch::_name[] PROGMEM = "PIRsensorSwitch";
|
||||||
const char PIRsensorSwitch::_switchOffDelay[] PROGMEM = "PIRoffSec";
|
const char PIRsensorSwitch::_switchOffDelay[] PROGMEM = "PIRoffSec";
|
||||||
const char PIRsensorSwitch::_enabled[] PROGMEM = "PIRenabled";
|
const char PIRsensorSwitch::_enabled[] PROGMEM = "PIRenabled";
|
||||||
const char PIRsensorSwitch::_active[] PROGMEM = "active";
|
|
||||||
const char PIRsensorSwitch::_inactive[] PROGMEM = "inactive";
|
|
||||||
|
@ -44,10 +44,10 @@ class UsermodTemperature : public Usermod {
|
|||||||
float temperature = -100; // default to -100, DS18B20 only goes down to -50C
|
float temperature = -100; // default to -100, DS18B20 only goes down to -50C
|
||||||
// indicates requestTemperatures has been called but the sensor measurement is not complete
|
// indicates requestTemperatures has been called but the sensor measurement is not complete
|
||||||
bool waitingForConversion = false;
|
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
|
// allows this library to report to the user how long until the first
|
||||||
// measurement
|
// measurement
|
||||||
bool getTemperatureComplete = false;
|
bool readTemperatureComplete = false;
|
||||||
// flag set at startup if DS18B20 sensor not found, avoids trying to keep getting
|
// flag set at startup if DS18B20 sensor not found, avoids trying to keep getting
|
||||||
// temperature if flashed to a board without a sensor attached
|
// temperature if flashed to a board without a sensor attached
|
||||||
bool disabled = false;
|
bool disabled = false;
|
||||||
@ -84,12 +84,11 @@ class UsermodTemperature : public Usermod {
|
|||||||
DEBUG_PRINTLN(F("Requested temperature."));
|
DEBUG_PRINTLN(F("Requested temperature."));
|
||||||
}
|
}
|
||||||
|
|
||||||
void getTemperature() {
|
void readTemperature() {
|
||||||
temperature = readDallas();
|
temperature = readDallas();
|
||||||
if (!degC) temperature = temperature * 1.8f + 32;
|
|
||||||
lastMeasurement = millis();
|
lastMeasurement = millis();
|
||||||
waitingForConversion = false;
|
waitingForConversion = false;
|
||||||
getTemperatureComplete = true;
|
readTemperatureComplete = true;
|
||||||
DEBUG_PRINTF("Read temperature %2.1f.\n", temperature);
|
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?
|
// 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 */) {
|
if (now - lastTemperaturesRequest >= 100 /* 93.75ms per the datasheet but can be up to 750ms */) {
|
||||||
getTemperature();
|
readTemperature();
|
||||||
|
|
||||||
if (WLED_MQTT_CONNECTED) {
|
if (WLED_MQTT_CONNECTED) {
|
||||||
char subuf[64];
|
char subuf[64];
|
||||||
@ -166,6 +165,8 @@ class UsermodTemperature : public Usermod {
|
|||||||
// reading the sensor
|
// reading the sensor
|
||||||
strcat_P(subuf, PSTR("/temperature"));
|
strcat_P(subuf, PSTR("/temperature"));
|
||||||
mqtt->publish(subuf, 0, true, String(temperature).c_str());
|
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 {
|
} else {
|
||||||
// publish something else to indicate status?
|
// 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.
|
* 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.
|
* 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));
|
JsonArray temp = user.createNestedArray(FPSTR(_name));
|
||||||
//temp.add(F("Loaded."));
|
//temp.add(F("Loaded."));
|
||||||
|
|
||||||
if (!getTemperatureComplete) {
|
if (!readTemperatureComplete) {
|
||||||
// if we haven't read the sensor yet, let the user know
|
// if we haven't read the sensor yet, let the user know
|
||||||
// that we are still waiting for the first measurement
|
// that we are still waiting for the first measurement
|
||||||
temp.add((USERMOD_DALLASTEMPERATURE_FIRST_MEASUREMENT_AT - millis()) / 1000);
|
temp.add((USERMOD_DALLASTEMPERATURE_FIRST_MEASUREMENT_AT - millis()) / 1000);
|
||||||
@ -202,7 +213,7 @@ class UsermodTemperature : public Usermod {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
temp.add(temperature);
|
temp.add(degC ? temperature : (float)temperature * 1.8f + 32);
|
||||||
if (degC) temp.add(F("°C"));
|
if (degC) temp.add(F("°C"));
|
||||||
else temp.add(F("°F"));
|
else temp.add(F("°F"));
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// version code in format yymmddb (b = daily build)
|
// 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
|
//uncomment this if you have a "my_config.h" file you'd like to use
|
||||||
//#define WLED_USE_MY_CONFIG
|
//#define WLED_USE_MY_CONFIG
|
||||||
|
Loading…
Reference in New Issue
Block a user