diff --git a/common/veml6075.yaml b/common/veml6075.yaml index d9673eb..f7943b6 100644 --- a/common/veml6075.yaml +++ b/common/veml6075.yaml @@ -4,13 +4,16 @@ sensor: lambda: |- auto veml6075 = new VEML6075CustomSensor(); App.register_component(veml6075); - return {veml6075->uva_sensor, veml6075->uvb_sensor}; + return {veml6075->uva_sensor, veml6075->uvb_sensor, veml6075->uvi_sensor}; sensors: - - name: "zelva UVA" + - name: "${node_name} UVA" id: uva unit_of_measurement: "mW/cm²" accuracy_decimals: 0 - - name: "zelva UVB" + - name: "${node_name} UVB" id: uvb unit_of_measurement: "mW/cm²" accuracy_decimals: 0 + - name: "${node_name} UVI" + id: uvi + accuracy_decimals: 0 diff --git a/custom/veml6075_custom_sensor.h b/custom/veml6075_custom_sensor.h index fa94b1d..6785cb3 100644 --- a/custom/veml6075_custom_sensor.h +++ b/custom/veml6075_custom_sensor.h @@ -1,27 +1,43 @@ #include "esphome.h" +#include #include "Adafruit_VEML6075.h" -class VEML6075CustomSensor : public PollingComponent, public Sensor { +class VEML6075CustomSensor : public PollingComponent { public: Adafruit_VEML6075 uv = Adafruit_VEML6075(); Sensor *uva_sensor = new Sensor(); Sensor *uvb_sensor = new Sensor(); + Sensor *uvi_sensor = new Sensor(); VEML6075CustomSensor() : PollingComponent(15000) {} + + float get_setup_priority() const override { return esphome::setup_priority::HARDWARE; } + void setup() override { //Wire.begin(21, 22); - uv.begin(); + if (!uv.begin()) { + ESP_LOGE("custom", "VEML6075 init failed"); + this->mark_failed(); + return; + } uv.setIntegrationTime(VEML6075_100MS); + //uv.setHighDynamic(true); + + // Set the calibration coefficients + uv.setCoefficients(2.22, 1.33, // UVA_A and UVA_B coefficients + 2.95, 1.74, // UVB_C and UVB_D coefficients + 0.001461, 0.002591); // UVA and UVB responses } void update() override { float uva = uv.readUVA(); float uvb = uv.readUVB(); + float uvi = uv.readUVI(); ESP_LOGD("custom", "The value of sensor uva is: %.0f", uva); ESP_LOGD("custom", "The value of sensor uvb is: %.0f", uvb); - publish_state(uva); uva_sensor->publish_state(uva); uvb_sensor->publish_state(uvb); + uvi_sensor->publish_state(uvi); } };