ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
gy39.cpp
Go to the documentation of this file.
1#include "gy39.h"
3#include "esphome/core/log.h"
4
5namespace esphome::gy39 {
6
7ESPHOME_LOG_TAG(TAG, "gy39");
8
9// Registers 0x00-0x0D, big-endian: lux x100 (u32), °C x100 (s16), Pa x100 (u32), %RH x100 (u16), metres (s16)
10static constexpr uint8_t REGISTER_DATA = 0x00;
11static constexpr size_t DATA_SIZE = 14;
12
13void GY39::setup() {
14 uint8_t data[DATA_SIZE];
15 this->setup_check(this->read_bytes(REGISTER_DATA, data, sizeof(data)));
16}
17
19 uint8_t data[DATA_SIZE];
20 if (!this->status_comm_result(this->read_bytes(REGISTER_DATA, data, sizeof(data))))
21 return;
22 PUBLISH_STATE(this->illuminance_sensor_, encode_value<uint32_t>(data) / 100.0f);
23 PUBLISH_STATE(this->temperature_sensor_, encode_value<int16_t>(data + 4) / 100.0f);
24 PUBLISH_STATE(this->pressure_sensor_, encode_value<uint32_t>(data + 6) / 100.0f);
25 PUBLISH_STATE(this->humidity_sensor_, encode_value<uint16_t>(data + 10) / 100.0f);
26 PUBLISH_STATE(this->altitude_sensor_, encode_value<int16_t>(data + 12));
27}
28
30 ESP_LOGCONFIG(TAG, "GY-39:");
31 LOG_I2C_DEVICE(this);
32 LOG_UPDATE_INTERVAL(this);
33 LOG_SENSOR(" ", "Illuminance", this->illuminance_sensor_);
34 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
35 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
36 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
37 LOG_SENSOR(" ", "Altitude", this->altitude_sensor_);
38}
39
40} // namespace esphome::gy39
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:235
void update() override
Definition gy39.cpp:18
void dump_config() override
Definition gy39.cpp:29
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
ESPHOME_LOG_TAG(TAG, "gy39")
constexpr T encode_value(const uint8_t *bytes)
Encode a value from its constituent bytes (from most to least significant) in an array with length si...
Definition helpers.h:1220