Arduino_Sensor_Nodes/sketch_dec6a/sketch_dec6a.ino

134 lines
4.3 KiB
C++

/*
* ESP32-C6 Zigbee Router with DHT11 Sensor
*
* Logic:
* 1. Initialize as a Zigbee Router (ZR).
* 2. Create an endpoint with Temperature and Humidity clusters.
* 3. Read DHT11 sensor periodically.
* 4. Update and report values to the Zigbee network.
*
* Libraries required:
* - EspZigbee (Standard with ESP32 Board package v3.0.0+)
* - DHT sensor library by Adafruit (Install via Library Manager)
*/
#include "Zigbee.h"
#include "DHT.h"
// --- Hardware Config ---
#define DHTPIN 17 // Pin connected to DHT11 Data
#define DHTTYPE DHT11 // Sensor type
#define DHTPOWER 4 // GPIO pin to power the sensor
// --- Zigbee Config ---
// Identify the endpoint (arbitrary number, 1-240)
#define SENSOR_ENDPOINT_NUM 10
// --- Global Objects ---
DHT dht(DHTPIN, DHTTYPE);
// Define the Zigbee Temperature Sensor object
// Note: Depending on the specific library version, this helper class
// might primarily handle Temperature. We will configure it to report.
ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(SENSOR_ENDPOINT_NUM);
// Timer for reporting
unsigned long lastReportTime = 0;
const unsigned long REPORT_INTERVAL = 10000; // Report every 10 seconds
void setup() {
Serial.begin(115200);
pinMode(DHTPOWER, OUTPUT);
digitalWrite(DHTPOWER, HIGH); // Power on the sensor
// Initialize DHT Sensor
dht.begin();
Serial.println("DHT11 Sensor Initialized");
// --- Zigbee Setup ---
// 1. Set Manufacturer and Model details (visible in Home Assistant/Zigbee2MQTT)
zbTempSensor.setManufacturerAndModel("Espressif", "ESP32C6-Router-Temp");
// 2. Add the Temperature and Humidity clusters to the endpoint
// The default ZigbeeTempSensor includes the Temperature Measurement cluster.
// We explicitly try to set the sensor config.
// Note: If you need a dedicated Humidity cluster separate from this helper,
// you might need to use the generic EspZigbee::ZigbeeEP helper, but
// most modern helpers support adding standard clusters.
// Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement)
zbTempSensor.setMinMaxValue(10, 50);
// Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C)
zbTempSensor.setTolerance(0.1);
// Add humidity cluster to the temperature sensor device with min, max and tolerance values
zbTempSensor.addHumiditySensor(0, 100, 1);
// 3. Add Endpoint to Zigbee Core
Zigbee.addEndpoint(&zbTempSensor);
// 4. Initialize Zigbee as a ROUTER
// ZIGBEE_ROUTER: Mains-powered, always on, routes packets for other devices.
// Serial.println("Starting Zigbee as Router...");
// if (!Zigbee.begin(ZIGBEE_ROUTER)) {
// Serial.println("Zigbee failed to start! Restarting...");
// delay(1000);
// ESP.restart();
// }
// Serial.println("Zigbee Started. Waiting for network...");
// Zigbee.factoryReset();
Serial.println("Starting Zigbee...");
// When all EPs are registered, start Zigbee in Router mode
if (!Zigbee.begin(ZIGBEE_ROUTER)) {
Serial.println("Zigbee failed to start!");
Serial.println("Rebooting...");
ESP.restart();
} else {
Serial.println("Zigbee started successfully!");
}
Serial.println("Connecting to network");
while (!Zigbee.connected()) {
Serial.print(".");
delay(100);
}
Serial.println("Connected");
}
void loop() {
// Check if enough time has passed to report data
if (millis() - lastReportTime > REPORT_INTERVAL) {
lastReportTime = millis();
// 1. Read Sensor
float temp = dht.readTemperature();
float hum = dht.readHumidity();
// Check if read failed
if (isnan(temp) || isnan(hum)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.printf("Temp: %.1f C, Humidity: %.1f %%\n", temp, hum);
// 2. Update Zigbee Clusters
// The ZigbeeTempSensor class updates the local attribute
zbTempSensor.setTemperature(temp);
// Note: If your specific version of ZigbeeTempSensor does not support setHumidity,
// you may need to add a separate Humidity endpoint or use raw attribute setting.
// However, recent ESP32-Arduino versions often bundle these.
zbTempSensor.setHumidity(hum); // Uncomment if supported by your version
// 3. Report Values
// This triggers a report attribute command to the coordinator
zbTempSensor.report();
}
}