Реализованы 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).
43 lines
1.1 KiB
C++
43 lines
1.1 KiB
C++
#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);
|
|
}
|