ESPHome 2025.6.3
Loading...
Searching...
No Matches
tem3200.cpp
Go to the documentation of this file.
1#include "tem3200.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace tem3200 {
8
9static const char *const TAG = "tem3200";
10
12 NONE = 0,
14 STALE = 2,
15 FAULT = 3,
16};
17
19 ESP_LOGCONFIG(TAG, "Running setup");
20
21 uint8_t status(NONE);
22 uint16_t raw_temperature(0);
23 uint16_t raw_pressure(0);
24
25 i2c::ErrorCode err = this->read_(status, raw_temperature, raw_pressure);
26 if (err != i2c::ERROR_OK) {
27 ESP_LOGCONFIG(TAG, ESP_LOG_MSG_COMM_FAIL);
28 this->mark_failed();
29 return;
30 }
31
32 switch (status) {
33 case RESERVED:
34 ESP_LOGE(TAG, "Invalid RESERVED Device Status");
35 this->mark_failed();
36 return;
37 case FAULT:
38 ESP_LOGE(TAG, "FAULT condition in the SSC or sensing element");
39 this->mark_failed();
40 return;
41 case STALE:
42 ESP_LOGE(TAG, "STALE data. Data has not been updated since last fetch");
43 this->status_set_warning();
44 break;
45 }
46}
47
49 ESP_LOGCONFIG(TAG, "TEM3200:");
50 LOG_I2C_DEVICE(this);
51 LOG_UPDATE_INTERVAL(this);
52 LOG_SENSOR(" ", "Raw Pressure", this->raw_pressure_sensor_);
53 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
54}
55
57
58i2c::ErrorCode TEM3200Component::read_(uint8_t &status, uint16_t &raw_temperature, uint16_t &raw_pressure) {
59 uint8_t response[4] = {0x00, 0x00, 0x00, 0x00};
60
61 // initiate data read
62 i2c::ErrorCode err = this->read(response, 4);
63 if (err != i2c::ERROR_OK) {
64 return err;
65 }
66
67 // extract top 2 bits of first byte for status
68 status = (ErrorCode) (response[0] & 0xc0) >> 6;
69 if (status == RESERVED || status == FAULT) {
70 return i2c::ERROR_OK;
71 }
72
73 // if data is stale; reread
74 if (status == STALE) {
75 // wait for measurement 2ms
76 delay(2);
77
78 err = this->read(response, 4);
79 if (err != i2c::ERROR_OK) {
80 return err;
81 }
82 }
83
84 // extract top 2 bits of first byte for status
85 status = (ErrorCode) (response[0] & 0xc0) >> 6;
86 if (status == RESERVED || status == FAULT) {
87 return i2c::ERROR_OK;
88 }
89
90 // extract top 6 bits of first byte and all bits of second byte for pressure
91 raw_pressure = (((response[0] & 0x3f)) << 8 | response[1]);
92
93 // extract all bytes of 3rd byte and top 3 bits of fourth byte for temperature
94 raw_temperature = ((response[2] << 3) | (response[3] & 0xe0) >> 5);
95
96 return i2c::ERROR_OK;
97}
98
99inline float convert_temperature(uint16_t raw_temperature) {
100 const float temperature_bits_span = 2048;
101 const float temperature_max = 150;
102 const float temperature_min = -50;
103 const float temperature_span = temperature_max - temperature_min;
104
105 float temperature = (raw_temperature * temperature_span / temperature_bits_span) + temperature_min;
106
107 return temperature;
108}
109
111 uint8_t status(NONE);
112 uint16_t raw_temperature(0);
113 uint16_t raw_pressure(0);
114 i2c::ErrorCode err = this->read_(status, raw_temperature, raw_pressure);
115
116 if (err != i2c::ERROR_OK) {
117 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
118 this->status_set_warning();
119 return;
120 }
121
122 switch (status) {
123 case RESERVED:
124 ESP_LOGE(TAG, "Failed: Device return RESERVED status");
125 this->status_set_warning();
126 return;
127 case FAULT:
128 ESP_LOGE(TAG, "Failed: FAULT condition in the SSC or sensing element");
129 this->mark_failed();
130 return;
131 case STALE:
132 ESP_LOGE(TAG, "Warning: STALE data. Data has not been updated since last fetch");
133 this->status_set_warning();
134 return;
135 }
136
137 float temperature = convert_temperature(raw_temperature);
138
139 ESP_LOGD(TAG, "Got raw pressure=%d, temperature=%.1f°C", raw_pressure, temperature);
140
141 if (this->temperature_sensor_ != nullptr)
142 this->temperature_sensor_->publish_state(temperature);
143 if (this->raw_pressure_sensor_ != nullptr)
144 this->raw_pressure_sensor_->publish_state(raw_pressure);
145
146 this->status_clear_warning();
147}
148
149} // namespace tem3200
150} // namespace esphome
uint8_t status
Definition bl0942.h:8
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
i2c::ErrorCode read_(uint8_t &status, uint16_t &raw_temperature, uint16_t &raw_pressure)
Definition tem3200.cpp:58
sensor::Sensor * raw_pressure_sensor_
Definition tem3200.h:26
sensor::Sensor * temperature_sensor_
Definition tem3200.h:25
float get_setup_priority() const override
Definition tem3200.cpp:56
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:20
float convert_temperature(uint16_t raw_temperature)
Definition tem3200.cpp:99
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint16_t temperature
Definition sun_gtil2.cpp:12