Tasmota/sonoff/xsns_ds18b20.ino
arendst e08d96acc2 v5.9.0
5.9.0 20171030
* Rewrite code (partly) using Google C++ Style Guide
(https://google.github.io/styleguide/cppguide.html)
* Rewrite code by using command lookup tables and javascript (client
side) web page expansions
* Change HTML/CSS to enable nicer form field entry
* Change default PWM assignments for H801 RGB(CW) led controller to
support optional Color/Dimmer control
*   GPIO04 (W2)    from GPIO_PWM2 to GPIO_USER to be user configurable
for GPIO_PWM5 (second White - Warm if W1 is Cold)
*   GPIO12 (Blue)  GPIO_PWM3 no change
*   GPIO13 (Green) from GPIO_PWM4 to GPIO_PWM2
*   GPIO14 (W1)    from GPIO_PWM1 to GPIO_USER to be user configurable
for GPIO_PWM4 (first White - Cold or Warm)
*   GPIO15 (Red)   from GPIO_PWM5 to GPIO_PWM1
* Change default PWM assignments for MagicHome RGB(W) led controller to
support optional Color/Dimmer control
*   GPIO05 (Green) from GPIO_PWM4 to GPIO_PWM2
*   GPIO12 (Blue)  from GPIO_PWM5 to GPIO_PWM3
*   GPIO13 (White) GPIO_USER to be user configurable for GPIO_PWM4
(White - Cold or Warm)
*   GPIO14 (Red)   from GPIO_PWM3 to GPIO_PWM1
* Change default PWM assignment for Witty Cloud to support optional
Color/Dimmer control (#976)
*   GPIO12 (Green) from GPIO_PWM4 to GPIO_PWM2
*   GPIO13 (Blue)  from GPIO_PWM5 to GPIO_PWM3
*   GPIO15 (Red)   from GPIO_PWM3 to GPIO_PWM1
* Change when another module is selected now all GPIO user configuration
is removed
* Change command name IRRemote to IRSend (#956)
* Remove Arduino IDE version too low warning as it interferes with
platformio.ini platform = espressif8266_stage
* Fix command FullTopic entry when using serial or console interface
* Fix possible UDP syslog blocking
* Fix minimum TelePeriod of 10 seconds set by web page
* Fix command GPIOx JSON response (#897)
* Fix inverted relay power on state (#909)
* Fix compile error when DOMOTICZ_UPDATE_TIMER is not defined (#930)
* Fix alignment of web page items in some browsers (#935)
* Fix setting all saved power settings to Off when SetOption0
(SaveState) = 0 (#955)
* Fix timezone range from -12/12 to -13/13 (#968)
* Fix Southern Hemisphere TIME_STD/TIME_DST (#968)
* Fix TLS MQTT SSL fingerprint test (#970, #808)
* Fix virtual relay status message used with Color/Dimmer control (#989)
* Fix command IRSend and IRHvac case sensitive parameter regression
introduced with version 5.8.0 (#993)
* Fix pressure calculation for some BMP versions regression introduced
with version 5.8.0i (#974)
* Fix Domoticz Dimmer set to same level not powering on (#945)
* Fix Blocked Loop when erasing large flash using command reset 2
(#1002)
* Fix relay power control when light power control is also configured as
regression from 5.8.0 (#1016)
* Fix Mqtt server mDNS lookup only when MqttHost name is empty (#1026)
* Add debug information to MQTT subscribe
* Add translations to I2Cscan
* Add translation to BH1750 unit lx
* Add light scheme options (Color cycle Up, Down, Random) and moving
WS2812 schemes up by 3
* Add Domoticz counter sensor to IrReceive representing Received IR
Protocol and Data
* Add option 0 to MqttHost to allow empty Mqtt host name
* Add support for Arilux AL-LC01 RGB Led controller (#370)
* Add esp8266 de-blocking to PubSubClient library (#790)
* Add Domoticz sensors for Voltage and Current (#903)
* Add platformio OTA upload support (#928, #934)
* Add warning to webpage when USE_MINIMAL is selected (#929)
* Add smoother movement of hour hand in WS2812 led clock (#936)
* Add support for Magic Home RGBW and some Arilux Led controllers (#940)
* Add command SetOption15 0 (default) for command PWM control or
SetOption15 1 for commands Color/Dimmer control to PWM RGB(CW) leds
(#941)
* Add Domoticz counter sensor to Sonoff Bridge representing Received RF
code (#943)
* Add support for Luani HVIO board
(https://luani.de/projekte/esp8266-hvio/) (#953)
* Add PWM initialization after restart (#955)
* Add IR Receiver support. Disable in user_config.h (#956)
* Add support for inverted PWM (#960)
* Add Sea level pressure calculation and Provide command Altitude (#974)
* Add support for up to 8 relays (#995)
* Add commands RfSync, RfLow, RfHigh, RfHost and RfCode to allow sending
custom RF codes (#1001)
* Add retain to ENERGY messages controlled by command SensorRetain
(#1013)
* Add commands Color2, Color3, Color4, Width2, Width3, Width4 and
SetOption16 to set Ws2812 Clock parameters (#1019)
* Add German language file (#1022)
* Add support for connecting to MQTT brokers without userid and/or
password (#1023)
* Add support for esp8266 core v2.4.0-rc2 (#1024)
* Add commands PwmRange 1,255..1023 and PwmFrequency 1,100..4000 (#1025)
* Add Polish language file (#1044, #1047)
* Add support for KMC 70011 Power Monitoring Smart Plug (#1045)
* Add support for VEML6070 I2C Ultra Violet level sensor (#1053)
* Add light turn Off Fade (#925)
* Add IrSend command option Panasonic as IrSend {"Protocol":"Panasonic",
"Bits":16388, "Data":<Panasonic data>}
*   where 16388 is 0x4004 hexadecimal (#1014)
* Add retry counter to DHT11/21/22 sensors (#1082)
2017-10-30 15:43:39 +01:00

223 lines
5.8 KiB
C++

/*
xsns_ds18b20.ino - DS18B20 temperature sensor support for Sonoff-Tasmota
Copyright (C) 2017 Theo Arends
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/>.
*/
#ifdef USE_DS18B20
/*********************************************************************************************\
* DS18B20 - Temperature
*
* Source: Marinus vd Broek https://github.com/ESP8266nu/ESPEasy and AlexTransit (CRC)
\*********************************************************************************************/
float ds18b20_last_temperature = 0;
uint16_t ds18b20_last_result = 0;
uint8_t Ds18b20Reset()
{
uint8_t r;
uint8_t retries = 125;
pinMode(pin[GPIO_DSB], INPUT);
do { // wait until the wire is high... just in case
if (--retries == 0) {
return 0;
}
delayMicroseconds(2);
} while (!digitalRead(pin[GPIO_DSB]));
pinMode(pin[GPIO_DSB], OUTPUT);
digitalWrite(pin[GPIO_DSB], LOW);
delayMicroseconds(492); // Dallas spec. = Min. 480uSec. Arduino 500uSec.
pinMode(pin[GPIO_DSB], INPUT); // Float
delayMicroseconds(40);
r = !digitalRead(pin[GPIO_DSB]);
delayMicroseconds(420);
return r;
}
uint8_t Ds18b20ReadBit(void)
{
uint8_t r;
pinMode(pin[GPIO_DSB], OUTPUT);
digitalWrite(pin[GPIO_DSB], LOW);
delayMicroseconds(3);
pinMode(pin[GPIO_DSB], INPUT); // let pin float, pull up will raise
delayMicroseconds(10);
r = digitalRead(pin[GPIO_DSB]);
delayMicroseconds(53);
return r;
}
uint8_t Ds18b20Read(void)
{
uint8_t bit_mask;
uint8_t r = 0;
for (bit_mask = 1; bit_mask; bit_mask <<= 1) {
if (Ds18b20ReadBit()) {
r |= bit_mask;
}
}
return r;
}
void Ds18b20WriteBit(uint8_t v)
{
if (v & 1) {
digitalWrite(pin[GPIO_DSB], LOW);
pinMode(pin[GPIO_DSB], OUTPUT);
delayMicroseconds(10);
digitalWrite(pin[GPIO_DSB], HIGH);
delayMicroseconds(55);
} else {
digitalWrite(pin[GPIO_DSB], LOW);
pinMode(pin[GPIO_DSB], OUTPUT);
delayMicroseconds(65);
digitalWrite(pin[GPIO_DSB], HIGH);
delayMicroseconds(5);
}
}
void Ds18b20Write(uint8_t byte_to_write)
{
uint8_t bit_mask;
for (bit_mask = 1; bit_mask; bit_mask <<= 1) {
Ds18b20WriteBit((bit_mask & byte_to_write) ? 1 : 0);
}
}
uint8 Ds18b20Crc(uint8 inp, uint8 crc)
{
inp ^= crc;
crc = 0;
if (inp & 0x1) crc ^= 0x5e;
if (inp & 0x2) crc ^= 0xbc;
if (inp & 0x4) crc ^= 0x61;
if (inp & 0x8) crc ^= 0xc2;
if (inp & 0x10) crc ^= 0x9d;
if (inp & 0x20) crc ^= 0x23;
if (inp & 0x40) crc ^= 0x46;
if (inp & 0x80) crc ^= 0x8c;
return crc;
}
void Ds18b20ReadTempPrep()
{
Ds18b20Reset();
Ds18b20Write(0xCC); // Skip ROM
Ds18b20Write(0x44); // Start conversion
}
boolean Ds18b20ReadTemperature(float &t)
{
int16_t DSTemp;
byte msb, lsb, crc, sign = 1;
if (!ds18b20_last_temperature) {
t = NAN;
} else {
ds18b20_last_result++;
if (ds18b20_last_result > 8) { // Reset after 8 misses
ds18b20_last_temperature = NAN;
}
t = ds18b20_last_temperature;
}
if (!Ds18b20ReadBit()) { //check measurement end
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DSB D_SENSOR_BUSY));
return !isnan(t);
}
/*
Ds18b20Reset();
Ds18b20Write(0xCC); // Skip ROM
Ds18b20Write(0x44); // Start conversion
delay(800);
*/
Ds18b20Reset();
Ds18b20Write(0xCC); // Skip ROM
Ds18b20Write(0xBE); // Read scratchpad
lsb = Ds18b20Read();
msb = Ds18b20Read();
crc = Ds18b20Crc(lsb, crc);
crc = Ds18b20Crc(msb, crc);
crc = Ds18b20Crc(Ds18b20Read(), crc);
crc = Ds18b20Crc(Ds18b20Read(), crc);
crc = Ds18b20Crc(Ds18b20Read(), crc);
crc = Ds18b20Crc(Ds18b20Read(), crc);
crc = Ds18b20Crc(Ds18b20Read(), crc);
crc = Ds18b20Crc(Ds18b20Read(), crc);
crc = Ds18b20Crc(Ds18b20Read(), crc);
Ds18b20Reset();
if (crc) { //check crc
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DSB D_SENSOR_CRC_ERROR));
} else {
DSTemp = (msb << 8) + lsb;
if (DSTemp > 2047) {
DSTemp = (~DSTemp) +1;
sign = -1;
}
t = ConvertTemp((float)sign * DSTemp * 0.0625);
ds18b20_last_result = 0;
}
if (!isnan(t)) {
ds18b20_last_temperature = t;
}
return !isnan(t);
}
/*********************************************************************************************\
* Presentation
\*********************************************************************************************/
void MqttShowDs18b20(uint8_t* djson)
{
char stemp1[10];
float t;
if (Ds18b20ReadTemperature(t)) { // Check if read failed
dtostrfd(t, Settings.flag.temperature_resolution, stemp1);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, \"DS18B20\":{\"" D_TEMPERATURE "\":%s}"), mqtt_data, stemp1);
*djson = 1;
#ifdef USE_DOMOTICZ
DomoticzSensor(DZ_TEMP, stemp1);
#endif // USE_DOMOTICZ
}
}
#ifdef USE_WEBSERVER
String WebShowDs18b20()
{
// Needs TelePeriod to refresh data (Do not do it here as it takes too much time)
String page = "";
float st;
if (Ds18b20ReadTemperature(st)) { // Check if read failed
char stemp[10];
char sensor[80];
dtostrfi(st, Settings.flag.temperature_resolution, stemp);
snprintf_P(sensor, sizeof(sensor), HTTP_SNS_TEMP, "DS18B20", stemp, TempUnit());
page += sensor;
}
Ds18b20ReadTempPrep();
return page;
}
#endif // USE_WEBSERVER
#endif // USE_DS18B20