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