Browse Source

fix veml6075 code

esphome
Hendrik Langer 3 years ago
parent
commit
fbd0523463
  1. 9
      common/veml6075.yaml
  2. 22
      custom/veml6075_custom_sensor.h

9
common/veml6075.yaml

@ -4,13 +4,16 @@ sensor:
lambda: |- lambda: |-
auto veml6075 = new VEML6075CustomSensor(); auto veml6075 = new VEML6075CustomSensor();
App.register_component(veml6075); App.register_component(veml6075);
return {veml6075->uva_sensor, veml6075->uvb_sensor}; return {veml6075->uva_sensor, veml6075->uvb_sensor, veml6075->uvi_sensor};
sensors: sensors:
- name: "zelva UVA" - name: "${node_name} UVA"
id: uva id: uva
unit_of_measurement: "mW/cm²" unit_of_measurement: "mW/cm²"
accuracy_decimals: 0 accuracy_decimals: 0
- name: "zelva UVB" - name: "${node_name} UVB"
id: uvb id: uvb
unit_of_measurement: "mW/cm²" unit_of_measurement: "mW/cm²"
accuracy_decimals: 0 accuracy_decimals: 0
- name: "${node_name} UVI"
id: uvi
accuracy_decimals: 0

22
custom/veml6075_custom_sensor.h

@ -1,27 +1,43 @@
#include "esphome.h" #include "esphome.h"
#include <Wire.h>
#include "Adafruit_VEML6075.h" #include "Adafruit_VEML6075.h"
class VEML6075CustomSensor : public PollingComponent, public Sensor { class VEML6075CustomSensor : public PollingComponent {
public: public:
Adafruit_VEML6075 uv = Adafruit_VEML6075(); Adafruit_VEML6075 uv = Adafruit_VEML6075();
Sensor *uva_sensor = new Sensor(); Sensor *uva_sensor = new Sensor();
Sensor *uvb_sensor = new Sensor(); Sensor *uvb_sensor = new Sensor();
Sensor *uvi_sensor = new Sensor();
VEML6075CustomSensor() : PollingComponent(15000) {} VEML6075CustomSensor() : PollingComponent(15000) {}
float get_setup_priority() const override { return esphome::setup_priority::HARDWARE; }
void setup() override { void setup() override {
//Wire.begin(21, 22); //Wire.begin(21, 22);
uv.begin(); if (!uv.begin()) {
ESP_LOGE("custom", "VEML6075 init failed");
this->mark_failed();
return;
}
uv.setIntegrationTime(VEML6075_100MS); 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 { void update() override {
float uva = uv.readUVA(); float uva = uv.readUVA();
float uvb = uv.readUVB(); 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 uva is: %.0f", uva);
ESP_LOGD("custom", "The value of sensor uvb is: %.0f", uvb); ESP_LOGD("custom", "The value of sensor uvb is: %.0f", uvb);
publish_state(uva);
uva_sensor->publish_state(uva); uva_sensor->publish_state(uva);
uvb_sensor->publish_state(uvb); uvb_sensor->publish_state(uvb);
uvi_sensor->publish_state(uvi);
} }
}; };

Loading…
Cancel
Save