2017-01-28 13:41:01 +00:00
|
|
|
/*
|
2017-11-07 14:57:24 +00:00
|
|
|
xsns_05_ds18b20.ino - DS18B20 temperature sensor support for Sonoff-Tasmota
|
2017-05-13 12:02:10 +01:00
|
|
|
|
2017-12-22 13:55:24 +00:00
|
|
|
Copyright (C) 2018 Theo Arends
|
2017-05-13 12:02:10 +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/>.
|
2017-01-28 13:41:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_DS18B20
|
|
|
|
/*********************************************************************************************\
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
* DS18B20 - Temperature - Single sensor
|
2017-01-28 13:41:01 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_05 5
|
|
|
|
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
#define W1_SKIP_ROM 0xCC
|
|
|
|
#define W1_CONVERT_TEMP 0x44
|
|
|
|
#define W1_READ_SCRATCHPAD 0xBE
|
|
|
|
|
2018-07-09 13:18:26 +01:00
|
|
|
float ds18b20_temperature = 0;
|
2018-07-10 21:12:16 +01:00
|
|
|
uint8_t ds18b20_valid = 0;
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
uint8_t ds18x20_pin = 0;
|
2018-07-10 21:12:16 +01:00
|
|
|
char ds18b20_types[] = "DS18B20";
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Embedded stripped and tuned OneWire library
|
|
|
|
\*********************************************************************************************/
|
2017-03-05 14:07:30 +00:00
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
uint8_t OneWireReset(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
uint8_t retries = 125;
|
|
|
|
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
//noInterrupts();
|
|
|
|
pinMode(ds18x20_pin, INPUT);
|
|
|
|
do {
|
2017-04-25 17:24:42 +01:00
|
|
|
if (--retries == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
delayMicroseconds(2);
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
} while (!digitalRead(ds18x20_pin));
|
|
|
|
pinMode(ds18x20_pin, OUTPUT);
|
|
|
|
digitalWrite(ds18x20_pin, LOW);
|
|
|
|
delayMicroseconds(480);
|
|
|
|
pinMode(ds18x20_pin, INPUT);
|
|
|
|
delayMicroseconds(70);
|
|
|
|
uint8_t r = !digitalRead(ds18x20_pin);
|
|
|
|
//interrupts();
|
|
|
|
delayMicroseconds(410);
|
2017-01-28 13:41:01 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
void OneWireWriteBit(uint8_t v)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
static const uint8_t delay_low[2] = { 65, 10 };
|
|
|
|
static const uint8_t delay_high[2] = { 5, 55 };
|
|
|
|
|
|
|
|
v &= 1;
|
|
|
|
//noInterrupts();
|
|
|
|
digitalWrite(ds18x20_pin, LOW);
|
|
|
|
pinMode(ds18x20_pin, OUTPUT);
|
|
|
|
delayMicroseconds(delay_low[v]);
|
|
|
|
digitalWrite(ds18x20_pin, HIGH);
|
|
|
|
//interrupts();
|
|
|
|
delayMicroseconds(delay_high[v]);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
uint8_t OneWireReadBit(void)
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
{
|
|
|
|
//noInterrupts();
|
|
|
|
pinMode(ds18x20_pin, OUTPUT);
|
|
|
|
digitalWrite(ds18x20_pin, LOW);
|
2017-01-28 13:41:01 +00:00
|
|
|
delayMicroseconds(3);
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
pinMode(ds18x20_pin, INPUT);
|
2017-01-28 13:41:01 +00:00
|
|
|
delayMicroseconds(10);
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
uint8_t r = digitalRead(ds18x20_pin);
|
|
|
|
//interrupts();
|
2017-01-28 13:41:01 +00:00
|
|
|
delayMicroseconds(53);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
void OneWireWrite(uint8_t v)
|
|
|
|
{
|
|
|
|
for (uint8_t bit_mask = 0x01; bit_mask; bit_mask <<= 1) {
|
|
|
|
OneWireWriteBit((bit_mask & v) ? 1 : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
uint8_t OneWireRead(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
|
|
|
uint8_t r = 0;
|
|
|
|
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
for (uint8_t bit_mask = 0x01; bit_mask; bit_mask <<= 1) {
|
|
|
|
if (OneWireReadBit()) {
|
2017-10-18 17:22:34 +01:00
|
|
|
r |= bit_mask;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
boolean OneWireCrc8(uint8_t *addr)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
uint8_t crc = 0;
|
|
|
|
uint8_t len = 8;
|
|
|
|
|
|
|
|
while (len--) {
|
|
|
|
uint8_t inbyte = *addr++; // from 0 to 7
|
|
|
|
for (uint8_t i = 8; i; i--) {
|
|
|
|
uint8_t mix = (crc ^ inbyte) & 0x01;
|
|
|
|
crc >>= 1;
|
|
|
|
if (mix) {
|
|
|
|
crc ^= 0x8C;
|
|
|
|
}
|
|
|
|
inbyte >>= 1;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
return (crc == *addr); // addr 8
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
/********************************************************************************************/
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void Ds18b20Convert(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
OneWireReset();
|
|
|
|
OneWireWrite(W1_SKIP_ROM); // Address all Sensors on Bus
|
|
|
|
OneWireWrite(W1_CONVERT_TEMP); // start conversion, no parasite power on at the end
|
|
|
|
// delay(750); // 750ms should be enough for 12bit conv
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
boolean Ds18b20Read(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
uint8_t data[9];
|
|
|
|
int8_t sign = 1;
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-07-10 21:12:16 +01:00
|
|
|
if (ds18b20_valid) { ds18b20_valid--; }
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
/*
|
2018-07-09 13:18:26 +01:00
|
|
|
if (!OneWireReadBit()) { // Check end of measurement
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DSB D_SENSOR_BUSY));
|
2018-07-09 13:18:26 +01:00
|
|
|
return;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
*/
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
for (uint8_t retry = 0; retry < 3; retry++) {
|
|
|
|
OneWireReset();
|
|
|
|
OneWireWrite(W1_SKIP_ROM);
|
|
|
|
OneWireWrite(W1_READ_SCRATCHPAD);
|
|
|
|
for (uint8_t i = 0; i < 9; i++) {
|
|
|
|
data[i] = OneWireRead();
|
|
|
|
}
|
|
|
|
if (OneWireCrc8(data)) {
|
|
|
|
uint16_t temp12 = (data[1] << 8) + data[0];
|
|
|
|
if (temp12 > 2047) {
|
|
|
|
temp12 = (~temp12) +1;
|
|
|
|
sign = -1;
|
|
|
|
}
|
2018-07-09 13:18:26 +01:00
|
|
|
ds18b20_temperature = ConvertTemp(sign * temp12 * 0.0625);
|
2018-07-10 21:12:16 +01:00
|
|
|
ds18b20_valid = SENSOR_MAX_MISS;
|
|
|
|
return true;
|
2017-04-10 16:25:31 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
v5.9.1j - Add Dual R2, Rewrite DS18x20 and fixes
5.9.1j
* Revert changes to xsns_05_ds18x20.ino and rename to
xsns_05_ds18x20_legacy.ino still needing library OneWire and providing
legacy JSON message:
*
"DS18x20":{"DS1":{"Type":"DS18B20","Address":"284CC48E04000079","Temperature":19.5},"DS2":{"Type":"DS18B20","Address":"283AC28304000052","Temperature":19.6}}
* Add new xdrv_05_ds18x20.ino free from library OneWire and add the
following features:
* Add support for DS1822
* Add forced setting of
12-bit resolution for selected device types (#1222)
* Add read
temperature retry counter (#1215)
* Fix lost sensors by performing
sensor probe at restart only thereby removing dynamic sensor probe
(#1215)
* Fix sensor address sorting using ascending sort on sensor
type followed by sensor address
* Rewrite JSON resulting in shorter
message allowing more sensors in default firmware image:
*
"DS18B20-1":{"Id":"00000483C23A","Temperature":19.5},"DS18B20-2":{"Id":"0000048EC44C","Temperature":19.6}
* Add additional define in user_config.h to select either single sensor
(defines disabled), new multi sensor (USE_DS18X20) or legacy multi
sensor (USE_DS18X20_LEGACY)
* Add support for Sonoff Dual R2 (#1249)
*
Fix ADS1115 detection (#1258)
2017-11-27 16:46:51 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DSB D_SENSOR_CRC_ERROR));
|
2018-07-10 21:12:16 +01:00
|
|
|
return false;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 13:18:26 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void Ds18b20EverySecond(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2018-07-09 13:18:26 +01:00
|
|
|
ds18x20_pin = pin[GPIO_DSB];
|
2018-07-09 15:39:24 +01:00
|
|
|
if (uptime &1) {
|
2018-07-12 11:19:08 +01:00
|
|
|
// 2mS
|
2018-07-09 13:18:26 +01:00
|
|
|
Ds18b20Convert(); // Start conversion, takes up to one second
|
|
|
|
} else {
|
2018-07-12 11:19:08 +01:00
|
|
|
// 12mS
|
2018-07-10 21:12:16 +01:00
|
|
|
if (!Ds18b20Read()) { // Read temperature
|
|
|
|
AddLogMissed(ds18b20_types, ds18b20_valid);
|
|
|
|
}
|
2018-07-09 13:18:26 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-07-09 13:18:26 +01:00
|
|
|
void Ds18b20Show(boolean json)
|
|
|
|
{
|
2018-07-10 21:12:16 +01:00
|
|
|
if (ds18b20_valid) { // Check for valid temperature
|
2018-12-21 15:17:06 +00:00
|
|
|
char temperature[33];
|
2018-07-09 13:18:26 +01:00
|
|
|
dtostrfd(ds18b20_temperature, Settings.flag2.temperature_resolution, temperature);
|
2017-11-04 15:36:51 +00:00
|
|
|
if(json) {
|
2018-07-10 21:12:16 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), JSON_SNS_TEMP, mqtt_data, ds18b20_types, temperature);
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2018-07-10 21:12:16 +01:00
|
|
|
if (0 == tele_period) {
|
|
|
|
DomoticzSensor(DZ_TEMP, temperature);
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
2018-06-12 18:46:52 +01:00
|
|
|
#ifdef USE_KNX
|
2018-07-10 21:12:16 +01:00
|
|
|
if (0 == tele_period) {
|
|
|
|
KnxSensor(KNX_TEMPERATURE, ds18b20_temperature);
|
|
|
|
}
|
2018-06-12 18:46:52 +01:00
|
|
|
#endif // USE_KNX
|
2017-11-04 15:36:51 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2018-07-10 21:12:16 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, ds18b20_types, temperature, TempUnit());
|
2017-11-04 15:36:51 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
2017-11-04 15:36:51 +00:00
|
|
|
}
|
2017-11-03 17:07:25 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
boolean Xsns05(byte function)
|
|
|
|
{
|
|
|
|
boolean result = false;
|
|
|
|
|
|
|
|
if (pin[GPIO_DSB] < 99) {
|
|
|
|
switch (function) {
|
2018-07-09 13:18:26 +01:00
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
Ds18b20EverySecond();
|
2017-11-03 17:07:25 +00:00
|
|
|
break;
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
2017-11-04 15:36:51 +00:00
|
|
|
Ds18b20Show(1);
|
2017-11-03 17:07:25 +00:00
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_WEB_APPEND:
|
2017-11-04 15:36:51 +00:00
|
|
|
Ds18b20Show(0);
|
2017-11-03 17:07:25 +00:00
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_DS18B20
|