2020-05-28 02:20:02 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "wled.h"
|
2021-04-24 22:24:14 +02:00
|
|
|
#include "OneWire.h"
|
2020-05-28 02:20:02 +02:00
|
|
|
|
2021-02-25 09:54:10 +01:00
|
|
|
//Pin defaults for QuinLed Dig-Uno if not overriden
|
2021-01-17 15:00:14 +01:00
|
|
|
#ifndef TEMPERATURE_PIN
|
2021-02-25 09:54:10 +01:00
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
#define TEMPERATURE_PIN 18
|
|
|
|
#else //ESP8266 boards
|
|
|
|
#define TEMPERATURE_PIN 14
|
|
|
|
#endif
|
2021-01-17 15:00:14 +01:00
|
|
|
#endif
|
2020-05-28 02:20:02 +02:00
|
|
|
|
2020-09-13 19:26:27 +02:00
|
|
|
// the frequency to check temperature, 1 minute
|
|
|
|
#ifndef USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL
|
|
|
|
#define USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL 60000
|
|
|
|
#endif
|
2020-05-28 02:20:02 +02:00
|
|
|
|
|
|
|
class UsermodTemperature : public Usermod {
|
2021-04-11 16:47:53 +02:00
|
|
|
|
2020-05-28 02:20:02 +02:00
|
|
|
private:
|
2021-04-11 16:47:53 +02:00
|
|
|
|
|
|
|
bool initDone = false;
|
2021-02-25 09:54:10 +01:00
|
|
|
OneWire *oneWire;
|
|
|
|
// GPIO pin used for sensor (with a default compile-time fallback)
|
|
|
|
int8_t temperaturePin = TEMPERATURE_PIN;
|
|
|
|
// measurement unit (true==°C, false==°F)
|
|
|
|
bool degC = true;
|
2021-05-26 16:11:48 +02:00
|
|
|
// using parasite power on the sensor
|
|
|
|
bool parasite = false;
|
|
|
|
// how often do we read from sensor?
|
2021-04-17 17:04:36 +02:00
|
|
|
unsigned long readingInterval = USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL;
|
2020-09-13 19:26:27 +02:00
|
|
|
// set last reading as "40 sec before boot", so first reading is taken after 20 sec
|
2021-05-25 23:00:21 +02:00
|
|
|
unsigned long lastMeasurement = UINT32_MAX - USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL;
|
2020-09-13 19:26:27 +02:00
|
|
|
// last time requestTemperatures was called
|
|
|
|
// used to determine when we can read the sensors temperature
|
|
|
|
// we have to wait at least 93.75 ms after requestTemperatures() is called
|
|
|
|
unsigned long lastTemperaturesRequest;
|
2021-12-15 19:34:06 +01:00
|
|
|
float temperature = -100.0f; // default to -100, DS18B20 only goes down to -50C
|
2020-09-13 19:26:27 +02:00
|
|
|
// indicates requestTemperatures has been called but the sensor measurement is not complete
|
|
|
|
bool waitingForConversion = 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
|
2021-12-15 19:34:06 +01:00
|
|
|
byte sensorFound;
|
2021-07-04 14:23:53 +02:00
|
|
|
|
2021-06-27 12:15:35 +02:00
|
|
|
bool enabled = true;
|
2020-09-13 19:26:27 +02:00
|
|
|
|
2021-04-22 22:34:43 +02:00
|
|
|
// strings to reduce flash memory usage (used more than twice)
|
|
|
|
static const char _name[];
|
|
|
|
static const char _enabled[];
|
|
|
|
static const char _readInterval[];
|
2021-05-26 16:11:48 +02:00
|
|
|
static const char _parasite[];
|
2021-04-22 22:34:43 +02:00
|
|
|
|
2021-04-29 17:44:31 +02:00
|
|
|
//Dallas sensor quick (& dirty) reading. Credit to - Author: Peter Scargill, August 17th, 2013
|
2021-05-26 16:11:48 +02:00
|
|
|
float readDallas() {
|
2021-12-16 10:47:56 +01:00
|
|
|
byte data[9];
|
2021-05-26 16:11:48 +02:00
|
|
|
int16_t result; // raw data from sensor
|
2021-12-19 12:05:28 +01:00
|
|
|
float retVal = -127.0f;
|
|
|
|
if (oneWire->reset()) { // if reset() fails there are no OneWire devices
|
|
|
|
oneWire->skip(); // skip ROM
|
|
|
|
oneWire->write(0xBE); // read (temperature) from EEPROM
|
2021-12-19 16:50:55 +01:00
|
|
|
delayMicroseconds(150);
|
|
|
|
oneWire->read_bytes(data, 9); // first 2 bytes contain temperature
|
|
|
|
#ifdef WLED_DEBUG
|
|
|
|
if (OneWire::crc8(data,8) != data[8]) {
|
|
|
|
DEBUG_PRINTLN(F("CRC error reading temperature."));
|
|
|
|
for (byte i=0; i < 9; i++) DEBUG_PRINTF("0x%02X ", data[i]);
|
|
|
|
DEBUG_PRINT(F(" => "));
|
|
|
|
DEBUG_PRINTF("0x%02X\n", OneWire::crc8(data,8));
|
|
|
|
}
|
|
|
|
#endif
|
2021-12-19 12:05:28 +01:00
|
|
|
switch(sensorFound) {
|
|
|
|
case 0x10: // DS18S20 has 9-bit precision
|
|
|
|
result = (data[1] << 8) | data[0];
|
|
|
|
retVal = float(result) * 0.5f;
|
|
|
|
break;
|
|
|
|
case 0x22: // DS18B20
|
|
|
|
case 0x28: // DS1822
|
|
|
|
case 0x3B: // DS1825
|
|
|
|
case 0x42: // DS28EA00
|
|
|
|
result = (data[1]<<4) | (data[0]>>4); // we only need whole part, we will add fraction when returning
|
|
|
|
if (data[1] & 0x80) result |= 0xF000; // fix negative value
|
|
|
|
retVal = float(result) + ((data[0] & 0x08) ? 0.5f : 0.0f);
|
|
|
|
break;
|
|
|
|
}
|
2021-12-15 19:34:06 +01:00
|
|
|
}
|
2021-12-19 12:05:28 +01:00
|
|
|
return retVal;
|
2021-04-17 17:04:36 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 19:26:27 +02:00
|
|
|
void requestTemperatures() {
|
2021-12-19 12:05:28 +01:00
|
|
|
DEBUG_PRINTLN(F("Requesting temperature."));
|
|
|
|
oneWire->reset();
|
|
|
|
oneWire->skip(); // skip ROM
|
|
|
|
oneWire->write(0x44,parasite); // request new temperature reading (TODO: parasite would need special handling)
|
2021-04-17 17:04:36 +02:00
|
|
|
lastTemperaturesRequest = millis();
|
|
|
|
waitingForConversion = true;
|
2020-09-13 19:26:27 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 22:39:08 +02:00
|
|
|
void readTemperature() {
|
2021-04-29 17:44:31 +02:00
|
|
|
temperature = readDallas();
|
2020-09-13 19:26:27 +02:00
|
|
|
lastMeasurement = millis();
|
|
|
|
waitingForConversion = false;
|
2021-08-24 21:35:47 +02:00
|
|
|
//DEBUG_PRINTF("Read temperature %2.1f.\n", temperature); // does not work properly on 8266
|
|
|
|
DEBUG_PRINT(F("Read temperature "));
|
|
|
|
DEBUG_PRINTLN(temperature);
|
2020-05-28 02:20:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 17:44:31 +02:00
|
|
|
bool findSensor() {
|
|
|
|
DEBUG_PRINTLN(F("Searching for sensor..."));
|
|
|
|
uint8_t deviceAddress[8] = {0,0,0,0,0,0,0,0};
|
|
|
|
// find out if we have DS18xxx sensor attached
|
|
|
|
oneWire->reset_search();
|
2021-07-04 14:23:53 +02:00
|
|
|
delay(10);
|
2021-04-29 17:44:31 +02:00
|
|
|
while (oneWire->search(deviceAddress)) {
|
2021-07-04 14:23:53 +02:00
|
|
|
DEBUG_PRINTLN(F("Found something..."));
|
2021-04-29 17:44:31 +02:00
|
|
|
if (oneWire->crc8(deviceAddress, 7) == deviceAddress[7]) {
|
|
|
|
switch (deviceAddress[0]) {
|
|
|
|
case 0x10: // DS18S20
|
|
|
|
case 0x22: // DS18B20
|
|
|
|
case 0x28: // DS1822
|
|
|
|
case 0x3B: // DS1825
|
|
|
|
case 0x42: // DS28EA00
|
|
|
|
DEBUG_PRINTLN(F("Sensor found."));
|
2021-12-15 19:34:06 +01:00
|
|
|
sensorFound = deviceAddress[0];
|
2021-12-19 16:50:55 +01:00
|
|
|
DEBUG_PRINTF("0x%02X\n", sensorFound);
|
2021-04-29 17:44:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-13 19:26:27 +02:00
|
|
|
public:
|
|
|
|
|
2020-05-28 02:20:02 +02:00
|
|
|
void setup() {
|
2021-04-29 17:44:31 +02:00
|
|
|
int retries = 10;
|
2021-12-15 19:34:06 +01:00
|
|
|
sensorFound = 0;
|
2021-07-03 21:27:06 +02:00
|
|
|
if (enabled) {
|
|
|
|
// config says we are enabled
|
|
|
|
DEBUG_PRINTLN(F("Allocating temperature pin..."));
|
|
|
|
// pin retrieved from cfg.json (readFromConfig()) prior to running setup()
|
2021-08-23 14:14:48 +02:00
|
|
|
if (temperaturePin >= 0 && pinManager.allocatePin(temperaturePin, true, PinOwner::UM_Temperature)) {
|
2021-04-29 17:44:31 +02:00
|
|
|
oneWire = new OneWire(temperaturePin);
|
2021-12-15 19:34:06 +01:00
|
|
|
if (oneWire->reset()) {
|
|
|
|
while (!findSensor() && retries--) {
|
2021-08-23 14:14:48 +02:00
|
|
|
delay(25); // try to find sensor
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 21:27:06 +02:00
|
|
|
} else {
|
2021-08-23 14:14:48 +02:00
|
|
|
if (temperaturePin >= 0) {
|
|
|
|
DEBUG_PRINTLN(F("Temperature pin allocation failed."));
|
|
|
|
}
|
2021-07-03 21:27:06 +02:00
|
|
|
temperaturePin = -1; // allocation failed
|
2021-04-17 17:04:36 +02:00
|
|
|
}
|
2020-09-13 19:26:27 +02:00
|
|
|
}
|
2021-07-04 14:23:53 +02:00
|
|
|
lastMeasurement = millis() - readingInterval + 10000;
|
2021-04-11 16:47:53 +02:00
|
|
|
initDone = true;
|
2020-05-28 02:20:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2021-06-27 12:15:35 +02:00
|
|
|
if (!enabled || strip.isUpdating()) return;
|
2021-02-25 09:54:10 +01:00
|
|
|
|
2020-09-13 19:26:27 +02:00
|
|
|
unsigned long now = millis();
|
|
|
|
|
|
|
|
// check to see if we are due for taking a measurement
|
|
|
|
// lastMeasurement will not be updated until the conversion
|
|
|
|
// is complete the the reading is finished
|
2021-04-17 17:04:36 +02:00
|
|
|
if (now - lastMeasurement < readingInterval) return;
|
2020-09-13 19:26:27 +02:00
|
|
|
|
2021-02-25 09:54:10 +01:00
|
|
|
// we are due for a measurement, if we are not already waiting
|
2020-09-13 19:26:27 +02:00
|
|
|
// for a conversion to complete, then make a new request for temps
|
2021-02-25 09:54:10 +01:00
|
|
|
if (!waitingForConversion) {
|
2020-09-13 19:26:27 +02:00
|
|
|
requestTemperatures();
|
|
|
|
return;
|
|
|
|
}
|
2020-05-28 02:20:02 +02:00
|
|
|
|
2020-09-13 19:26:27 +02:00
|
|
|
// we were waiting for a conversion to complete, have we waited log enough?
|
2021-04-29 22:39:08 +02:00
|
|
|
if (now - lastTemperaturesRequest >= 100 /* 93.75ms per the datasheet but can be up to 750ms */) {
|
|
|
|
readTemperature();
|
2021-02-25 09:54:10 +01:00
|
|
|
|
2020-05-28 02:20:02 +02:00
|
|
|
if (WLED_MQTT_CONNECTED) {
|
2021-02-25 09:54:10 +01:00
|
|
|
char subuf[64];
|
2020-05-28 02:20:02 +02:00
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
2021-12-15 19:34:06 +01:00
|
|
|
if (temperature > -100.0f) {
|
2020-09-13 19:26:27 +02:00
|
|
|
// dont publish super low temperature as the graph will get messed up
|
|
|
|
// the DallasTemperature library returns -127C or -196.6F when problem
|
|
|
|
// reading the sensor
|
2021-01-17 15:00:14 +01:00
|
|
|
strcat_P(subuf, PSTR("/temperature"));
|
2021-12-15 19:34:06 +01:00
|
|
|
mqtt->publish(subuf, 0, false, String(getTemperatureC()).c_str());
|
2021-05-03 22:50:55 +02:00
|
|
|
strcat_P(subuf, PSTR("_f"));
|
2021-12-15 19:34:06 +01:00
|
|
|
mqtt->publish(subuf, 0, false, String(getTemperatureF()).c_str());
|
2020-09-13 19:26:27 +02:00
|
|
|
} else {
|
|
|
|
// publish something else to indicate status?
|
|
|
|
}
|
2020-05-28 02:20:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 22:39:08 +02:00
|
|
|
/*
|
|
|
|
* API calls te enable data exchange between WLED modules
|
|
|
|
*/
|
|
|
|
inline float getTemperatureC() {
|
|
|
|
return (float)temperature;
|
|
|
|
}
|
|
|
|
inline float getTemperatureF() {
|
|
|
|
return (float)temperature * 1.8f + 32;
|
|
|
|
}
|
|
|
|
|
2021-04-13 08:11:51 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
* Below it is shown how this could be used for e.g. a light sensor
|
|
|
|
*/
|
2020-05-28 02:20:02 +02:00
|
|
|
void addToJsonInfo(JsonObject& root) {
|
2020-09-13 19:26:27 +02:00
|
|
|
// dont add temperature to info if we are disabled
|
2021-06-27 12:15:35 +02:00
|
|
|
if (!enabled) return;
|
2020-09-13 19:26:27 +02:00
|
|
|
|
2021-04-12 21:10:47 +02:00
|
|
|
JsonObject user = root["u"];
|
|
|
|
if (user.isNull()) user = root.createNestedObject("u");
|
2020-05-28 02:20:02 +02:00
|
|
|
|
2021-04-22 22:34:43 +02:00
|
|
|
JsonArray temp = user.createNestedArray(FPSTR(_name));
|
2021-04-13 08:11:51 +02:00
|
|
|
//temp.add(F("Loaded."));
|
2020-09-13 19:26:27 +02:00
|
|
|
|
2021-12-15 19:34:06 +01:00
|
|
|
if (temperature <= -100.0f) {
|
2020-05-28 02:20:02 +02:00
|
|
|
temp.add(0);
|
2021-01-17 15:00:14 +01:00
|
|
|
temp.add(F(" Sensor Error!"));
|
2020-05-28 02:20:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:34:06 +01:00
|
|
|
temp.add(degC ? getTemperatureC() : getTemperatureF());
|
2021-02-25 09:54:10 +01:00
|
|
|
if (degC) temp.add(F("°C"));
|
|
|
|
else temp.add(F("°F"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object).
|
|
|
|
* Values in the state object may be modified by connected clients
|
|
|
|
*/
|
2021-04-13 08:11:51 +02:00
|
|
|
//void addToJsonState(JsonObject &root)
|
|
|
|
//{
|
|
|
|
//}
|
2021-02-25 09:54:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object).
|
|
|
|
* Values in the state object may be modified by connected clients
|
2021-04-10 00:17:15 +02:00
|
|
|
* Read "<usermodname>_<usermodparam>" from json state and and change settings (i.e. GPIO pin) used.
|
2021-02-25 09:54:10 +01:00
|
|
|
*/
|
2021-04-22 22:34:43 +02:00
|
|
|
//void readFromJsonState(JsonObject &root) {
|
|
|
|
// if (!initDone) return; // prevent crash on boot applyPreset()
|
|
|
|
//}
|
2021-02-25 09:54:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* addToConfig() (called from set.cpp) stores persistent properties to cfg.json
|
|
|
|
*/
|
|
|
|
void addToConfig(JsonObject &root) {
|
|
|
|
// we add JSON object: {"Temperature": {"pin": 0, "degC": true}}
|
2021-04-22 22:34:43 +02:00
|
|
|
JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname
|
2021-06-27 12:15:35 +02:00
|
|
|
top[FPSTR(_enabled)] = enabled;
|
2021-04-12 21:10:47 +02:00
|
|
|
top["pin"] = temperaturePin; // usermodparam
|
|
|
|
top["degC"] = degC; // usermodparam
|
2021-04-22 22:34:43 +02:00
|
|
|
top[FPSTR(_readInterval)] = readingInterval / 1000;
|
2021-05-26 16:11:48 +02:00
|
|
|
top[FPSTR(_parasite)] = parasite;
|
2021-04-17 17:04:36 +02:00
|
|
|
DEBUG_PRINTLN(F("Temperature config saved."));
|
2021-02-25 09:54:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
|
2021-06-27 12:15:35 +02:00
|
|
|
*
|
|
|
|
* The function should return true if configuration was successfully loaded or false if there was no configuration.
|
2021-02-25 09:54:10 +01:00
|
|
|
*/
|
2021-06-25 01:26:15 +02:00
|
|
|
bool readFromConfig(JsonObject &root) {
|
2021-02-25 09:54:10 +01:00
|
|
|
// we look for JSON object: {"Temperature": {"pin": 0, "degC": true}}
|
2021-04-17 17:04:36 +02:00
|
|
|
int8_t newTemperaturePin = temperaturePin;
|
2021-10-02 22:32:33 +02:00
|
|
|
DEBUG_PRINT(FPSTR(_name));
|
2021-05-26 21:33:40 +02:00
|
|
|
|
2021-06-27 12:15:35 +02:00
|
|
|
JsonObject top = root[FPSTR(_name)];
|
|
|
|
if (top.isNull()) {
|
|
|
|
DEBUG_PRINTLN(F(": No config found. (Using defaults.)"));
|
|
|
|
return false;
|
2021-05-26 21:33:40 +02:00
|
|
|
}
|
|
|
|
|
2021-06-27 12:15:35 +02:00
|
|
|
enabled = top[FPSTR(_enabled)] | enabled;
|
|
|
|
newTemperaturePin = top["pin"] | newTemperaturePin;
|
|
|
|
degC = top["degC"] | degC;
|
|
|
|
readingInterval = top[FPSTR(_readInterval)] | readingInterval/1000;
|
|
|
|
readingInterval = min(120,max(10,(int)readingInterval)) * 1000; // convert to ms
|
|
|
|
parasite = top[FPSTR(_parasite)] | parasite;
|
2021-04-22 22:34:43 +02:00
|
|
|
|
2021-04-17 17:04:36 +02:00
|
|
|
if (!initDone) {
|
|
|
|
// first run: reading from cfg.json
|
|
|
|
temperaturePin = newTemperaturePin;
|
2021-06-27 12:15:35 +02:00
|
|
|
DEBUG_PRINTLN(F(" config loaded."));
|
2021-04-17 17:04:36 +02:00
|
|
|
} else {
|
2021-07-04 14:23:53 +02:00
|
|
|
DEBUG_PRINTLN(F(" config (re)loaded."));
|
2021-04-17 17:04:36 +02:00
|
|
|
// changing paramters from settings page
|
|
|
|
if (newTemperaturePin != temperaturePin) {
|
2021-07-04 14:23:53 +02:00
|
|
|
DEBUG_PRINTLN(F("Re-init temperature."));
|
2021-04-17 17:04:36 +02:00
|
|
|
// deallocate pin and release memory
|
|
|
|
delete oneWire;
|
2021-08-23 14:14:48 +02:00
|
|
|
pinManager.deallocatePin(temperaturePin, PinOwner::UM_Temperature);
|
2021-04-17 17:04:36 +02:00
|
|
|
temperaturePin = newTemperaturePin;
|
|
|
|
// initialise
|
|
|
|
setup();
|
|
|
|
}
|
|
|
|
}
|
2021-06-27 12:15:35 +02:00
|
|
|
// use "return !top["newestParameter"].isNull();" when updating Usermod with new features
|
2021-06-27 15:32:33 +02:00
|
|
|
return !top[FPSTR(_parasite)].isNull();
|
2020-05-28 02:20:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t getId()
|
|
|
|
{
|
|
|
|
return USERMOD_ID_TEMPERATURE;
|
|
|
|
}
|
2020-09-13 19:26:27 +02:00
|
|
|
};
|
2021-04-22 22:34:43 +02:00
|
|
|
|
|
|
|
// strings to reduce flash memory usage (used more than twice)
|
|
|
|
const char UsermodTemperature::_name[] PROGMEM = "Temperature";
|
|
|
|
const char UsermodTemperature::_enabled[] PROGMEM = "enabled";
|
2021-05-26 16:11:48 +02:00
|
|
|
const char UsermodTemperature::_readInterval[] PROGMEM = "read-interval-s";
|
|
|
|
const char UsermodTemperature::_parasite[] PROGMEM = "parasite-pwr";
|