ESPHome 2026.5.0
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
8static const char *const 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 }
43}
44
46 ESP_LOGCONFIG(TAG, "TEM3200:");
47 LOG_I2C_DEVICE(this);
48 LOG_UPDATE_INTERVAL(this);
49 LOG_SENSOR(" ", "Raw Pressure", this->raw_pressure_sensor_);
50 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
51}
52
53i2c::ErrorCode TEM3200Component::read_(uint8_t &status, uint16_t &raw_temperature, uint16_t &raw_pressure) {
54 uint8_t response[4] = {0x00, 0x00, 0x00, 0x00};
55
56 // initiate data read
57 i2c::ErrorCode err = this->read(response, 4);
58 if (err != i2c::ERROR_OK) {
59 return err;
60 }
61
62 // extract top 2 bits of first byte for status
63 status = (ErrorCode) (response[0] & 0xc0) >> 6;
64 if (status == RESERVED || status == FAULT) {
65 return i2c::ERROR_OK;
66 }
67
68 // if data is stale; reread
69 if (status == STALE) {
70 // wait for measurement 2ms
71 delay(2);
72
73 err = this->read(response, 4);
74 if (err != i2c::ERROR_OK) {
75 return err;
76 }
77 }
78
79 // extract top 2 bits of first byte for status
80 status = (ErrorCode) (response[0] & 0xc0) >> 6;
81 if (status == RESERVED || status == FAULT) {
82 return i2c::ERROR_OK;
83 }
84
85 // extract top 6 bits of first byte and all bits of second byte for pressure
86 raw_pressure = (((response[0] & 0x3f)) << 8 | response[1]);
87
88 // extract all bytes of 3rd byte and top 3 bits of fourth byte for temperature
89 raw_temperature = ((response[2] << 3) | (response[3] & 0xe0) >> 5);
90
91 return i2c::ERROR_OK;
92}
93
94inline float convert_temperature(uint16_t raw_temperature) {
95 const float temperature_bits_span = 2048;
96 const float temperature_max = 150;
97 const float temperature_min = -50;
98 const float temperature_span = temperature_max - temperature_min;
99
100 float temperature = (raw_temperature * temperature_span / temperature_bits_span) + temperature_min;
101
102 return temperature;
103}
104
106 uint8_t status(NONE);
107 uint16_t raw_temperature(0);
108 uint16_t raw_pressure(0);
109 i2c::ErrorCode err = this->read_(status, raw_temperature, raw_pressure);
110
111 if (err != i2c::ERROR_OK) {
112 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
113 this->status_set_warning();
114 return;
115 }
116
117 switch (status) {
118 case RESERVED:
119 ESP_LOGE(TAG, "Failed: Device return RESERVED status");
120 this->status_set_warning();
121 return;
122 case FAULT:
123 ESP_LOGE(TAG, "Failed: FAULT condition in the SSC or sensing element");
124 this->mark_failed();
125 return;
126 case STALE:
127 ESP_LOGE(TAG, "Warning: STALE data. Data has not been updated since last fetch");
128 this->status_set_warning();
129 return;
130 }
131
132 float temperature = convert_temperature(raw_temperature);
133
134 ESP_LOGD(TAG, "Got raw pressure=%d, temperature=%.1f°C", raw_pressure, temperature);
135
136 if (this->temperature_sensor_ != nullptr)
137 this->temperature_sensor_->publish_state(temperature);
138 if (this->raw_pressure_sensor_ != nullptr)
139 this->raw_pressure_sensor_->publish_state(raw_pressure);
140
141 this->status_clear_warning();
142}
143
144} // 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:306
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
i2c::ErrorCode read_(uint8_t &status, uint16_t &raw_temperature, uint16_t &raw_pressure)
Definition tem3200.cpp:53
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:12
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
float convert_temperature(uint16_t raw_temperature)
Definition tem3200.cpp:94
void HOT delay(uint32_t ms)
Definition hal.cpp:82
uint16_t temperature
Definition sun_gtil2.cpp:12