Created
November 22, 2019 07:32
-
-
Save m-pavel/14a03c967067ee8a8b11fa34184fe3e2 to your computer and use it in GitHub Desktop.
SHT1X for esphome using https://github.com/practicalarduino/SHT1x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
includes: | |
- ./sht1x/sht1x.h | |
- ./sht1x/sht1x.cpp | |
- ./sht1x/SHT1x.h | |
- ./sht1x/SHT1x.cpp | |
sensor: | |
- platform: custom | |
lambda: |- | |
auto my_sensor = new esphome::sht1x::SHT1XSensor(27, 14); | |
App.register_component(my_sensor); | |
return {my_sensor->temperature_sensor, my_sensor->humidity_sensor}; | |
sensors: | |
- name: "SHT1X Temperature Sensor" | |
unit_of_measurement: °C | |
accuracy_decimals: 3 | |
- name: "SHT1X Humidity Sensor" | |
unit_of_measurement: "%" | |
accuracy_decimals: 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "sht1x.h" | |
#include "esphome/core/log.h" | |
namespace esphome { | |
namespace sht1x { | |
#define MIN_TEMP -40 | |
#define MAX_TEMP 125 | |
static const char *TAG = "sht1x.sensor"; | |
void SHT1XSensor::setup() { | |
ESP_LOGCONFIG(TAG, "Setting up SHT1X '%s'...", this->name_.c_str()); | |
ESP_LOGCONFIG(TAG, "Data pin is '%d'...", this->data->get_pin()); | |
ESP_LOGCONFIG(TAG, "Clock pin is '%d'...", this->clock->get_pin()); | |
this->sht1x_ = new SHT1x(this->data->get_pin(), this->clock->get_pin()); | |
ESP_LOGCONFIG(TAG, "Done setting up SHT1X '%s'...", this->name_.c_str()); | |
} | |
void SHT1XSensor::update() { | |
float temp = this->sht1x_->readTemperatureC(); | |
if (temp >= MIN_TEMP && temp <= MAX_TEMP) { | |
this->temperature_sensor->publish_state(temp); | |
} | |
float humidity = this->sht1x_->readHumidity(); | |
if (humidity >= 0 && humidity <= 100) { | |
this->humidity_sensor->publish_state(humidity); | |
} | |
} | |
} // namespace sht1x | |
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "esphome/core/component.h" | |
#include "esphome/components/sensor/sensor.h" | |
#include "SHT1x.h" | |
namespace esphome { | |
namespace sht1x { | |
/// This class implements support for | |
class SHT1XSensor : public sensor::Sensor, public PollingComponent { | |
public: | |
SHT1XSensor() : PollingComponent(15000) { } | |
SHT1XSensor(int data, int clock) : PollingComponent(15000) { | |
this->set_data(new GPIOPin(data, INPUT, false)); | |
this->set_clock(new GPIOPin(clock, INPUT, false)); | |
} | |
Sensor *temperature_sensor = new Sensor(); | |
Sensor *humidity_sensor = new Sensor(); | |
// ========== INTERNAL METHODS ========== | |
// (In most use cases you won't need these) | |
void setup() override; | |
void update() override; | |
void set_data(GPIOPin *pin) { data = pin; } | |
void set_clock(GPIOPin *pin) { clock = pin; } | |
protected: | |
GPIOPin *data; | |
GPIOPin *clock; | |
SHT1x *sht1x_; | |
}; | |
} // namespace sht1x | |
} // namespace esphome |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment