8static const char *
const TAG =
"ultrasonic.sensor";
11 ESP_LOGCONFIG(TAG,
"Setting up Ultrasonic Sensor...");
23 const uint32_t start =
micros();
28 const uint32_t pulse_start =
micros();
31 const uint32_t pulse_end =
micros();
33 ESP_LOGV(TAG,
"Echo took %" PRIu32
"µs", pulse_end - pulse_start);
36 ESP_LOGD(TAG,
"'%s' - Distance measurement timed out!", this->
name_.
c_str());
40 ESP_LOGD(TAG,
"'%s' - Got distance: %.3f m", this->
name_.
c_str(), result);
45 LOG_SENSOR(
"",
"Ultrasonic Sensor",
this);
48 ESP_LOGCONFIG(TAG,
" Pulse time: %" PRIu32
" µs", this->
pulse_time_us_);
49 ESP_LOGCONFIG(TAG,
" Timeout: %" PRIu32
" µs", this->
timeout_us_);
50 LOG_UPDATE_INTERVAL(
this);
53 const float speed_sound_m_per_s = 343.0f;
54 const float time_s = us / 1e6f;
55 const float total_dist = time_s * speed_sound_m_per_s;
56 return total_dist / 2.0f;
virtual void digital_write(bool value)=0
virtual ISRInternalGPIOPin to_isr() const =0
constexpr const char * c_str() const
void publish_state(float state)
Publish a new state to the front-end.
float get_setup_priority() const override
void dump_config() override
uint32_t pulse_time_us_
2 meters.
InternalGPIOPin * echo_pin_
ISRInternalGPIOPin echo_isr_
static float us_to_m(uint32_t us)
Helper function to convert the specified echo duration in µs to meters.
void set_timeout_us(uint32_t timeout_us)
Set the timeout for waiting for the echo in µs.
GPIOPin * trigger_pin_
Helper function to convert the specified distance in meters to the echo duration in µs.
void set_pulse_time_us(uint32_t pulse_time_us)
Set the time in µs the trigger pin should be enabled for in µs, defaults to 10µs (for HC-SR04)
void setup() override
Set up pins and register interval.
const float DATA
For components that import data from directly connected sensors like DHT.
Providing packet encoding functions for exchanging data with a remote host.
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
uint32_t IRAM_ATTR HOT micros()