10static const char *
const TAG =
"jsn_sr04t.sensor";
14 ESP_LOGV(TAG,
"Request read out from sensor");
22 ESP_LOGV(TAG,
"Read byte from sensor: %x", data);
24 if (this->
buffer_.empty() && data != 0xFF)
36 uint16_t distance =
encode_uint16(this->buffer_[1], this->buffer_[2]);
38 float meters = distance / 1000.0f;
39 ESP_LOGV(TAG,
"Distance from sensor: %umm, %.3fm", distance, meters);
42 ESP_LOGW(TAG,
"Invalid data read from sensor: %s",
format_hex_pretty(this->buffer_).c_str());
45 ESP_LOGW(TAG,
"checksum failed: %02x != %02x",
checksum, this->buffer_[3]);
47 this->buffer_.clear();
51 LOG_SENSOR(
"",
"JST_SR04T Sensor",
this);
54 ESP_LOGCONFIG(TAG,
" sensor model: jsn_sr04t");
57 ESP_LOGCONFIG(TAG,
" sensor model: aj_sr04m");
60 LOG_UPDATE_INTERVAL(
this);
void dump_config() override
std::vector< uint8_t > buffer_
void publish_state(float state)
Publish a new state to the front-end.
bool read_byte(uint8_t *data)
void write_byte(uint8_t data)
Providing packet encoding functions for exchanging data with a remote host.
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.