8 static 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
const float DATA
For components that import data from directly connected sensors like DHT.
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) ...
uint32_t pulse_time_us_
2 meters.
GPIOPin * trigger_pin_
Helper function to convert the specified distance in meters to the echo duration in µs...
void dump_config() override
void set_timeout_us(uint32_t timeout_us)
Set the timeout for waiting for the echo in µs.
uint32_t IRAM_ATTR HOT micros()
InternalGPIOPin * echo_pin_
static float us_to_m(uint32_t us)
Helper function to convert the specified echo duration in µs to meters.
void setup() override
Set up pins and register interval.
void publish_state(float state)
Publish a new state to the front-end.
ISRInternalGPIOPin echo_isr_
constexpr const char * c_str() const
virtual ISRInternalGPIOPin to_isr() const =0
Implementation of SPI Controller mode.
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
float get_setup_priority() const override