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