// Copyright 2024 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * @brief This example demonstrates Zigbee temperature sensor. * * The example demonstrates how to use Zigbee library to create a end device temperature sensor. * The temperature sensor is a Zigbee end device, which is controlled by a Zigbee coordinator. * * Proper Zigbee mode must be selected in Tools->Zigbee mode * and also the correct partition scheme must be selected in Tools->Partition Scheme. * * Please check the README.md for instructions and more detailed description. * * Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/) */ #ifndef ZIGBEE_MODE_ED #error "Zigbee end device mode is not selected in Tools->Zigbee mode" #endif #include "Zigbee.h" #include #include #define TEMP_PIN 10 #define PUMP_PIN 13 #define LED_PIN 8 #define NUM_LEDS 1 /* Zigbee temperature sensor configuration */ #define ANALOG_DEVICE_ENDPOINT_NUMBER 1 #define TEMP_SENSOR_ENDPOINT_NUMBER 10 uint8_t button = BOOT_PIN; // Optional Time cluster variables struct tm timeinfo; struct tm *localTime; int32_t timezone; int8_t counter = 0; ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER); ZigbeeAnalog zbSetTemp = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER); ZigbeeAnalog zbSetTempDelta = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER); Adafruit_NeoPixel rgb(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800); uint32_t red = rgb.Color(255,0,0); uint32_t orange = rgb.Color(255, 127, 0); uint32_t yellow = rgb.Color(255, 255, 0); uint32_t green = rgb.Color(0, 255, 0); uint32_t blue = rgb.Color(0, 0, 255); uint32_t white = rgb.Color(255, 255, 255); /************************ 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); Serial.printf("Number of devices: %d\r\n", devices); ds.request(); vTaskDelay(1500 / portTICK_PERIOD_MS); 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]); return err + 200; } else { avg += temp[i]; num += 1; } } if (num == 0) return -100.0; return avg / num; } /************************ Temp sensor ZigBee *****************************/ static void temp_sensor_value_update(void *arg) { float temp_tmp, temp_set = 55, temp_delta; for (;;) { // Read temperature sensor value // float tsens_value = temperatureRead(); float tsens_value = get_temp(TEMP_PIN); // Update temperature value in Temperature sensor EP if(tsens_value < -20.0 || tsens_value > 150.0) { delay(100); continue; } zbTempSensor.setTemperature(tsens_value); temp_tmp = zbSetTemp.getAnalogOutput(); if(temp_tmp < 10 || temp_tmp > 70) { temp_set = 55; } else { temp_set = temp_tmp; } // check temp and control pump temp_delta = zbSetTempDelta.getAnalogOutput(); if (tsens_value >= temp_set + temp_delta) { counter++; if(counter > 3) counter = 3; } else if (tsens_value <= temp_set - temp_delta) { counter--; if(counter < 0) counter = 0; } if(counter != 0) { Serial.printf("#%d PUMP ON %.2f°C\r\n", counter, tsens_value); digitalWrite(PUMP_PIN, HIGH); } else { Serial.printf("#%d PUMP OFF %.2f°C\r\n", counter, tsens_value); digitalWrite(PUMP_PIN, LOW); } delay(2000); } } /********************* Arduino functions **************************/ void setup() { // Init button switch pinMode(button, INPUT_PULLUP); pinMode(PUMP_PIN, OUTPUT); digitalWrite(PUMP_PIN, HIGH); // default to high just in case Serial.begin(115200); // while(!Serial); // TODO comment out Serial.println("Starting LED..."); rgb.begin(); rgb.setBrightness(30); rgb.setPixelColor(0,red); rgb.show(); Serial.println("Starting temp task..."); // Start Temperature sensor reading task xTaskCreate(temp_sensor_value_update, "temp_sensor_update", 3072, NULL, 10, NULL); Serial.println("Setting up ZigBee endpoints..."); // 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 zbSetTemp.addAnalogOutput(); zbSetTemp.setAnalogOutputApplication(ESP_ZB_ZCL_AI_TEMPERATURE_OTHER); zbSetTemp.setAnalogOutputDescription("Water Temperature (°C)"); zbSetTemp.setAnalogOutputResolution(1); zbSetTemp.setAnalogOutputMinMax(10, 70); // the delta value plus/minus the desired temperature zbSetTempDelta.addAnalogOutput(); zbSetTempDelta.setAnalogOutputApplication(ESP_ZB_ZCL_AI_TEMPERATURE_OTHER); zbSetTempDelta.setAnalogOutputDescription("Temperature delta (°C)"); zbSetTempDelta.setAnalogOutputResolution(0.5); zbSetTempDelta.setAnalogOutputMinMax(1, 10); Serial.println("Adding ZigBee endpoints..."); // Add endpoint to Zigbee Core Zigbee.addEndpoint(&zbTempSensor); Zigbee.addEndpoint(&zbSetTemp); Zigbee.addEndpoint(&zbSetTempDelta); // ZigBee endpoints setup rgb.setPixelColor(0,orange); rgb.show(); Serial.println("Starting Zigbee..."); // When all EPs are registered, start Zigbee in End Device mode if (!Zigbee.begin()) { Serial.println("Zigbee failed to start!"); Serial.println("Rebooting..."); ESP.restart(); } else { Serial.println("Zigbee started successfully!"); } // ZigBee started successfully rgb.setPixelColor(0,yellow); rgb.show(); Serial.println("Connecting to network..."); while (!Zigbee.connected()) { Serial.print("."); delay(100); } Serial.println(); // ZigBee connected rgb.setPixelColor(0,green); rgb.show(); Serial.println("Getting time..."); // Optional: If time cluster is added, time can be read from the coordinator timeinfo = zbTempSensor.getTime(); timezone = zbTempSensor.getTimezone(); Serial.println("UTC time:"); Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); time_t local = mktime(&timeinfo) + timezone; localTime = localtime(&local); Serial.println("Local time with timezone:"); Serial.println(localTime, "%A, %B %d %Y %H:%M:%S"); // localtime? rgb.setPixelColor(0,blue); rgb.show(); Serial.println("Setting default ZigBee values and intervals..."); // 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 // min 10s, max 300s (5min), delta 1 (1 degree celcius) zbTempSensor.setReporting(10, 300, 1); // set default values zbSetTemp.setAnalogOutput(55.0); // starting default value zbSetTempDelta.setAnalogOutput(5.0); // starting default value // everything done. rgb.setBrightness(0); rgb.show(); } void loop() { // Checking button for factory reset if (digitalRead(button) == LOW) { // Push button pressed // Key debounce handling delay(100); int startTime = millis(); while (digitalRead(button) == LOW) { delay(50); if ((millis() - startTime) > 3000) { // If key pressed for more than 3secs, factory reset Zigbee and reboot Serial.println("Resetting Zigbee to factory and rebooting in 1s."); delay(1000); Zigbee.factoryReset(); } } zbTempSensor.reportTemperature(); } delay(100); }