zmiana biblioteki LEDów

This commit is contained in:
Sasza Stanczew 2026-09-07 10:32:00 +02:00
parent 6177d8e4e9
commit f88dbe3fb8

View file

@ -32,13 +32,12 @@
#include "Zigbee.h" #include "Zigbee.h"
#include <OneWireESP32.h> #include <OneWireESP32.h>
#include <FastLED.h> #include <Adafruit_NeoPixel.h>
#define TEMP_PIN 10 #define TEMP_PIN 10
#define PUMP_PIN 13 #define PUMP_PIN 13
#define LED_PIN 8 #define LED_PIN 8
#define NUM_LEDS 1
CRGB leds[1];
/* Zigbee temperature sensor configuration */ /* Zigbee temperature sensor configuration */
#define ANALOG_DEVICE_ENDPOINT_NUMBER 1 #define ANALOG_DEVICE_ENDPOINT_NUMBER 1
@ -50,9 +49,19 @@ struct tm timeinfo;
struct tm *localTime; struct tm *localTime;
int32_t timezone; int32_t timezone;
ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER); int8_t counter = 0;
ZigbeeAnalog zbAnalogTemp = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER);
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 *****************************/ /************************ Temp sensor *****************************/
const uint8_t MaxDevices = 1; const uint8_t MaxDevices = 1;
static float get_temp(int gpio) { static float get_temp(int gpio) {
@ -67,7 +76,7 @@ static float get_temp(int gpio) {
Serial.printf("Number of devices: %d\r\n", devices); Serial.printf("Number of devices: %d\r\n", devices);
ds.request(); ds.request();
vTaskDelay(800 / portTICK_PERIOD_MS); vTaskDelay(1500 / portTICK_PERIOD_MS);
for (int i = 0; i < devices; i++) { for (int i = 0; i < devices; i++) {
uint8_t err = ds.getTemp(addr[i], temp[i]); uint8_t err = ds.getTemp(addr[i], temp[i]);
@ -76,7 +85,7 @@ static float get_temp(int gpio) {
Serial.print(i); Serial.print(i);
Serial.print(": "); Serial.print(": ");
Serial.println(errt[err]); Serial.println(errt[err]);
return err; return err + 200;
} else { } else {
avg += temp[i]; avg += temp[i];
num += 1; num += 1;
@ -89,43 +98,69 @@ static float get_temp(int gpio) {
/************************ Temp sensor ZigBee *****************************/ /************************ Temp sensor ZigBee *****************************/
static void temp_sensor_value_update(void *arg) { static void temp_sensor_value_update(void *arg) {
float temp_tmp, temp_set = 55, temp_delta;
for (;;) { for (;;) {
// Read temperature sensor value // Read temperature sensor value
// float tsens_value = temperatureRead(); // float tsens_value = temperatureRead();
float tsens_value = get_temp(TEMP_PIN); float tsens_value = get_temp(TEMP_PIN);
// Update temperature value in Temperature sensor EP // Update temperature value in Temperature sensor EP
if(tsens_value > -50.0 && tsens_value < 150.0) { if(tsens_value < -20.0 || tsens_value > 150.0) {
delay(100);
continue;
}
zbTempSensor.setTemperature(tsens_value); 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 // check temp and control pump
if (tsens_value >= zbAnalogTemp.getAnalogOutput() + 5.0 || tsens_value < 0) { // < 0 for safety if value is uninitialized temp_delta = zbSetTempDelta.getAnalogOutput();
Serial.printf("PUMP ON %.2f°C\r\n", tsens_value); 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); digitalWrite(PUMP_PIN, HIGH);
} else if (tsens_value <= zbAnalogTemp.getAnalogOutput() - 5.0) { } else {
Serial.printf("PUMP OFF %.2f°C\r\n", tsens_value); Serial.printf("#%d PUMP OFF %.2f°C\r\n", counter, tsens_value);
digitalWrite(PUMP_PIN, LOW); digitalWrite(PUMP_PIN, LOW);
} }
delay(1000); delay(2000);
} }
} }
/********************* Arduino functions **************************/ /********************* Arduino functions **************************/
void setup() { void setup() {
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, 1);
leds[0] = CRGB::Red; FastLED.show();
// Init button switch // Init button switch
pinMode(button, INPUT_PULLUP); pinMode(button, INPUT_PULLUP);
pinMode(PUMP_PIN, OUTPUT); pinMode(PUMP_PIN, OUTPUT);
digitalWrite(PUMP_PIN, HIGH); // default to high just in case digitalWrite(PUMP_PIN, HIGH); // default to high just in case
Serial.begin(115200); Serial.begin(115200);
// while(!Serial); // while(!Serial); // TODO comment out
leds[0] = CRGB::Orange; FastLED.show();
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 // actual temp given by the thermometer
zbTempSensor.setManufacturerAndModel("ESP32H2", "Furnace Pump"); zbTempSensor.setManufacturerAndModel("ESP32H2", "Furnace Pump");
zbTempSensor.setMinMaxValue(-10, 125); zbTempSensor.setMinMaxValue(-10, 125);
@ -133,17 +168,27 @@ void setup() {
zbTempSensor.addTimeCluster(); zbTempSensor.addTimeCluster();
// temperature set as the threshold for pump activation/deactivation // temperature set as the threshold for pump activation/deactivation
zbAnalogTemp.addAnalogOutput(); zbSetTemp.addAnalogOutput();
zbAnalogTemp.setAnalogOutputApplication(ESP_ZB_ZCL_AI_TEMPERATURE_OTHER); zbSetTemp.setAnalogOutputApplication(ESP_ZB_ZCL_AI_TEMPERATURE_OTHER);
zbAnalogTemp.setAnalogOutputDescription("Water Temperature (°C)"); zbSetTemp.setAnalogOutputDescription("Water Temperature (°C)");
zbAnalogTemp.setAnalogOutputResolution(1); zbSetTemp.setAnalogOutputResolution(1);
zbAnalogTemp.setAnalogOutputMinMax(20, 80); 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 // Add endpoint to Zigbee Core
Zigbee.addEndpoint(&zbTempSensor); Zigbee.addEndpoint(&zbTempSensor);
Zigbee.addEndpoint(&zbAnalogTemp); Zigbee.addEndpoint(&zbSetTemp);
Zigbee.addEndpoint(&zbSetTempDelta);
leds[0] = CRGB::Yellow; FastLED.show(); // ZigBee endpoints setup
rgb.setPixelColor(0,orange); rgb.show();
Serial.println("Starting Zigbee..."); Serial.println("Starting Zigbee...");
// When all EPs are registered, start Zigbee in End Device mode // When all EPs are registered, start Zigbee in End Device mode
@ -155,17 +200,20 @@ void setup() {
Serial.println("Zigbee started successfully!"); Serial.println("Zigbee started successfully!");
} }
leds[0] = CRGB::Green; FastLED.show(); // ZigBee started successfully
rgb.setPixelColor(0,yellow); rgb.show();
Serial.println("Connecting to network"); Serial.println("Connecting to network...");
while (!Zigbee.connected()) { while (!Zigbee.connected()) {
Serial.print("."); Serial.print(".");
delay(100); delay(100);
} }
Serial.println(); Serial.println();
leds[0] = CRGB::Blue; FastLED.show(); // 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 // Optional: If time cluster is added, time can be read from the coordinator
timeinfo = zbTempSensor.getTime(); timeinfo = zbTempSensor.getTime();
timezone = zbTempSensor.getTimezone(); timezone = zbTempSensor.getTimezone();
@ -179,19 +227,24 @@ void setup() {
Serial.println("Local time with timezone:"); Serial.println("Local time with timezone:");
Serial.println(localTime, "%A, %B %d %Y %H:%M:%S"); Serial.println(localTime, "%A, %B %d %Y %H:%M:%S");
// Start Temperature sensor reading task // localtime?
xTaskCreate(temp_sensor_value_update, "temp_sensor_update", 3072, NULL, 10, NULL); rgb.setPixelColor(0,blue); rgb.show();
leds[0] = CRGB::White; FastLED.show();
Serial.println("Setting default ZigBee values and intervals...");
// Set reporting interval for temperature measurement in seconds, must be called after Zigbee.begin() // 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) // 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 = 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 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 // 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(); // 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();
} }