ESPHome 2025.5.0
Loading...
Searching...
No Matches
dallas_temp.cpp
Go to the documentation of this file.
1#include "dallas_temp.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace dallas_temp {
6
7static const char *const TAG = "dallas.temp.sensor";
8
9static const uint8_t DALLAS_MODEL_DS18S20 = 0x10;
10static const uint8_t DALLAS_COMMAND_START_CONVERSION = 0x44;
11static const uint8_t DALLAS_COMMAND_READ_SCRATCH_PAD = 0xBE;
12static const uint8_t DALLAS_COMMAND_WRITE_SCRATCH_PAD = 0x4E;
13static const uint8_t DALLAS_COMMAND_COPY_SCRATCH_PAD = 0x48;
14
16 switch (this->resolution_) {
17 case 9:
18 return 94;
19 case 10:
20 return 188;
21 case 11:
22 return 375;
23 default:
24 return 750;
25 }
26}
27
29 ESP_LOGCONFIG(TAG, "Dallas Temperature Sensor:");
30 if (this->address_ == 0) {
31 ESP_LOGW(TAG, " Unable to select an address");
32 return;
33 }
34 LOG_ONE_WIRE_DEVICE(this);
35 ESP_LOGCONFIG(TAG, " Resolution: %u bits", this->resolution_);
36 LOG_UPDATE_INTERVAL(this);
37}
38
40 if (this->address_ == 0)
41 return;
42
44
45 this->send_command_(DALLAS_COMMAND_START_CONVERSION);
46
47 this->set_timeout(this->get_address_name(), this->millis_to_wait_for_conversion_(), [this] {
48 if (!this->read_scratch_pad_() || !this->check_scratch_pad_()) {
49 this->publish_state(NAN);
50 return;
51 }
52
53 float tempc = this->get_temp_c_();
54 ESP_LOGD(TAG, "'%s': Got Temperature=%.1f°C", this->get_name().c_str(), tempc);
55 this->publish_state(tempc);
56 });
57}
58
60 bool success = this->send_command_(DALLAS_COMMAND_READ_SCRATCH_PAD);
61 if (success) {
62 for (uint8_t &i : this->scratch_pad_) {
63 i = this->bus_->read8();
64 }
65 } else {
66 ESP_LOGW(TAG, "'%s' - reading scratch pad failed bus reset", this->get_name().c_str());
67 this->status_set_warning("bus reset failed");
68 }
69 return success;
70}
71
73 ESP_LOGCONFIG(TAG, "setting up Dallas temperature sensor...");
74 if (!this->check_address_())
75 return;
76 if (!this->read_scratch_pad_())
77 return;
78 if (!this->check_scratch_pad_())
79 return;
80
81 if ((this->address_ & 0xff) == DALLAS_MODEL_DS18S20) {
82 // DS18S20 doesn't support resolution.
83 ESP_LOGW(TAG, "DS18S20 doesn't support setting resolution.");
84 return;
85 }
86
87 uint8_t res;
88 switch (this->resolution_) {
89 case 12:
90 res = 0x7F;
91 break;
92 case 11:
93 res = 0x5F;
94 break;
95 case 10:
96 res = 0x3F;
97 break;
98 case 9:
99 default:
100 res = 0x1F;
101 break;
102 }
103
104 if (this->scratch_pad_[4] == res)
105 return;
106 this->scratch_pad_[4] = res;
107
108 if (this->send_command_(DALLAS_COMMAND_WRITE_SCRATCH_PAD)) {
109 this->bus_->write8(this->scratch_pad_[2]); // high alarm temp
110 this->bus_->write8(this->scratch_pad_[3]); // low alarm temp
111 this->bus_->write8(this->scratch_pad_[4]); // resolution
112 }
113
114 // write value to EEPROM
115 this->send_command_(DALLAS_COMMAND_COPY_SCRATCH_PAD);
116}
117
119 bool chksum_validity = (crc8(this->scratch_pad_, 8) == this->scratch_pad_[8]);
120
121#ifdef ESPHOME_LOG_LEVEL_VERY_VERBOSE
122 ESP_LOGVV(TAG, "Scratch pad: %02X.%02X.%02X.%02X.%02X.%02X.%02X.%02X.%02X (%02X)", this->scratch_pad_[0],
123 this->scratch_pad_[1], this->scratch_pad_[2], this->scratch_pad_[3], this->scratch_pad_[4],
124 this->scratch_pad_[5], this->scratch_pad_[6], this->scratch_pad_[7], this->scratch_pad_[8],
125 crc8(this->scratch_pad_, 8));
126#endif
127 if (!chksum_validity) {
128 ESP_LOGW(TAG, "'%s' - Scratch pad checksum invalid!", this->get_name().c_str());
129 this->status_set_warning("scratch pad checksum invalid");
130 ESP_LOGD(TAG, "Scratch pad: %02X.%02X.%02X.%02X.%02X.%02X.%02X.%02X.%02X (%02X)", this->scratch_pad_[0],
131 this->scratch_pad_[1], this->scratch_pad_[2], this->scratch_pad_[3], this->scratch_pad_[4],
132 this->scratch_pad_[5], this->scratch_pad_[6], this->scratch_pad_[7], this->scratch_pad_[8],
133 crc8(this->scratch_pad_, 8));
134 }
135 return chksum_validity;
136}
137
139 int16_t temp = (this->scratch_pad_[1] << 8) | this->scratch_pad_[0];
140 if ((this->address_ & 0xff) == DALLAS_MODEL_DS18S20) {
141 return (temp >> 1) + (this->scratch_pad_[7] - this->scratch_pad_[6]) / float(this->scratch_pad_[7]) - 0.25;
142 }
143 switch (this->resolution_) {
144 case 9:
145 temp &= 0xfff8;
146 break;
147 case 10:
148 temp &= 0xfffc;
149 break;
150 case 11:
151 temp &= 0xfffe;
152 break;
153 case 12:
154 default:
155 break;
156 }
157
158 return temp / 16.0f;
159}
160
161} // namespace dallas_temp
162} // namespace esphome
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.cpp:72
const StringRef & get_name() const
uint16_t millis_to_wait_for_conversion_() const
Get the number of milliseconds we have to wait for the conversion phase.
virtual void write8(uint8_t val)=0
Write a word to the bus. LSB first.
virtual uint8_t read8()=0
Read an 8 bit word from the bus.
bool check_address_()
find an address if necessary should be called from setup
Definition one_wire.cpp:23
OneWireBus * bus_
pointer to OneWireBus instance
Definition one_wire.h:31
bool send_command_(uint8_t cmd)
send command on the bus
Definition one_wire.cpp:16
const std::string & get_address_name()
Helper to create (and cache) the name for this sensor. For example "0xfe0000031f1eaf29".
Definition one_wire.cpp:8
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t crc8(const uint8_t *data, uint8_t len)
Calculate a CRC-8 checksum of data with size len using the CRC-8-Dallas/Maxim polynomial.
Definition helpers.cpp:96