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