diff --git a/esp32h2_water_temp_pump/esp32h2_water_temp_pump.ino b/esp32h2_water_temp_pump/esp32h2_water_temp_pump.ino index e8037eb..fd20517 100644 --- a/esp32h2_water_temp_pump/esp32h2_water_temp_pump.ino +++ b/esp32h2_water_temp_pump/esp32h2_water_temp_pump.ino @@ -32,9 +32,13 @@ #include "Zigbee.h" #include +#include #define TEMP_PIN 10 #define PUMP_PIN 13 +#define LED_PIN 8 + +CRGB leds[1]; /* Zigbee temperature sensor configuration */ #define ANALOG_DEVICE_ENDPOINT_NUMBER 1 @@ -60,6 +64,11 @@ static float get_temp(int gpio) { uint8_t devices = ds.search(addr, MaxDevices); + Serial.printf("Number of devices: %d\r\n", devices); + + ds.request(); + vTaskDelay(800 / portTICK_PERIOD_MS); + for (int i = 0; i < devices; i++) { uint8_t err = ds.getTemp(addr[i], temp[i]); if (err) { @@ -67,12 +76,13 @@ static float get_temp(int gpio) { Serial.print(i); Serial.print(": "); Serial.println(errt[err]); + return err; } else { avg += temp[i]; num += 1; } } - if (num == 0) return -10.0; + if (num == 0) return -100.0; return avg / num; } @@ -81,11 +91,13 @@ static float get_temp(int gpio) { 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); + // float tsens_value = temperatureRead(); + float tsens_value = get_temp(TEMP_PIN); + // Update temperature value in Temperature sensor EP - zbTempSensor.setTemperature(tsens_value); + if(tsens_value > -50.0 && tsens_value < 150.0) { + 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 @@ -102,11 +114,17 @@ static void temp_sensor_value_update(void *arg) { /********************* Arduino functions **************************/ void setup() { - Serial.begin(115200); + FastLED.addLeds(leds, 1); + leds[0] = CRGB::Red; FastLED.show(); // 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); + leds[0] = CRGB::Orange; FastLED.show(); // actual temp given by the thermometer zbTempSensor.setManufacturerAndModel("ESP32H2", "Furnace Pump"); @@ -125,6 +143,7 @@ void setup() { Zigbee.addEndpoint(&zbTempSensor); Zigbee.addEndpoint(&zbAnalogTemp); + leds[0] = CRGB::Yellow; FastLED.show(); Serial.println("Starting Zigbee..."); // When all EPs are registered, start Zigbee in End Device mode @@ -135,6 +154,9 @@ void setup() { } else { Serial.println("Zigbee started successfully!"); } + + leds[0] = CRGB::Green; FastLED.show(); + Serial.println("Connecting to network"); while (!Zigbee.connected()) { Serial.print("."); @@ -142,6 +164,8 @@ void setup() { } Serial.println(); + leds[0] = CRGB::Blue; FastLED.show(); + // Optional: If time cluster is added, time can be read from the coordinator timeinfo = zbTempSensor.getTime(); timezone = zbTempSensor.getTimezone(); @@ -157,6 +181,7 @@ void setup() { // Start Temperature sensor reading task xTaskCreate(temp_sensor_value_update, "temp_sensor_update", 3072, NULL, 10, NULL); + leds[0] = CRGB::White; FastLED.show(); // 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) @@ -165,6 +190,9 @@ void setup() { // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of temperature change zbTempSensor.setReporting(10, 300, 1); zbAnalogTemp.setAnalogOutput(60.0); // starting default value + + leds[0] = CRGB::Black; FastLED.show(); + } void loop() {