2020-05-28 02:20:02 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "wled.h"
|
|
|
|
|
|
|
|
#include <DallasTemperature.h> //DS18B20
|
|
|
|
|
|
|
|
//Pin defaults for QuinLed Dig-Uno
|
2021-01-17 15:00:14 +01:00
|
|
|
#ifndef TEMPERATURE_PIN
|
2020-05-28 02:20:02 +02: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
|
|
|
|
2020-09-13 19:26:27 +02:00
|
|
|
// how many seconds after boot to take first measurement, 20 seconds
|
|
|
|
#ifndef USERMOD_DALLASTEMPERATURE_FIRST_MEASUREMENT_AT
|
|
|
|
#define USERMOD_DALLASTEMPERATURE_FIRST_MEASUREMENT_AT 20000
|
|
|
|
#endif
|
2020-05-28 02:20:02 +02:00
|
|
|
|
|
|
|
OneWire oneWire(TEMPERATURE_PIN);
|
|
|
|
DallasTemperature sensor(&oneWire);
|
|
|
|
|
|
|
|
class UsermodTemperature : public Usermod {
|
|
|
|
private:
|
2020-09-13 19:26:27 +02:00
|
|
|
// The device's unique 64-bit serial code stored in on-board ROM.
|
|
|
|
// Reading directly from the sensor device address is faster than
|
|
|
|
// reading from index. When reading by index, DallasTemperature
|
|
|
|
// must first look up the device address at the specified index.
|
|
|
|
DeviceAddress sensorDeviceAddress;
|
|
|
|
// set last reading as "40 sec before boot", so first reading is taken after 20 sec
|
|
|
|
unsigned long lastMeasurement = UINT32_MAX - (USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL - USERMOD_DALLASTEMPERATURE_FIRST_MEASUREMENT_AT);
|
|
|
|
// 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;
|
|
|
|
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
|
|
|
|
// allows this library to report to the user how long until the first
|
|
|
|
// measurement
|
|
|
|
bool getTemperatureComplete = 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;
|
|
|
|
|
|
|
|
void requestTemperatures() {
|
|
|
|
// there is requestTemperaturesByAddress however it
|
|
|
|
// appears to do more work,
|
|
|
|
// TODO: measure exection time difference
|
|
|
|
sensor.requestTemperatures();
|
|
|
|
lastTemperaturesRequest = millis();
|
|
|
|
waitingForConversion = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void getTemperature() {
|
2021-01-17 15:00:14 +01:00
|
|
|
if (strip.isUpdating()) return;
|
2020-09-13 19:26:27 +02:00
|
|
|
#ifdef USERMOD_DALLASTEMPERATURE_CELSIUS
|
|
|
|
temperature = sensor.getTempC(sensorDeviceAddress);
|
2020-05-28 02:20:02 +02:00
|
|
|
#else
|
2020-09-13 19:26:27 +02:00
|
|
|
temperature = sensor.getTempF(sensorDeviceAddress);
|
2020-05-28 02:20:02 +02:00
|
|
|
#endif
|
2020-09-13 19:26:27 +02:00
|
|
|
|
|
|
|
lastMeasurement = millis();
|
|
|
|
waitingForConversion = false;
|
|
|
|
getTemperatureComplete = true;
|
2020-05-28 02:20:02 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 19:26:27 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
|
2020-05-28 02:20:02 +02:00
|
|
|
void setup() {
|
|
|
|
sensor.begin();
|
2020-09-13 19:26:27 +02:00
|
|
|
|
|
|
|
// get the unique 64-bit serial code stored in on-board ROM
|
|
|
|
// if getAddress returns false, the sensor was not found
|
|
|
|
disabled = !sensor.getAddress(sensorDeviceAddress, 0);
|
|
|
|
|
|
|
|
if (!disabled) {
|
2021-01-17 15:00:14 +01:00
|
|
|
DEBUG_PRINTLN(F("Dallas Temperature found"));
|
2020-09-13 19:26:27 +02:00
|
|
|
// set the resolution for this specific device
|
|
|
|
sensor.setResolution(sensorDeviceAddress, 9, true);
|
|
|
|
// do not block waiting for reading
|
2021-01-17 15:00:14 +01:00
|
|
|
sensor.setWaitForConversion(false);
|
|
|
|
// allocate pin & prevent other use
|
|
|
|
if (!pinManager.allocatePin(TEMPERATURE_PIN,false))
|
|
|
|
disabled = true;
|
2020-09-13 19:26:27 +02:00
|
|
|
} else {
|
2021-01-17 15:00:14 +01:00
|
|
|
DEBUG_PRINTLN(F("Dallas Temperature not found"));
|
2020-09-13 19:26:27 +02:00
|
|
|
}
|
2020-05-28 02:20:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2021-01-21 01:21:16 +01:00
|
|
|
if (disabled || strip.isUpdating()) return;
|
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-01-21 01:21:16 +01:00
|
|
|
if (now - lastMeasurement < USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL) return;
|
2020-09-13 19:26:27 +02:00
|
|
|
|
|
|
|
// we are due for a measurement, if we are not already waiting
|
|
|
|
// for a conversion to complete, then make a new request for temps
|
|
|
|
if (!waitingForConversion)
|
2020-05-28 02:20:02 +02:00
|
|
|
{
|
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?
|
|
|
|
if (now - lastTemperaturesRequest >= 94 /* 93.75ms per the datasheet */)
|
|
|
|
{
|
|
|
|
getTemperature();
|
|
|
|
|
2020-05-28 02:20:02 +02:00
|
|
|
if (WLED_MQTT_CONNECTED) {
|
2021-04-02 20:04:27 +02:00
|
|
|
char subuf[45];
|
2020-05-28 02:20:02 +02:00
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
2020-09-13 19:26:27 +02:00
|
|
|
if (-100 <= temperature) {
|
|
|
|
// 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"));
|
2020-09-13 19:26:27 +02:00
|
|
|
mqtt->publish(subuf, 0, true, String(temperature).c_str());
|
|
|
|
} else {
|
|
|
|
// publish something else to indicate status?
|
|
|
|
}
|
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-01-21 01:21:16 +01:00
|
|
|
if (disabled) return;
|
2020-09-13 19:26:27 +02:00
|
|
|
|
2021-01-17 15:00:14 +01:00
|
|
|
JsonObject user = root[F("u")];
|
|
|
|
if (user.isNull()) user = root.createNestedObject(F("u"));
|
2020-05-28 02:20:02 +02:00
|
|
|
|
2021-01-17 15:00:14 +01:00
|
|
|
JsonArray temp = user.createNestedArray(F("Temperature"));
|
2020-09-13 19:26:27 +02:00
|
|
|
|
|
|
|
if (!getTemperatureComplete) {
|
|
|
|
// 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);
|
2021-01-17 15:00:14 +01:00
|
|
|
temp.add(F(" sec until read"));
|
2020-09-13 19:26:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temperature <= -100) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp.add(temperature);
|
2020-09-13 19:26:27 +02:00
|
|
|
#ifdef USERMOD_DALLASTEMPERATURE_CELSIUS
|
2021-01-17 15:00:14 +01:00
|
|
|
temp.add(F("°C"));
|
2020-05-28 02:20:02 +02:00
|
|
|
#else
|
2021-01-17 15:00:14 +01:00
|
|
|
temp.add(F("°F"));
|
2020-05-28 02:20:02 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t getId()
|
|
|
|
{
|
|
|
|
return USERMOD_ID_TEMPERATURE;
|
|
|
|
}
|
2020-09-13 19:26:27 +02:00
|
|
|
};
|