#include "esphome.h" class AJ_SR04M_Sensor : public PollingComponent, public UARTDevice, public Sensor { public: AJ_SR04M_Sensor(UARTComponent *parent) : PollingComponent(5000), UARTDevice(parent) {} void update() override { char frame[5]; int pos = 0; float value = 0.0; write(0x00); while (available()) { frame[pos] = read(); pos++; if(pos==5) { if ((frame[0] == 0xFF) && (frame[4] == 0x00) && ((frame[1]+frame[2])==frame[3])) { value = ((frame[1]<<8) + frame[2]) / 1000.0; publish_state(value); } break; } } } };