2017-01-28 13:41:01 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xsns_05_ds18x20.ino - DS18x20 temperature sensor support for Tasmota
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Theo Arends
|
2017-01-28 13:41:01 +00:00
|
|
|
|
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.
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-13 12:02:10 +01:00
|
|
|
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
|
|
|
*/
|
|
|
|
|
2020-09-27 17:26:30 +01:00
|
|
|
#ifdef ESP8266
|
2017-01-28 13:41:01 +00:00
|
|
|
#ifdef USE_DS18x20
|
|
|
|
/*********************************************************************************************\
|
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 - Multiple sensors
|
2017-01-28 13:41:01 +00:00
|
|
|
\*********************************************************************************************/
|
2018-11-06 16:33:51 +00:00
|
|
|
|
|
|
|
#define XSNS_05 5
|
|
|
|
|
2018-07-12 11:19:08 +01:00
|
|
|
//#define USE_DS18x20_RECONFIGURE // When sensor is lost keep retrying or re-configure
|
2021-01-10 17:18:28 +00:00
|
|
|
//#define DS18x20_USE_ID_AS_NAME // Use last 3 bytes for naming of sensors
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-11-24 16:26:20 +00: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
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-03-29 17:42:05 +01:00
|
|
|
#define W1_SKIP_ROM 0xCC
|
|
|
|
#define W1_CONVERT_TEMP 0x44
|
2017-11-24 16:26:20 +00:00
|
|
|
#define W1_WRITE_EEPROM 0x48
|
|
|
|
#define W1_WRITE_SCRATCHPAD 0x4E
|
2017-03-29 17:42:05 +01:00
|
|
|
#define W1_READ_SCRATCHPAD 0xBE
|
|
|
|
|
2020-12-18 20:56:10 +00:00
|
|
|
#ifndef DS18X20_MAX_SENSORS // DS18X20_MAX_SENSORS fallback to 8 if not defined in user_config_override.h
|
2017-03-29 17:42:05 +01:00
|
|
|
#define DS18X20_MAX_SENSORS 8
|
2020-12-18 20:56:10 +00:00
|
|
|
#endif
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-11-24 16:26:20 +00:00
|
|
|
const char kDs18x20Types[] PROGMEM = "DS18x20|DS18S20|DS1822|DS18B20|MAX31850";
|
|
|
|
|
|
|
|
uint8_t ds18x20_chipids[] = { 0, DS18S20_CHIPID, DS1822_CHIPID, DS18B20_CHIPID, MAX31850_CHIPID };
|
2018-07-09 14:50:52 +01:00
|
|
|
|
2021-04-02 10:43:31 +01:00
|
|
|
struct {
|
|
|
|
float temperature;
|
|
|
|
float temp_sum;
|
|
|
|
uint16_t numread;
|
2018-07-09 15:39:24 +01:00
|
|
|
uint8_t address[8];
|
|
|
|
uint8_t index;
|
2018-07-10 21:12:16 +01:00
|
|
|
uint8_t valid;
|
2018-07-09 15:39:24 +01:00
|
|
|
} ds18x20_sensor[DS18X20_MAX_SENSORS];
|
2021-03-09 17:01:29 +00:00
|
|
|
|
|
|
|
struct {
|
2018-10-01 15:28:21 +01:00
|
|
|
#ifdef W1_PARASITE_POWER
|
2021-03-09 17:01:29 +00:00
|
|
|
uint32_t w1_power_until = 0;
|
|
|
|
uint8_t current_sensor = 0;
|
2018-10-01 15:28:21 +01:00
|
|
|
#endif
|
2021-03-09 17:01:29 +00:00
|
|
|
char name[17];
|
|
|
|
uint8_t sensors = 0;
|
|
|
|
uint8_t input_mode = 0; // INPUT or INPUT_PULLUP (=2)
|
|
|
|
int8_t pin = 0; // Shelly GPIO3 input only
|
|
|
|
int8_t pin_out = 0; // Shelly GPIO00 output only
|
|
|
|
bool dual_mode = false; // Single pin mode
|
|
|
|
} DS18X20Data;
|
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
|
|
|
/*********************************************************************************************\
|
|
|
|
* Embedded tuned OneWire library
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define W1_MATCH_ROM 0x55
|
|
|
|
#define W1_SEARCH_ROM 0xF0
|
|
|
|
|
|
|
|
uint8_t onewire_last_discrepancy = 0;
|
|
|
|
uint8_t onewire_last_family_discrepancy = 0;
|
|
|
|
bool onewire_last_device_flag = false;
|
|
|
|
unsigned char onewire_rom_id[8] = { 0 };
|
|
|
|
|
2020-02-05 15:28:56 +00:00
|
|
|
/*------------------------------------------------------------------------------------------*/
|
|
|
|
|
2021-03-09 17:01:29 +00:00
|
|
|
uint8_t OneWireReset(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
|
|
|
uint8_t retries = 125;
|
|
|
|
|
2021-03-09 17:01:29 +00:00
|
|
|
if (!DS18X20Data.dual_mode) {
|
|
|
|
pinMode(DS18X20Data.pin, DS18X20Data.input_mode);
|
2020-02-05 15:28:56 +00:00
|
|
|
do {
|
|
|
|
if (--retries == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
delayMicroseconds(2);
|
2021-03-09 17:01:29 +00:00
|
|
|
} while (!digitalRead(DS18X20Data.pin));
|
|
|
|
pinMode(DS18X20Data.pin, OUTPUT);
|
|
|
|
digitalWrite(DS18X20Data.pin, LOW);
|
2020-02-05 15:28:56 +00:00
|
|
|
delayMicroseconds(480);
|
2021-03-09 17:01:29 +00:00
|
|
|
pinMode(DS18X20Data.pin, DS18X20Data.input_mode);
|
2020-03-10 15:54:06 +00:00
|
|
|
delayMicroseconds(70);
|
2021-03-09 17:01:29 +00:00
|
|
|
uint8_t r = !digitalRead(DS18X20Data.pin);
|
2020-03-10 15:54:06 +00:00
|
|
|
delayMicroseconds(410);
|
|
|
|
return r;
|
2020-02-05 15:28:56 +00:00
|
|
|
} else {
|
2021-03-09 17:01:29 +00:00
|
|
|
digitalWrite(DS18X20Data.pin_out, HIGH);
|
2020-02-05 15:28:56 +00:00
|
|
|
do {
|
|
|
|
if (--retries == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
delayMicroseconds(2);
|
2021-03-09 17:01:29 +00:00
|
|
|
} while (!digitalRead(DS18X20Data.pin));
|
|
|
|
digitalWrite(DS18X20Data.pin_out, LOW);
|
2020-02-05 15:28:56 +00:00
|
|
|
delayMicroseconds(480);
|
2021-03-09 17:01:29 +00:00
|
|
|
digitalWrite(DS18X20Data.pin_out, HIGH);
|
2020-03-10 15:54:06 +00:00
|
|
|
delayMicroseconds(70);
|
2021-03-09 17:01:29 +00:00
|
|
|
uint8_t r = !digitalRead(DS18X20Data.pin);
|
2020-03-10 15:54:06 +00:00
|
|
|
delayMicroseconds(410);
|
|
|
|
return r;
|
2020-02-05 15:28:56 +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
|
|
|
}
|
|
|
|
|
2021-03-09 17:01:29 +00:00
|
|
|
void OneWireWriteBit(uint8_t v) {
|
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;
|
2021-03-09 17:01:29 +00:00
|
|
|
if (!DS18X20Data.dual_mode) {
|
|
|
|
digitalWrite(DS18X20Data.pin, LOW);
|
|
|
|
pinMode(DS18X20Data.pin, OUTPUT);
|
2020-02-05 15:28:56 +00:00
|
|
|
delayMicroseconds(delay_low[v]);
|
2021-03-09 17:01:29 +00:00
|
|
|
digitalWrite(DS18X20Data.pin, HIGH);
|
2020-02-05 15:28:56 +00:00
|
|
|
} else {
|
2021-03-09 17:01:29 +00:00
|
|
|
digitalWrite(DS18X20Data.pin_out, LOW);
|
2020-02-05 15:28:56 +00:00
|
|
|
delayMicroseconds(delay_low[v]);
|
2021-03-09 17:01:29 +00:00
|
|
|
digitalWrite(DS18X20Data.pin_out, HIGH);
|
2020-02-05 15:28:56 +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
|
|
|
delayMicroseconds(delay_high[v]);
|
|
|
|
}
|
|
|
|
|
2021-03-09 17:01:29 +00:00
|
|
|
uint8_t OneWire1ReadBit(void) {
|
|
|
|
pinMode(DS18X20Data.pin, OUTPUT);
|
|
|
|
digitalWrite(DS18X20Data.pin, LOW);
|
2020-03-10 15:54:06 +00:00
|
|
|
delayMicroseconds(3);
|
2021-03-09 17:01:29 +00:00
|
|
|
pinMode(DS18X20Data.pin, DS18X20Data.input_mode);
|
2020-03-10 15:54:06 +00:00
|
|
|
delayMicroseconds(10);
|
2021-03-09 17:01:29 +00:00
|
|
|
uint8_t r = digitalRead(DS18X20Data.pin);
|
2020-03-10 15:54:06 +00:00
|
|
|
delayMicroseconds(53);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2021-03-09 17:01:29 +00:00
|
|
|
uint8_t OneWire2ReadBit(void) {
|
|
|
|
digitalWrite(DS18X20Data.pin_out, LOW);
|
2020-03-10 15:54:06 +00:00
|
|
|
delayMicroseconds(3);
|
2021-03-09 17:01:29 +00:00
|
|
|
digitalWrite(DS18X20Data.pin_out, HIGH);
|
2020-03-10 15:54:06 +00:00
|
|
|
delayMicroseconds(10);
|
2021-03-09 17:01:29 +00:00
|
|
|
uint8_t r = digitalRead(DS18X20Data.pin);
|
2020-03-10 15:54:06 +00:00
|
|
|
delayMicroseconds(53);
|
|
|
|
return r;
|
2020-02-18 17:10:36 +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
|
|
|
|
2020-02-05 15:28:56 +00:00
|
|
|
/*------------------------------------------------------------------------------------------*/
|
|
|
|
|
2021-03-09 17:01:29 +00:00
|
|
|
void OneWireWrite(uint8_t v) {
|
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) {
|
|
|
|
OneWireWriteBit((bit_mask & v) ? 1 : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 17:01:29 +00:00
|
|
|
uint8_t OneWireRead(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
|
|
|
uint8_t r = 0;
|
|
|
|
|
2021-03-09 17:01:29 +00:00
|
|
|
if (!DS18X20Data.dual_mode) {
|
2020-03-10 15:54:06 +00:00
|
|
|
for (uint8_t bit_mask = 0x01; bit_mask; bit_mask <<= 1) {
|
|
|
|
if (OneWire1ReadBit()) {
|
|
|
|
r |= bit_mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (uint8_t bit_mask = 0x01; bit_mask; bit_mask <<= 1) {
|
|
|
|
if (OneWire2ReadBit()) {
|
|
|
|
r |= bit_mask;
|
|
|
|
}
|
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 r;
|
|
|
|
}
|
|
|
|
|
2021-03-09 17:01:29 +00:00
|
|
|
void OneWireSelect(const uint8_t rom[8]) {
|
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
|
|
|
OneWireWrite(W1_MATCH_ROM);
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < 8; i++) {
|
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
|
|
|
OneWireWrite(rom[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 17:01:29 +00:00
|
|
|
uint8_t OneWireSearch(uint8_t *newAddr) {
|
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 id_bit_number = 1;
|
|
|
|
uint8_t last_zero = 0;
|
|
|
|
uint8_t rom_byte_number = 0;
|
|
|
|
uint8_t search_result = 0;
|
|
|
|
uint8_t id_bit;
|
|
|
|
uint8_t cmp_id_bit;
|
|
|
|
unsigned char rom_byte_mask = 1;
|
|
|
|
unsigned char search_direction;
|
|
|
|
|
|
|
|
if (!onewire_last_device_flag) {
|
|
|
|
if (!OneWireReset()) {
|
|
|
|
onewire_last_discrepancy = 0;
|
|
|
|
onewire_last_device_flag = false;
|
|
|
|
onewire_last_family_discrepancy = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
OneWireWrite(W1_SEARCH_ROM);
|
|
|
|
do {
|
2021-03-09 17:01:29 +00:00
|
|
|
if (!DS18X20Data.dual_mode) {
|
2020-03-10 15:54:06 +00:00
|
|
|
id_bit = OneWire1ReadBit();
|
|
|
|
cmp_id_bit = OneWire1ReadBit();
|
|
|
|
} else {
|
|
|
|
id_bit = OneWire2ReadBit();
|
|
|
|
cmp_id_bit = OneWire2ReadBit();
|
|
|
|
}
|
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
|
|
|
if ((id_bit == 1) && (cmp_id_bit == 1)) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (id_bit != cmp_id_bit) {
|
|
|
|
search_direction = id_bit;
|
|
|
|
} else {
|
|
|
|
if (id_bit_number < onewire_last_discrepancy) {
|
|
|
|
search_direction = ((onewire_rom_id[rom_byte_number] & rom_byte_mask) > 0);
|
|
|
|
} else {
|
|
|
|
search_direction = (id_bit_number == onewire_last_discrepancy);
|
|
|
|
}
|
|
|
|
if (search_direction == 0) {
|
|
|
|
last_zero = id_bit_number;
|
|
|
|
if (last_zero < 9) {
|
|
|
|
onewire_last_family_discrepancy = last_zero;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (search_direction == 1) {
|
|
|
|
onewire_rom_id[rom_byte_number] |= rom_byte_mask;
|
|
|
|
} else {
|
|
|
|
onewire_rom_id[rom_byte_number] &= ~rom_byte_mask;
|
|
|
|
}
|
|
|
|
OneWireWriteBit(search_direction);
|
|
|
|
id_bit_number++;
|
|
|
|
rom_byte_mask <<= 1;
|
|
|
|
if (rom_byte_mask == 0) {
|
|
|
|
rom_byte_number++;
|
|
|
|
rom_byte_mask = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (rom_byte_number < 8);
|
|
|
|
if (!(id_bit_number < 65)) {
|
|
|
|
onewire_last_discrepancy = last_zero;
|
|
|
|
if (onewire_last_discrepancy == 0) {
|
|
|
|
onewire_last_device_flag = true;
|
|
|
|
}
|
|
|
|
search_result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!search_result || !onewire_rom_id[0]) {
|
|
|
|
onewire_last_discrepancy = 0;
|
|
|
|
onewire_last_device_flag = false;
|
|
|
|
onewire_last_family_discrepancy = 0;
|
|
|
|
search_result = false;
|
|
|
|
}
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < 8; i++) {
|
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
|
|
|
newAddr[i] = onewire_rom_id[i];
|
|
|
|
}
|
|
|
|
return search_result;
|
|
|
|
}
|
|
|
|
|
2021-03-09 17:01:29 +00:00
|
|
|
bool OneWireCrc8(uint8_t *addr) {
|
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
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 8; i; i--) {
|
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 mix = (crc ^ inbyte) & 0x01;
|
|
|
|
crc >>= 1;
|
|
|
|
if (mix) {
|
|
|
|
crc ^= 0x8C;
|
|
|
|
}
|
|
|
|
inbyte >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (crc == *addr); // addr 8
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2021-03-09 17:01:29 +00:00
|
|
|
void Ds18x20Init(void) {
|
|
|
|
DS18X20Data.pin = Pin(GPIO_DSB);
|
2021-06-11 17:14:12 +01:00
|
|
|
DS18X20Data.input_mode = Settings->flag3.ds18x20_internal_pullup ? INPUT_PULLUP : INPUT; // SetOption74 - Enable internal pullup for single DS18x20 sensor
|
2020-02-18 17:10:36 +00:00
|
|
|
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_DSB_OUT)) {
|
2021-03-09 17:01:29 +00:00
|
|
|
DS18X20Data.pin_out = Pin(GPIO_DSB_OUT);
|
|
|
|
DS18X20Data.dual_mode = true; // Dual pins mode as used by Shelly
|
|
|
|
pinMode(DS18X20Data.pin_out, OUTPUT);
|
|
|
|
pinMode(DS18X20Data.pin, DS18X20Data.input_mode);
|
2020-02-05 15:28:56 +00:00
|
|
|
}
|
2018-07-12 11:19:08 +01:00
|
|
|
|
|