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