diff --git a/esp32h2_water_temp_pump/esp32h2_water_temp_pump.ino b/esp32h2_water_temp_pump/esp32h2_water_temp_pump.ino index ad007ab..e8037eb 100644 --- a/esp32h2_water_temp_pump/esp32h2_water_temp_pump.ino +++ b/esp32h2_water_temp_pump/esp32h2_water_temp_pump.ino @@ -31,8 +31,13 @@ #endif #include "Zigbee.h" +#include + +#define TEMP_PIN 10 +#define PUMP_PIN 13 /* Zigbee temperature sensor configuration */ +#define ANALOG_DEVICE_ENDPOINT_NUMBER 1 #define TEMP_SENSOR_ENDPOINT_NUMBER 10 uint8_t button = BOOT_PIN; @@ -42,15 +47,55 @@ struct tm *localTime; int32_t timezone; ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER); +ZigbeeAnalog zbAnalogTemp = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER); /************************ Temp sensor *****************************/ +const uint8_t MaxDevices = 1; +static float get_temp(int gpio) { + OneWire32 ds(gpio); + uint64_t addr[MaxDevices]; + float temp[MaxDevices]; + float avg = 0; + float num = 0; + + uint8_t devices = ds.search(addr, MaxDevices); + + for (int i = 0; i < devices; i++) { + uint8_t err = ds.getTemp(addr[i], temp[i]); + if (err) { + const char *errt[] = { "", "CRC", "BAD", "DC", "DRV" }; + Serial.print(i); + Serial.print(": "); + Serial.println(errt[err]); + } else { + avg += temp[i]; + num += 1; + } + } + if (num == 0) return -10.0; + return avg / num; +} + + +/************************ Temp sensor ZigBee *****************************/ static void temp_sensor_value_update(void *arg) { for (;;) { // Read temperature sensor value float tsens_value = temperatureRead(); + // float tsens_value = get_temp(TEMP_PIN); Serial.printf("Updated temperature sensor value to %.2f°C\r\n", tsens_value); // Update temperature value in Temperature sensor EP zbTempSensor.setTemperature(tsens_value); + + // check temp and control pump + if (tsens_value >= zbAnalogTemp.getAnalogOutput() + 5.0 || tsens_value < 0) { // < 0 for safety if value is uninitialized + Serial.printf("PUMP ON %.2f°C\r\n", tsens_value); + digitalWrite(PUMP_PIN, HIGH); + } else if (tsens_value <= zbAnalogTemp.getAnalogOutput() - 5.0) { + Serial.printf("PUMP OFF %.2f°C\r\n", tsens_value); + digitalWrite(PUMP_PIN, LOW); + } + delay(1000); } } @@ -61,21 +106,25 @@ void setup() { // Init button switch pinMode(button, INPUT_PULLUP); + pinMode(PUMP_PIN, OUTPUT); - // Optional: set Zigbee device name and model - zbTempSensor.setManufacturerAndModel("Espressif", "ZigbeeTempSensor"); - - // Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement) - zbTempSensor.setMinMaxValue(10, 50); - - // Optional: Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C) - zbTempSensor.setTolerance(1); - - // Optional: Time cluster configuration (default params, as this device will revieve time from coordinator) + // actual temp given by the thermometer + zbTempSensor.setManufacturerAndModel("ESP32H2", "Furnace Pump"); + zbTempSensor.setMinMaxValue(-10, 125); + zbTempSensor.setTolerance(0.1); zbTempSensor.addTimeCluster(); + // temperature set as the threshold for pump activation/deactivation + zbAnalogTemp.addAnalogOutput(); + zbAnalogTemp.setAnalogOutputApplication(ESP_ZB_ZCL_AI_TEMPERATURE_OTHER); + zbAnalogTemp.setAnalogOutputDescription("Water Temperature (°C)"); + zbAnalogTemp.setAnalogOutputResolution(1); + zbAnalogTemp.setAnalogOutputMinMax(20, 80); + // Add endpoint to Zigbee Core Zigbee.addEndpoint(&zbTempSensor); + Zigbee.addEndpoint(&zbAnalogTemp); + Serial.println("Starting Zigbee..."); // When all EPs are registered, start Zigbee in End Device mode @@ -107,14 +156,15 @@ void setup() { Serial.println(localTime, "%A, %B %d %Y %H:%M:%S"); // Start Temperature sensor reading task - xTaskCreate(temp_sensor_value_update, "temp_sensor_update", 2048, NULL, 10, NULL); + xTaskCreate(temp_sensor_value_update, "temp_sensor_update", 3072, NULL, 10, NULL); // Set reporting interval for temperature measurement in seconds, must be called after Zigbee.begin() // min_interval and max_interval in seconds, delta (temp change in 0,1 °C) // if min = 1 and max = 0, reporting is sent only when temperature changes by delta // if min = 0 and max = 10, reporting is sent every 10 seconds or temperature changes by delta // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of temperature change - zbTempSensor.setReporting(1, 0, 1); + zbTempSensor.setReporting(10, 300, 1); + zbAnalogTemp.setAnalogOutput(60.0); // starting default value } void loop() {