ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
npi19.cpp
Go to the documentation of this file.
1#include "npi19.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace npi19 {
8
9static const char *const TAG = "npi19";
10
11static const uint8_t READ_COMMAND = 0xAC;
12
14 uint16_t raw_temperature(0);
15 uint16_t raw_pressure(0);
16 i2c::ErrorCode err = this->read_(raw_temperature, raw_pressure);
17 if (err != i2c::ERROR_OK) {
18 ESP_LOGCONFIG(TAG, ESP_LOG_MSG_COMM_FAIL);
19 this->mark_failed();
20 return;
21 }
22}
23
25 ESP_LOGCONFIG(TAG, "NPI19:");
26 LOG_I2C_DEVICE(this);
27 LOG_UPDATE_INTERVAL(this);
28 LOG_SENSOR(" ", "Raw Pressure", this->raw_pressure_sensor_);
29 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
30}
31
33
34i2c::ErrorCode NPI19Component::read_(uint16_t &raw_temperature, uint16_t &raw_pressure) {
35 // initiate data read from device
36 i2c::ErrorCode w_err = write(&READ_COMMAND, sizeof(READ_COMMAND), true);
37 if (w_err != i2c::ERROR_OK) {
38 return w_err;
39 }
40
41 // read 4 bytes from senesor
42 uint8_t response[4] = {0x00, 0x00, 0x00, 0x00};
43 i2c::ErrorCode r_err = this->read(response, 4);
44
45 if (r_err != i2c::ERROR_OK) {
46 return r_err;
47 }
48
49 // extract top 6 bits of first byte and all bits of second byte for pressure
50 raw_pressure = ((response[0] & 0x3F) << 8) | response[1];
51
52 // extract all bytes of 3rd byte and top 3 bits of fourth byte for temperature
53 raw_temperature = (response[2] << 3) | ((response[3] & 0xE0) >> 5);
54
55 return i2c::ERROR_OK;
56}
57
58inline float convert_temperature(uint16_t raw_temperature) {
59 /*
60 * Correspondance with Amphenol confirmed the appropriate equation for computing temperature is:
61 * T (°C) =(((((Th*8)+Tl)/2048)*200)-50), where Th is the high (third) byte and Tl is the low (fourth) byte.
62 *
63 * Tl is actually the upper 3 bits of the fourth data byte; the first 5 (LSBs) must be masked out.
64 *
65 *
66 * The NPI-19 I2C has a temperature output, however the manufacturer does
67 * not specify its accuracy on the published datasheet. They indicate
68 * that the sensor should not be used as a calibrated temperature
69 * reading; it’s only intended for curve fitting data during
70 * compensation.
71 */
72 const float temperature_bits_span = 2048;
73 const float temperature_max = 150;
74 const float temperature_min = -50;
75 const float temperature_span = temperature_max - temperature_min;
76
77 float temperature = (raw_temperature * temperature_span / temperature_bits_span) + temperature_min;
78
79 return temperature;
80}
81
83 uint16_t raw_temperature(0);
84 uint16_t raw_pressure(0);
85
86 i2c::ErrorCode err = this->read_(raw_temperature, raw_pressure);
87
88 if (err != i2c::ERROR_OK) {
89 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
90 this->status_set_warning();
91 return;
92 }
93
94 float temperature = convert_temperature(raw_temperature);
95
96 ESP_LOGD(TAG, "Got raw pressure=%d, temperature=%.1f°C", raw_pressure, temperature);
97
98 if (this->temperature_sensor_ != nullptr)
99 this->temperature_sensor_->publish_state(temperature);
100 if (this->raw_pressure_sensor_ != nullptr)
101 this->raw_pressure_sensor_->publish_state(raw_pressure);
102
103 this->status_clear_warning();
104}
105
106} // namespace npi19
107} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
i2c::ErrorCode read_(uint16_t &raw_temperature, uint16_t &raw_pressure)
Definition npi19.cpp:34
sensor::Sensor * temperature_sensor_
Definition npi19.h:25
sensor::Sensor * raw_pressure_sensor_
Definition npi19.h:26
float get_setup_priority() const override
Definition npi19.cpp:32
void dump_config() override
Definition npi19.cpp:24
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
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
float convert_temperature(uint16_t raw_temperature)
Definition npi19.cpp:58
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t temperature
Definition sun_gtil2.cpp:12