v5.9.1i - Rewrite DS18x20 sensor code

5.9.1i
 * Fix Arilux LC11 restart exception 0 after OTA upgrade
 *
Disabled CRC lookup-table in OneWire.h (#define ONEWIRE_CRC8_TABLE 0) to
save some code space
 * Rewrite xsns_05_ds18x20.ino adding support for
DS1822, correct address calculation and force setting 12-bit resolution
(#1222)
 * DS18x20 sensor reconfiguration now only probed at restart
removing dynamic connection and intermittent sensor loss (#1215)
This commit is contained in:
arendst 2017-11-24 17:26:20 +01:00
parent d93aa17c94
commit 512566c727
10 changed files with 109 additions and 100 deletions

View File

@ -1,7 +1,7 @@
## Sonoff-Tasmota
Provide ESP8266 based Sonoff by [iTead Studio](https://www.itead.cc/) and ElectroDragon IoT Relay with Serial, Web and MQTT control allowing 'Over the Air' or OTA firmware updates using Arduino IDE.
Current version is **5.9.1h** - See [sonoff/_releasenotes.ino](https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/_releasenotes.ino) for change information.
Current version is **5.9.1i** - See [sonoff/_releasenotes.ino](https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/_releasenotes.ino) for change information.
### ATTENTION All versions

View File

@ -40,7 +40,7 @@
// old versions of OneWire). If you disable this, a slower
// but very compact algorithm is used.
#ifndef ONEWIRE_CRC8_TABLE
//#define ONEWIRE_CRC8_TABLE 1
#define ONEWIRE_CRC8_TABLE 0
#endif
// You can allow 16-bit CRC checks by defining this to 1

View File

@ -1,4 +1,10 @@
/* 5.9.1h
/* 5.9.1i
* Fix Arilux LC11 restart exception 0 after OTA upgrade
* Disabled CRC lookup-table in OneWire.h (#define ONEWIRE_CRC8_TABLE 0) to save some code space
* Rewrite xsns_05_ds18x20.ino adding support for DS1822, correct address calculation and force setting 12-bit resolution (#1222)
* DS18x20 sensor reconfiguration now only probed at restart removing dynamic connection and intermittent sensor loss (#1215)
*
* 5.9.1h
* Patch library I2Cdevlib-Core for esp8266-core 2.4.0-rc2
* Add option PUSHBUTTON_TOGGLE (SwitchMode 7) to allow toggling on any switch change (#1221)
* Fix DHT sensor timeout recognition by distinguish "signal already there" from "timeout" (#1233)

View File

@ -437,9 +437,10 @@
#define D_ENERGY_YESTERDAY "Energie gestern"
#define D_ENERGY_TOTAL "Energie insgesamt"
// xsns_05_ds18b20.ino
// xsns_05_ds18x20.ino
#define D_SENSOR_BUSY "Sensor beschäftigt"
#define D_SENSOR_CRC_ERROR "Sensor CRC-Fehler"
#define D_SENSORS_FOUND "Sensor gefunden"
// xsns_06_dht.ino
#define D_TIMEOUT_WAITING_FOR "Timeout wartet auf"

View File

@ -437,9 +437,10 @@
#define D_ENERGY_YESTERDAY "Energy Yesterday"
#define D_ENERGY_TOTAL "Energy Total"
// xsns_05_ds18b20.ino
// xsns_05_ds18x20.ino
#define D_SENSOR_BUSY "Sensor busy"
#define D_SENSOR_CRC_ERROR "Sensor CRC error"
#define D_SENSORS_FOUND "Sensors found"
// xsns_06_dht.ino
#define D_TIMEOUT_WAITING_FOR "Timeout waiting for"

View File

@ -437,9 +437,10 @@
#define D_ENERGY_YESTERDAY "Verbruik gisteren"
#define D_ENERGY_TOTAL "Verbruik totaal"
// xsns_05_ds18b20.ino
// xsns_05_ds18x20.ino
#define D_SENSOR_BUSY "Sensor bezet"
#define D_SENSOR_CRC_ERROR "Sensor CRC fout"
#define D_SENSORS_FOUND "Aantal sensoren"
// xsns_06_dht.ino
#define D_TIMEOUT_WAITING_FOR "Te lang wachten op"

View File

@ -437,9 +437,10 @@
#define D_ENERGY_YESTERDAY "Energia Wczoraj"
#define D_ENERGY_TOTAL "Energia suma"
// xsns_05_ds18b20.ino
// xsns_05_ds18x20.ino
#define D_SENSOR_BUSY "Czujnik DS18x20 zajety"
#define D_SENSOR_CRC_ERROR "Czujnik DS18x20 blad CRC"
#define D_SENSORS_FOUND "Znaleziono Czujnik DS18x20"
// xsns_06_dht.ino
#define D_TIMEOUT_WAITING_FOR "Trwa oczekiwanie"

View File

@ -25,7 +25,7 @@
- Select IDE Tools - Flash Size: "1M (no SPIFFS)"
====================================================*/
#define VERSION 0x05090108 // 5.9.1h
#define VERSION 0x05090109 // 5.9.1i
// Location specific includes
#include "sonoff.h" // Enumaration used in user_config.h
@ -864,7 +864,7 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
payload = (int16_t) lnum; // -32766 - 32767
payload16 = (uint16_t) lnum; // 0 - 65535
}
backlog_delay = MIN_BACKLOG_DELAY; // Reset backlog delay
backlog_delay = MIN_BACKLOG_DELAY; // Reset backlog delay
if ((GetCommandCode(command, sizeof(command), dataBuf, kOptionOff) >= 0) || !strcasecmp(dataBuf, Settings.state_text[0])) {
payload = 0;
@ -2207,6 +2207,12 @@ void StateLoop()
#endif // USE_IR_RECEIVE
#endif // USE_IR_REMOTE
#ifdef USE_ARILUX_RF
if (pin[GPIO_ARIRFRCV] < 99) {
AriluxRfHandler();
}
#endif // USE_ARILUX_RF
/*-------------------------------------------------------------------------------------------*\
* Every 0.05 second
\*-------------------------------------------------------------------------------------------*/
@ -2268,6 +2274,9 @@ void StateLoop()
StopWebserver();
}
#endif // USE_WEBSERVER
#ifdef USE_ARILUX_RF
AriluxRfDisable(); // Prevent restart exception on Arilux Interrupt routine
#endif // USE_ARILUX_RF
ota_state_flag = 92;
ota_result = 0;
ota_retry_counter--;
@ -2276,19 +2285,19 @@ void StateLoop()
// AddLog(LOG_LEVEL_INFO);
ota_result = (HTTP_UPDATE_FAILED != ESPhttpUpdate.update(Settings.ota_url));
if (!ota_result) {
ota_state_flag = 2;
ota_state_flag = 2; // Upgrade failed - retry
}
}
}
if (90 == ota_state_flag) { // Allow MQTT to reconnect
if (90 == ota_state_flag) { // Allow MQTT to reconnect
ota_state_flag = 0;
if (ota_result) {
SetFlashModeDout(); // Force DOUT for both ESP8266 and ESP8285
SetFlashModeDout(); // Force DOUT for both ESP8266 and ESP8285
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR(D_SUCCESSFUL ". " D_RESTARTING));
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR(D_FAILED " %s"), ESPhttpUpdate.getLastErrorString().c_str());
}
restart_flag = 2; // Restart anyway to keep memory clean webserver
restart_flag = 2; // Restart anyway to keep memory clean webserver
MqttPublishPrefixTopic_P(1, PSTR(D_CMND_UPGRADE));
}
}
@ -2377,7 +2386,7 @@ void SerialInput()
serial_in_byte = 0;
} else {
if (serial_in_byte != 0xA1) {
dual_button_code = 0; // 0xA1 - End of Sonoff dual button code
dual_button_code = 0; // 0xA1 - End of Sonoff dual button code
}
}
}
@ -2754,12 +2763,6 @@ void loop()
}
#endif // USE_EMULATION
#ifdef USE_ARILUX_RF
if (pin[GPIO_ARIRFRCV] < 99) {
AriluxRfHandler();
}
#endif // USE_ARILUX_RF
if (millis() >= state_loop_timer) {
StateLoop();
}

View File

@ -163,7 +163,7 @@
// -- Sensor code selection -----------------------
#define USE_ADC_VCC // Display Vcc in Power status. Disable for use as Analog input on selected devices
//#define USE_DS18x20 // Optional for multiple DS18B20 and/or DS18S20 sensors using library OneWire (+2k code)
#define USE_DS18x20 // Optional for more than one DS18B20 and/or DS18S20 sensors using library OneWire (+1.5k code)
#define USE_I2C // I2C using library wire (+10k code, 0.2k mem) - Disable by //
#define USE_SHT // Add I2C emulating code for SHT1X sensor

View File

@ -22,73 +22,58 @@
* DS18B20 - Temperature
\*********************************************************************************************/
#define DS18S20_CHIPID 0x10
#define DS18B20_CHIPID 0x28
#define MAX31850_CHIPID 0x3B
#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_WRITE_EEPROM 0x48
#define W1_WRITE_SCRATCHPAD 0x4E
#define W1_READ_SCRATCHPAD 0xBE
#define DS18X20_MAX_SENSORS 8
const char kDs18x20Types[] PROGMEM = "DS18x20|DS18S20|DS1822|DS18B20|MAX31850";
#include <OneWire.h>
OneWire *ds = NULL;
uint8_t ds18x20_chipids[] = { 0, DS18S20_CHIPID, DS1822_CHIPID, DS18B20_CHIPID, MAX31850_CHIPID };
uint8_t ds18x20_address[DS18X20_MAX_SENSORS][8];
uint8_t ds18x20_index[DS18X20_MAX_SENSORS];
uint8_t ds18x20_index[DS18X20_MAX_SENSORS] = { 0 };
uint8_t ds18x20_sensors = 0;
char ds18x20_types[9];
void Ds18x20Init()
{
ds = new OneWire(pin[GPIO_DSB]);
}
void Ds18x20Search()
{
uint8_t num_sensors=0;
uint8_t sensor = 0;
ds->reset_search();
for (num_sensors = 0; num_sensors < DS18X20_MAX_SENSORS; num_sensors) {
if (!ds->search(ds18x20_address[num_sensors])) {
for (ds18x20_sensors = 0; ds18x20_sensors < DS18X20_MAX_SENSORS; ds18x20_sensors) {
if (!ds->search(ds18x20_address[ds18x20_sensors])) {
ds->reset_search();
break;
}
// If CRC Ok and Type DS18S20, DS18B20 or MAX31850
if ((OneWire::crc8(ds18x20_address[num_sensors], 7) == ds18x20_address[num_sensors][7]) &&
((ds18x20_address[num_sensors][0]==DS18S20_CHIPID) || (ds18x20_address[num_sensors][0]==DS18B20_CHIPID) || (ds18x20_address[num_sensors][0]==MAX31850_CHIPID))) {
num_sensors++;
if ((OneWire::crc8(ds18x20_address[ds18x20_sensors], 7) == ds18x20_address[ds18x20_sensors][7]) &&
((ds18x20_address[ds18x20_sensors][0] == DS18S20_CHIPID) ||
(ds18x20_address[ds18x20_sensors][0] == DS1822_CHIPID) ||
(ds18x20_address[ds18x20_sensors][0] == DS18B20_CHIPID) ||
(ds18x20_address[ds18x20_sensors][0] == MAX31850_CHIPID))) {
ds18x20_index[ds18x20_sensors] = ds18x20_sensors;
ds18x20_sensors++;
}
}
for (byte i = 0; i < num_sensors; i++) {
ds18x20_index[i] = i;
}
for (byte i = 0; i < num_sensors; i++) {
for (byte j = i + 1; j < num_sensors; j++) {
for (byte i = 0; i < ds18x20_sensors; i++) {
for (byte j = i + 1; j < ds18x20_sensors; j++) {
if (uint32_t(ds18x20_address[ds18x20_index[i]]) > uint32_t(ds18x20_address[ds18x20_index[j]])) {
std::swap(ds18x20_index[i], ds18x20_index[j]);
}
}
}
ds18x20_sensors = num_sensors;
}
uint8_t Ds18x20Sensors()
{
return ds18x20_sensors;
}
String Ds18x20Addresses(uint8_t sensor)
{
char address[20];
for (byte i = 0; i < 8; i++) {
sprintf(address+2*i, "%02X", ds18x20_address[ds18x20_index[sensor]][i]);
}
return String(address);
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DSB D_SENSORS_FOUND " %d"), ds18x20_sensors);
AddLog(LOG_LEVEL_DEBUG);
}
void Ds18x20Convert()
@ -117,7 +102,7 @@ boolean Ds18x20Read(uint8_t sensor, float &t)
}
if (OneWire::crc8(data, 8) == data[8]) {
switch(ds18x20_address[ds18x20_index[sensor]][0]) {
case DS18S20_CHIPID: // DS18S20
case DS18S20_CHIPID:
if (data[1] > 0x80) {
data[0] = (~data[0]) +1;
sign = -1; // App-Note fix possible sign error
@ -129,8 +114,20 @@ boolean Ds18x20Read(uint8_t sensor, float &t)
}
t = ConvertTemp((temp9 - 0.25) + ((16.0 - data[6]) / 16.0));
break;
case DS18B20_CHIPID: // DS18B20
case MAX31850_CHIPID: // MAX31850
case DS1822_CHIPID:
case DS18B20_CHIPID:
if (data[4] != 0x7F) {
data[4] = 0x7F; // Set resolution to 12-bit
ds->reset();
ds->select(ds18x20_address[ds18x20_index[sensor]]);
ds->write(W1_WRITE_SCRATCHPAD); // Write Scratchpad
ds->write(data[2]); // Th Register
ds->write(data[3]); // Tl Register
ds->write(data[4]); // Configuration Register
ds->select(ds18x20_address[ds18x20_index[sensor]]);
ds->write(W1_WRITE_EEPROM); // Save scratchpad to EEPROM
}
case MAX31850_CHIPID:
uint16_t temp12 = (data[1] << 8) + data[0];
if (temp12 > 2047) {
temp12 = (~temp12) +1;
@ -139,71 +136,70 @@ boolean Ds18x20Read(uint8_t sensor, float &t)
t = ConvertTemp(sign * temp12 * 0.0625);
break;
}
} else {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DSB D_SENSOR_CRC_ERROR));
}
return (!isnan(t));
}
/********************************************************************************************/
void Ds18x20Type(uint8_t sensor)
{
strcpy_P(ds18x20_types, PSTR("DS18x20"));
switch(ds18x20_address[ds18x20_index[sensor]][0]) {
case DS18S20_CHIPID:
strcpy_P(ds18x20_types, PSTR("DS18S20"));
break;
case DS18B20_CHIPID:
strcpy_P(ds18x20_types, PSTR("DS18B20"));
break;
case MAX31850_CHIPID:
strcpy_P(ds18x20_types, PSTR("MAX31850"));
break;
}
}
void Ds18x20Show(boolean json)
{
char temperature[10];
char stemp[10];
char stemp[12];
char separator[2] = { '\0' };
float t;
bool domoticz_flag = true;
byte dsxflg = 0;
for (byte i = 0; i < Ds18x20Sensors(); i++) {
for (byte i = 0; i < ds18x20_sensors; i++) {
if (Ds18x20Read(i, t)) { // Check if read failed
Ds18x20Type(i);
dtostrfd(t, Settings.flag2.temperature_resolution, temperature);
if (json) {
if (!dsxflg) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"DS18x20\":{"), mqtt_data);
stemp[0] = '\0';
uint8_t index = sizeof(ds18x20_chipids);
while (index) {
if (ds18x20_address[ds18x20_index[i]][0] == ds18x20_chipids[index]) {
break;
}
index--;
}
GetTextIndexed(ds18x20_types, sizeof(ds18x20_types), index, kDs18x20Types);
if (json) {
if (1 == ds18x20_sensors) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"" D_TEMPERATURE "\":%s}"), mqtt_data, ds18x20_types, temperature);
} else {
char address[17];
// for (byte j = 0; j < 8; j++) {
// sprintf(address+2*j, "%02X", ds18x20_address[ds18x20_index[i]][j]);
// }
for (byte j = 0; j < 6; j++) {
sprintf(address+2*j, "%02X", ds18x20_address[ds18x20_index[i]][6-j]); // Skip sensor type and crc
}
if (!separator[0]) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"DS18x20\":{"), mqtt_data);
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"DS%d\":{\"" D_TYPE "\":\"%s\",\"" D_ADDRESS "\":\"%s\",\"" D_TEMPERATURE "\":%s}"),
mqtt_data, separator, i +1, ds18x20_types, address, temperature);
separator[0] = ',';
}
dsxflg++;
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s\"DS%d\":{\"" D_TYPE "\":\"%s\",\"" D_ADDRESS "\":\"%s\",\"" D_TEMPERATURE "\":%s}"),
mqtt_data, stemp, i +1, ds18x20_types, Ds18x20Addresses(i).c_str(), temperature);
strcpy(stemp, ",");
#ifdef USE_DOMOTICZ
if (1 == dsxflg) {
if (domoticz_flag) {
DomoticzSensor(DZ_TEMP, temperature);
domoticz_flag = false;
}
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
snprintf_P(stemp, sizeof(stemp), PSTR("%s-%d"), ds18x20_types, i +1);
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, stemp, temperature, TempUnit());
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, (1 == ds18x20_sensors) ? ds18x20_types : stemp, temperature, TempUnit());
#endif // USE_WEBSERVER
}
}
}
if (json) {
if (dsxflg) {
if (separator[0]) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
}
#ifdef USE_WEBSERVER
} else {
Ds18x20Search(); // Check for changes in sensors number
Ds18x20Convert(); // Start Conversion, takes up to one second
#endif // USE_WEBSERVER
}
}
@ -223,8 +219,7 @@ boolean Xsns05(byte function)
Ds18x20Init();
break;
case FUNC_XSNS_PREP:
Ds18x20Search(); // Check for changes in sensors number
Ds18x20Convert(); // Start Conversion, takes up to one second
Ds18x20Convert(); // Check for changes in sensor number and start conversion, takes up to one second
break;
case FUNC_XSNS_JSON_APPEND:
Ds18x20Show(1);
@ -232,6 +227,7 @@ boolean Xsns05(byte function)
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
Ds18x20Show(0);
Ds18x20Convert(); // Check for changes in sensor number and start conversion, takes up to one second
break;
#endif // USE_WEBSERVER
}