@@ -48,6 +48,7 @@ class UsermodTemperature : public Usermod {
4848 byte sensorFound;
4949
5050 bool enabled = true ;
51+ uint8_t resolution = 0 ; // 9bits=0, 10bits=1, 11bits=2, 12bits=3
5152
5253 bool HApublished = false ;
5354 int16_t idx = -1 ; // Domoticz virtual sensor idx
@@ -59,6 +60,7 @@ class UsermodTemperature : public Usermod {
5960 static const char _parasite[];
6061 static const char _parasitePin[];
6162 static const char _domoticzIDX[];
63+ static const char _resolution[];
6264 static const char _sensor[];
6365 static const char _temperature[];
6466 static const char _Temperature[];
@@ -135,9 +137,14 @@ float UsermodTemperature::readDallas() {
135137 case 0x28 : // DS1822
136138 case 0x3B : // DS1825
137139 case 0x42 : // DS28EA00
138- result = (data[1 ]<<4 ) | (data[0 ]>>4 ); // we only need whole part, we will add fraction when returning
139- if (data[1 ] & 0x80 ) result |= 0xF000 ; // fix negative value
140- retVal = float (result) + ((data[0 ] & 0x08 ) ? 0 .5f : 0 .0f );
140+ // 12-bit LSB = 0.0625°C (4-bit fraction)
141+ result = (data[1 ] << 8 ) | data[0 ];
142+ // Clear LSBs to match desired precision (9/10/11-bit)
143+ result &= 0xFFFF << (3 - (resolution & 3 ));
144+ retVal = float (result) * 0 .0625f ;
145+ // result = (data[1]<<4) | (data[0]>>4); // we only need whole part, we will add fraction when returning
146+ // if (data[1] & 0x80) result |= 0xF000; // fix negative value
147+ // retVal = float(result) + ((data[0] & 0x08) ? 0.5f : 0.0f);
141148 break ;
142149 }
143150 }
@@ -384,6 +391,7 @@ void UsermodTemperature::addToConfig(JsonObject &root) {
384391 top[FPSTR (_parasite)] = parasite;
385392 top[FPSTR (_parasitePin)] = parasitePin;
386393 top[FPSTR (_domoticzIDX)] = idx;
394+ top[FPSTR (_resolution)] = resolution;
387395 DEBUGUM_PRINTLN (F (" Temperature config saved." ));
388396}
389397
@@ -411,6 +419,7 @@ bool UsermodTemperature::readFromConfig(JsonObject &root) {
411419 parasite = top[FPSTR (_parasite)] | parasite;
412420 parasitePin = top[FPSTR (_parasitePin)] | parasitePin;
413421 idx = top[FPSTR (_domoticzIDX)] | idx;
422+ resolution = top[FPSTR (_resolution)] | resolution;
414423
415424 if (!initDone) {
416425 // first run: reading from cfg.json
@@ -431,14 +440,22 @@ bool UsermodTemperature::readFromConfig(JsonObject &root) {
431440 }
432441 }
433442 // use "return !top["newestParameter"].isNull();" when updating Usermod with new features
434- return !top[FPSTR (_domoticzIDX )].isNull ();
443+ return !top[FPSTR (_resolution )].isNull ();
435444}
436445
437446void UsermodTemperature::appendConfigData () {
438447 oappend (F (" addInfo('" )); oappend (String (FPSTR (_name)).c_str ()); oappend (F (" :" )); oappend (String (FPSTR (_parasite)).c_str ());
439448 oappend (F (" ',1,'<i>(if no Vcc connected)</i>');" )); // 0 is field type, 1 is actual field
440449 oappend (F (" addInfo('" )); oappend (String (FPSTR (_name)).c_str ()); oappend (F (" :" )); oappend (String (FPSTR (_parasitePin)).c_str ());
441450 oappend (F (" ',1,'<i>(for external MOSFET)</i>');" )); // 0 is field type, 1 is actual field
451+ oappend (F (" dd=addDD('" )); oappend (String (FPSTR (_name)).c_str ());
452+ oappend (F (" ','" )); oappend (String (FPSTR (_resolution)).c_str ()); oappend (F (" ');" ));
453+ oappend (F (" addO(dd,'0.5 °C (9-bit)',0);" ));
454+ oappend (F (" addO(dd,'0.25°C (10-bit)',1);" ));
455+ oappend (F (" addO(dd,'0.125°C (11-bit)',2);" ));
456+ oappend (F (" addO(dd,'0.0625°C (12-bit)',3);" ));
457+ oappend (F (" addInfo('" )); oappend (String (FPSTR (_name)).c_str ()); oappend (F (" :" )); oappend (String (FPSTR (_resolution)).c_str ());
458+ oappend (F (" ',1,'<i>(ignored on DS18S20)</i>');" )); // 0 is field type, 1 is actual field
442459}
443460
444461float UsermodTemperature::getTemperature () {
@@ -458,6 +475,7 @@ const char UsermodTemperature::_readInterval[] PROGMEM = "read-interval-s";
458475const char UsermodTemperature::_parasite[] PROGMEM = " parasite-pwr" ;
459476const char UsermodTemperature::_parasitePin[] PROGMEM = " parasite-pwr-pin" ;
460477const char UsermodTemperature::_domoticzIDX[] PROGMEM = " domoticz-idx" ;
478+ const char UsermodTemperature::_resolution[] PROGMEM = " resolution" ;
461479const char UsermodTemperature::_sensor[] PROGMEM = " sensor" ;
462480const char UsermodTemperature::_temperature[] PROGMEM = " temperature" ;
463481const char UsermodTemperature::_Temperature[] PROGMEM = " /temperature" ;
0 commit comments