ESPHome 2026.2.1
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
54i2c::ErrorCode TEM3200Component::read_(uint8_t &status, uint16_t &raw_temperature, uint16_t &raw_pressure) {
55 uint8_t response[4] = {0x00, 0x00, 0x00, 0x00};
56
57 // initiate data read
58 i2c::ErrorCode err = this->read(response, 4);
59 if (err != i2c::ERROR_OK) {
60 return err;
61 }
62
63 // extract top 2 bits of first byte for status
64 status = (ErrorCode) (response[0] & 0xc0) >> 6;
65 if (status == RESERVED || status == FAULT) {
66 return i2c::ERROR_OK;
67 }
68
69 // if data is stale; reread
70 if (status == STALE) {
71 // wait for measurement 2ms
72 delay(2);
73
74 err = this->read(response, 4);
75 if (err != i2c::ERROR_OK) {
76 return err;
77 }
78 }
79
80 // extract top 2 bits of first byte for status
81 status = (ErrorCode) (response[0] & 0xc0) >> 6;
82 if (status == RESERVED || status == FAULT) {
83 return i2c::ERROR_OK;
84 }
85
86 // extract top 6 bits of first byte and all bits of second byte for pressure
87 raw_pressure = (((response[0] & 0x3f)) << 8 | response[1]);
88
89 // extract all bytes of 3rd byte and top 3 bits of fourth byte for temperature
90 raw_temperature = ((response[2] << 3) | (response[3] & 0xe0) >> 5);
91
92 return i2c::ERROR_OK;
93}
94
95inline float convert_temperature(uint16_t raw_temperature) {
96 const float temperature_bits_span = 2048;
97 const float temperature_max = 150;
98 const float temperature_min = -50;
99 const float temperature_span = temperature_max - temperature_min;
100
101 float temperature = (raw_temperature * temperature_span / temperature_bits_span) + temperature_min;
102
103 return temperature;
104}
105
107 uint8_t status(NONE);
108 uint16_t raw_temperature(0);
109 uint16_t raw_pressure(0);
110 i2c::ErrorCode err = this->read_(status, raw_temperature, raw_pressure);
111
112 if (err != i2c::ERROR_OK) {
113 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
114 this->status_set_warning();
115 return;
116 }
117
118 switch (status) {
119 case RESERVED:
120 ESP_LOGE(TAG, "Failed: Device return RESERVED status");
121 this->status_set_warning();
122 return;
123 case FAULT:
124 ESP_LOGE(TAG, "Failed: FAULT condition in the SSC or sensing element");
125 this->mark_failed();
126 return;
127 case STALE:
128 ESP_LOGE(TAG, "Warning: STALE data. Data has not been updated since last fetch");
129 this->status_set_warning();
130 return;
131 }
132
133 float temperature = convert_temperature(raw_temperature);
134
135 ESP_LOGD(TAG, "Got raw pressure=%d, temperature=%.1f°C", raw_pressure, temperature);
136
137 if (this->temperature_sensor_ != nullptr)
138 this->temperature_sensor_->publish_state(temperature);
139 if (this->raw_pressure_sensor_ != nullptr)
140 this->raw_pressure_sensor_->publish_state(raw_pressure);
141
142 this->status_clear_warning();
143}
144
145} // namespace tem3200
146} // 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) const
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:65
i2c::ErrorCode read_(uint8_t &status, uint16_t &raw_temperature, uint16_t &raw_pressure)
Definition tem3200.cpp:54
sensor::Sensor * raw_pressure_sensor_
Definition tem3200.h:25
sensor::Sensor * temperature_sensor_
Definition tem3200.h:24
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:15
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:17
float convert_temperature(uint16_t raw_temperature)
Definition tem3200.cpp:95
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:26
uint16_t temperature
Definition sun_gtil2.cpp:12