2020-09-27 17:26:30 +01:00
|
|
|
/*
|
2021-12-29 13:52:58 +00:00
|
|
|
xsns_05_esp32_ds18x20.ino - DS18x20 temperature sensor support for ESP32 Tasmota
|
2020-09-27 17:26:30 +01:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Heiko Krupp and Theo Arends
|
2020-09-27 17:26:30 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-06-10 11:27:07 +01:00
|
|
|
|
|
|
|
#ifdef ESP32
|
2020-09-27 17:26:30 +01:00
|
|
|
#ifdef USE_DS18x20
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* DS18B20 - Temperature - Multiple sensors
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XSNS_05 5
|
|
|
|
|
2021-01-10 17:18:28 +00:00
|
|
|
//#define DS18x20_USE_ID_AS_NAME // Use last 3 bytes for naming of sensors
|
|
|
|
|
2020-09-27 17:26:30 +01:00
|
|
|
#define DS18S20_CHIPID 0x10 // +/-0.5C 9-bit
|
|
|
|
#define DS1822_CHIPID 0x22 // +/-2C 12-bit
|
|
|
|
#define DS18B20_CHIPID 0x28 // +/-0.5C 12-bit
|
|
|
|
#define MAX31850_CHIPID 0x3B // +/-0.25C 14-bit
|
|
|
|
|
|
|
|
#define W1_SKIP_ROM 0xCC
|
|
|
|
#define W1_CONVERT_TEMP 0x44
|
|
|
|
#define W1_READ_SCRATCHPAD 0xBE
|
|
|
|
|
2021-06-17 21:50:53 +01:00
|
|
|
#ifndef DS18X20_MAX_SENSORS // DS18X20_MAX_SENSORS fallback to 8 if not defined in user_config_override.h
|
2020-09-27 17:26:30 +01:00
|
|
|
#define DS18X20_MAX_SENSORS 8
|
2021-06-17 21:50:53 +01:00
|
|
|
#endif
|
2020-09-27 17:26:30 +01:00
|
|
|
|
|
|
|
const char kDs18x20Types[] PROGMEM = "DS18x20|DS18S20|DS1822|DS18B20|MAX31850";
|
|
|
|
|
|
|
|
uint8_t ds18x20_chipids[] = { 0, DS18S20_CHIPID, DS1822_CHIPID, DS18B20_CHIPID, MAX31850_CHIPID };
|
|
|
|
|
2021-04-02 10:43:31 +01:00
|
|
|
struct {
|
|
|
|
float temp_sum;
|
|
|
|
uint16_t numread;
|
|
|
|
uint8_t address[8];
|
|
|
|
uint8_t index;
|
|
|
|
uint8_t valid;
|
|
|
|
} ds18x20_sensor[DS18X20_MAX_SENSORS];
|
|
|
|
|
|
|
|
struct {
|
|
|
|
char name[17];
|
|
|
|
uint8_t sensors = 0;
|
|
|
|
} DS18X20Data;
|
2020-09-27 17:26:30 +01:00
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
#include <OneWire.h>
|
|
|
|
|
|
|
|
OneWire *ds = nullptr;
|
|
|
|
|
|
|
|
void Ds18x20Init(void) {
|
|
|
|
ds = new OneWire(Pin(GPIO_DSB));
|
|
|
|
|
|
|
|
Ds18x20Search();
|
2021-04-02 10:43:31 +01:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_DSB D_SENSORS_FOUND " %d"), DS18X20Data.sensors);
|
2020-09-27 17:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Ds18x20Search(void) {
|
|
|
|
uint8_t num_sensors=0;
|
|
|
|
uint8_t sensor = 0;
|
|
|
|
|
|
|
|
ds->reset_search();
|
|
|
|
for (num_sensors = 0; num_sensors < DS18X20_MAX_SENSORS; num_sensors) {
|
2021-04-02 10:43:31 +01:00
|
|
|
if (!ds->search(ds18x20_sensor[num_sensors].address)) {
|
2020-09-27 17:26:30 +01:00
|
|
|
ds->reset_search();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// If CRC Ok and Type DS18S20, DS1822, DS18B20 or MAX31850
|
2021-04-02 10:43:31 +01:00
|
|
|
if ((OneWire::crc8(ds18x20_sensor[num_sensors].address, 7) == ds18x20_sensor[num_sensors].address[7]) &&
|
|
|
|
((ds18x20_sensor[num_sensors].address[0] == DS18S20_CHIPID) ||
|
|
|
|
(ds18x20_sensor[num_sensors].address[0] == DS1822_CHIPID) ||
|
|
|
|
(ds18x20_sensor[num_sensors].address[0] == DS18B20_CHIPID) ||
|
|
|
|
(ds18x20_sensor[num_sensors].address[0] == MAX31850_CHIPID))) {
|
2020-09-27 17:26:30 +01:00
|
|
|
num_sensors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < num_sensors; i++) {
|
2021-04-02 10:43:31 +01:00
|
|
|
ds18x20_sensor[i].index = i;
|
2020-09-27 17:26:30 +01:00
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < num_sensors; i++) {
|
|
|
|
for (uint32_t j = i + 1; j < num_sensors; j++) {
|
2021-04-02 10:43:31 +01:00
|
|
|
if (uint32_t(ds18x20_sensor[ds18x20_sensor[i].index].address) > uint32_t(ds18x20_sensor[ds18x20_sensor[j].index].address)) {
|
|
|
|
std::swap(ds18x20_sensor[i].index, ds18x20_sensor[j].index);
|
2020-09-27 17:26:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 10:43:31 +01:00
|
|
|
DS18X20Data.sensors = num_sensors;
|
2020-09-27 17:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Ds18x20Convert(void) {
|
|
|
|
ds->reset();
|
|
|
|
ds->write(W1_SKIP_ROM); // Address all Sensors on Bus
|
|
|
|
ds->write(W1_CONVERT_TEMP); // start conversion, no parasite power on at the end
|
|
|
|
// delay(750); // 750ms should be enough for 12bit conv
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:43:31 +01:00
|
|
|
bool Ds18x20Read(uint8_t sensor, float &t) {
|
2020-09-27 17:26:30 +01:00
|
|
|
uint8_t data[12];
|
|
|
|
int8_t sign = 1;
|
|
|
|
|
|
|
|
t = NAN;
|
|
|
|
|
2021-04-02 10:43:31 +01:00
|
|
|
uint8_t index = ds18x20_sensor[sensor].index;
|
|
|
|
if (ds18x20_sensor[index].valid) { ds18x20_sensor[index].valid--; }
|
2020-09-27 17:26:30 +01:00
|
|
|
|
|
|
|
ds->reset();
|
2021-04-02 10:43:31 +01:00
|
|
|
ds->select(ds18x20_sensor[index].address);
|
2020-09-27 17:26:30 +01:00
|
|
|
ds->write(W1_READ_SCRATCHPAD); // Read Scratchpad
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < 9; i++) {
|
|
|
|
data[i] = ds->read();
|
|
|
|
}
|
|
|
|
if (OneWire::crc8(data, 8) == data[8]) {
|
2021-04-02 10:43:31 +01:00
|
|
|
switch(ds18x20_sensor[index].address[0]) {
|
2020-09-27 17:26:30 +01:00
|
|
|
case DS18S20_CHIPID: {
|
|
|
|
int16_t tempS = (((data[1] << 8) | (data[0] & 0xFE)) << 3) | ((0x10 - data[6]) & 0x0F);
|
2022-04-19 14:44:53 +01:00
|
|
|
t = ConvertTemp(tempS * 0.0625f - 0.250f);
|
2021-04-02 10:43:31 +01:00
|
|
|
ds18x20_sensor[index].valid = SENSOR_MAX_MISS;
|
2020-09-27 17:26:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case DS1822_CHIPID:
|
|
|
|
case DS18B20_CHIPID: {
|
|
|
|
uint16_t temp12 = (data[1] << 8) + data[0];
|
|
|
|
if (temp12 > 2047) {
|
|
|
|
temp12 = (~temp12) +1;
|
|
|
|
sign = -1;
|
|
|
|
}
|
2022-04-19 14:44:53 +01:00
|
|
|
t = ConvertTemp(sign * temp12 * 0.0625f); // Divide by 16
|
2021-04-02 10:43:31 +01:00
|
|
|
ds18x20_sensor[index].valid = SENSOR_MAX_MISS;
|
2020-09-27 17:26:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case MAX31850_CHIPID: {
|
|
|
|
int16_t temp14 = (data[1] << 8) + (data[0] & 0xFC);
|
2022-04-19 14:44:53 +01:00
|
|
|
t = ConvertTemp(temp14 * 0.0625f); // Divide by 16
|
2021-04-02 10:43:31 +01:00
|
|
|
ds18x20_sensor[index].valid = SENSOR_MAX_MISS;
|
2020-09-27 17:26:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_DSB D_SENSOR_CRC_ERROR));
|
2020-09-27 17:26:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:43:31 +01:00
|
|
|
void Ds18x20Name(uint8_t sensor) {
|
2020-09-27 17:26:30 +01:00
|
|
|
uint8_t index = sizeof(ds18x20_chipids);
|
2021-04-30 10:59:08 +01:00
|
|
|
while (--index) {
|
2021-04-02 10:43:31 +01:00
|
|
|
if (ds18x20_sensor[ds18x20_sensor[sensor].index].address[0] == ds18x20_chipids[index]) {
|
2020-09-27 17:26:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 10:43:31 +01:00
|
|
|
GetTextIndexed(DS18X20Data.name, sizeof(DS18X20Data.name), index, kDs18x20Types);
|
|
|
|
if (DS18X20Data.sensors > 1) {
|
2021-01-10 17:18:28 +00:00
|
|
|
#ifdef DS18x20_USE_ID_AS_NAME
|
|
|
|
char address[17];
|
|
|
|
for (uint32_t j = 0; j < 3; j++) {
|
|
|
|
sprintf(address+2*j, "%02X", ds18x20_sensor[ds18x20_sensor[sensor].index].address[3-j]); // Only last 3 bytes
|
|
|
|
}
|
2021-04-02 10:43:31 +01:00
|
|
|
snprintf_P(DS18X20Data.name, sizeof(DS18X20Data.name), PSTR("%s%c%s"), DS18X20Data.name, IndexSeparator(), address);
|
2021-01-10 17:18:28 +00:00
|
|
|
#else
|
2021-04-02 10:43:31 +01:00
|
|
|
snprintf_P(DS18X20Data.name, sizeof(DS18X20Data.name), PSTR("%s%c%d"), DS18X20Data.name, IndexSeparator(), sensor +1);
|
2021-01-10 17:18:28 +00:00
|
|
|
#endif
|
2020-09-27 17:26:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2021-04-02 10:43:31 +01:00
|
|
|
void Ds18x20EverySecond(void) {
|
|
|
|
if (!DS18X20Data.sensors) { return; }
|
2020-09-27 17:26:30 +01:00
|
|
|
|
2020-10-28 16:32:07 +00:00
|
|
|
if (TasmotaGlobal.uptime & 1) {
|
2020-09-27 17:26:30 +01:00
|
|
|
// 2mS
|
|
|
|
// Ds18x20Search(); // Check for changes in sensors number
|
|
|
|
Ds18x20Convert(); // Start Conversion, takes up to one second
|
|
|
|
} else {
|
|
|
|
float t;
|
2021-04-02 10:43:31 +01:00
|
|
|
for (uint32_t i = 0; i < DS18X20Data.sensors; i++) {
|
2020-09-27 17:26:30 +01:00
|
|
|
// 12mS per device
|
2021-04-02 10:43:31 +01:00
|
|
|
if (Ds18x20Read(i, t)) { // Read temperature
|
2021-06-11 17:14:12 +01:00
|
|
|
if (Settings->flag5.ds18x20_mean) {
|
2021-04-02 10:43:31 +01:00
|
|
|
if (ds18x20_sensor[i].numread++ == 0) {
|
|
|
|
ds18x20_sensor[i].temp_sum = 0;
|
|
|
|
}
|
|
|
|
ds18x20_sensor[i].temp_sum += t;
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-27 17:26:30 +01:00
|
|
|
Ds18x20Name(i);
|
2021-04-02 10:43:31 +01:00
|
|
|
AddLogMissed(DS18X20Data.name, ds18x20_sensor[ds18x20_sensor[i].index].valid);
|
2020-09-27 17:26:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:43:31 +01:00
|
|
|
void Ds18x20Show(bool json) {
|
2020-09-27 17:26:30 +01:00
|
|
|
float t;
|
|
|
|
|
|
|
|
uint8_t dsxflg = 0;
|
2021-04-02 10:43:31 +01:00
|
|
|
for (uint32_t i = 0; i < DS18X20Data.sensors; i++) {
|
2020-09-27 17:26:30 +01:00
|
|
|
if (Ds18x20Read(i, t)) { // Check if read failed
|
|
|
|
Ds18x20Name(i);
|
|
|
|
|
|
|
|
if (json) {
|
2021-06-11 17:14:12 +01:00
|
|
|
if (Settings->flag5.ds18x20_mean) {
|
2021-04-02 10:43:31 +01:00
|
|
|
if ((0 == TasmotaGlobal.tele_period) && ds18x20_sensor[i].numread) {
|
|
|
|
t = ds18x20_sensor[i].temp_sum / ds18x20_sensor[i].numread;
|
|
|
|
ds18x20_sensor[i].numread = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-09-27 17:26:30 +01:00
|
|
|
char address[17];
|
|
|
|
for (uint32_t j = 0; j < 6; j++) {
|
2021-04-02 10:43:31 +01:00
|
|
|
sprintf(address+2*j, "%02X", ds18x20_sensor[ds18x20_sensor[i].index].address[6-j]); // Skip sensor type and crc
|
2020-09-27 17:26:30 +01:00
|
|
|
}
|
2021-01-26 15:26:00 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_ID "\":\"%s\",\"" D_JSON_TEMPERATURE "\":%*_f}"),
|
2021-06-11 17:14:12 +01:00
|
|
|
DS18X20Data.name, address, Settings->flag2.temperature_resolution, &t);
|
2020-09-27 17:26:30 +01:00
|
|
|
dsxflg++;
|
|
|
|
#ifdef USE_DOMOTICZ
|
2020-10-29 12:37:09 +00:00
|
|
|
if ((0 == TasmotaGlobal.tele_period) && (1 == dsxflg)) {
|
2021-01-26 15:26:00 +00:00
|
|
|
DomoticzFloatSensor(DZ_TEMP, t);
|
2020-09-27 17:26:30 +01:00
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_KNX
|
2020-10-29 12:37:09 +00:00
|
|
|
if ((0 == TasmotaGlobal.tele_period) && (1 == dsxflg)) {
|
2020-09-27 17:26:30 +01:00
|
|
|
KnxSensor(KNX_TEMPERATURE, t);
|
|
|
|
}
|
|
|
|
#endif // USE_KNX
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2021-04-02 10:43:31 +01:00
|
|
|
WSContentSend_Temp(DS18X20Data.name, t);
|
2020-09-27 17:26:30 +01:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2021-04-02 10:43:31 +01:00
|
|
|
bool Xsns05(uint8_t function) {
|
2020-09-27 17:26:30 +01:00
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (PinUsed(GPIO_DSB)) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_INIT:
|
|
|
|
Ds18x20Init();
|
|
|
|
break;
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
Ds18x20EverySecond();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Ds18x20Show(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
Ds18x20Show(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_DS18x20
|
|
|
|
#endif // ESP32
|