Начальный каркас: прошивка ESP32 для зоны "Гроубокс"
Реализованы sensor-1 (DHT22, телеметрия temperature/humidity) и
реле-актуаторы light-1/pump-1/fan-1 (turn_on/turn_off/set_level) по
протоколу MQTT платформы home_automatization (devices/{id}/telemetry|commands|ack).
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
#include "telemetry.h"
|
||||
|
||||
#include <DHT.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "mqtt_link.h"
|
||||
#include "time_sync.h"
|
||||
|
||||
static DHT dht(PIN_DHT22, DHT22);
|
||||
static unsigned long lastPublish = 0;
|
||||
|
||||
void telemetryBegin() {
|
||||
dht.begin();
|
||||
}
|
||||
|
||||
void telemetryTick() {
|
||||
unsigned long now = millis();
|
||||
if (now - lastPublish < TELEMETRY_INTERVAL_MS && lastPublish != 0) {
|
||||
return;
|
||||
}
|
||||
// Без синхронизированного времени ingest-service отклонит timestamp
|
||||
// (или он будет ложным), поэтому просто пропускаем цикл и ждём.
|
||||
if (!timeSyncIsReady()) {
|
||||
return;
|
||||
}
|
||||
lastPublish = now;
|
||||
|
||||
float humidity = dht.readHumidity();
|
||||
float temperature = dht.readTemperature();
|
||||
if (isnan(humidity) || isnan(temperature)) {
|
||||
Serial.println("[telemetry] DHT22 read failed, skipping this cycle");
|
||||
return;
|
||||
}
|
||||
|
||||
mqttPublishTelemetry(DEVICE_ID_SENSOR, "temperature", temperature);
|
||||
mqttPublishTelemetry(DEVICE_ID_SENSOR, "humidity", humidity);
|
||||
|
||||
Serial.print("[telemetry] temperature=");
|
||||
Serial.print(temperature);
|
||||
Serial.print(" humidity=");
|
||||
Serial.println(humidity);
|
||||
}
|
||||
Reference in New Issue
Block a user