ESPHome 2025.9.0-dev
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 uint8_t status(NONE);
20 uint16_t raw_temperature(0);
21 uint16_t raw_pressure(0);
22
23 i2c::ErrorCode err = this->read_(status, raw_temperature, raw_pressure);
24 if (err != i2c::ERROR_OK) {
25 ESP_LOGCONFIG(TAG, ESP_LOG_MSG_COMM_FAIL);
26 this->mark_failed();
27 return;
28 }
29
30 switch (status) {
31 case RESERVED:
32 ESP_LOGE(TAG, "Invalid RESERVED Device Status");
33 this->mark_failed();
34 return;
35 case FAULT:
36 ESP_LOGE(TAG, "FAULT condition in the SSC or sensing element");
37 this->mark_failed();
38 return;
39 case STALE:
40 ESP_LOGE(TAG, "STALE data. Data has not been updated since last fetch");
41 this->status_set_warning();
42 break;
43 }
44}
45
47 ESP_LOGCONFIG(TAG, "TEM3200:");
48 LOG_I2C_DEVICE(this);
49 LOG_UPDATE_INTERVAL(this);
50 LOG_SENSOR(" ", "Raw Pressure", this->raw_pressure_sensor_);
51 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
52}
53
55
56i2c::ErrorCode TEM3200Component::read_(uint8_t &status, uint16_t &raw_temperature, uint16_t &raw_pressure) {
57 uint8_t response[4] = {0x00, 0x00, 0x00, 0x00};
58
59 // initiate data read
60 i2c::ErrorCode err = this->read(response, 4);
61 if (err != i2c::ERROR_OK) {
62 return err;
63 }
64
65 // extract top 2 bits of first byte for status
66 status = (ErrorCode) (response[0] & 0xc0) >> 6;
67 if (status == RESERVED || status == FAULT) {
68 return i2c::ERROR_OK;
69 }
70
71 // if data is stale; reread
72 if (status == STALE) {
73 // wait for measurement 2ms
74 delay(2);
75
76 err = this->read(response, 4);
77 if (err != i2c::ERROR_OK) {
78 return err;
79 }
80 }
81
82 // extract top 2 bits of first byte for status
83 status = (ErrorCode) (response[0] & 0xc0) >> 6;
84 if (status == RESERVED || status == FAULT) {
85 return i2c::ERROR_OK;
86 }
87
88 // extract top 6 bits of first byte and all bits of second byte for pressure
89 raw_pressure = (((response[0] & 0x3f)) << 8 | response[1]);
90
91 // extract all bytes of 3rd byte and top 3 bits of fourth byte for temperature
92 raw_temperature = ((response[2] << 3) | (response[3] & 0xe0) >> 5);
93
94 return i2c::ERROR_OK;
95}
96
97inline float convert_temperature(uint16_t raw_temperature) {
98 const float temperature_bits_span = 2048;
99 const float temperature_max = 150;
100 const float temperature_min = -50;
101 const float temperature_span = temperature_max - temperature_min;
102
103 float temperature = (raw_temperature * temperature_span / temperature_bits_span) + temperature_min;
104
105 return temperature;
106}
107
109 uint8_t status(NONE);
110 uint16_t raw_temperature(0);
111 uint16_t raw_pressure(0);
112 i2c::ErrorCode err = this->read_(status, raw_temperature, raw_pressure);
113
114 if (err != i2c::ERROR_OK) {
115 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
116 this->status_set_warning();
117 return;
118 }
119
120 switch (status) {
121 case RESERVED:
122 ESP_LOGE(TAG, "Failed: Device return RESERVED status");
123 this->status_set_warning();
124 return;
125 case FAULT:
126 ESP_LOGE(TAG, "Failed: FAULT condition in the SSC or sensing element");
127 this->mark_failed();
128 return;
129 case STALE:
130 ESP_LOGE(TAG, "Warning: STALE data. Data has not been updated since last fetch");
131 this->status_set_warning();
132 return;
133 }
134
135 float temperature = convert_temperature(raw_temperature);
136
137 ESP_LOGD(TAG, "Got raw pressure=%d, temperature=%.1f°C", raw_pressure, temperature);
138
139 if (this->temperature_sensor_ != nullptr)
140 this->temperature_sensor_->publish_state(temperature);
141 if (this->raw_pressure_sensor_ != nullptr)
142 this->raw_pressure_sensor_->publish_state(raw_pressure);
143
144 this->status_clear_warning();
145}
146
147} // namespace tem3200
148} // 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=nullptr)
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:45
i2c::ErrorCode read_(uint8_t &status, uint16_t &raw_temperature, uint16_t &raw_pressure)
Definition tem3200.cpp:56
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:54
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:50
float convert_temperature(uint16_t raw_temperature)
Definition tem3200.cpp:97
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