From 12312977df4491c228bd33a28d5172a0acc1d9ce Mon Sep 17 00:00:00 2001 From: lexa Date: Sat, 18 Jan 2025 16:43:46 +0300 Subject: [PATCH] Added BOX Balkon, LightTBL D1-Mini --- Box-ESP12/include/main.h | 29 ++++-- Box-ESP12/platformio.ini | 6 +- Box-ESP12/src/main.cpp | 139 ++++++++++++++++++--------- KuhLight_ESP/.vscode/extensions.json | 17 ++-- KuhLight_ESP/platformio.ini | 15 ++- KuhLight_ESP/src/main.cpp | 8 +- 6 files changed, 147 insertions(+), 67 deletions(-) diff --git a/Box-ESP12/include/main.h b/Box-ESP12/include/main.h index fdb139d..4874720 100644 --- a/Box-ESP12/include/main.h +++ b/Box-ESP12/include/main.h @@ -7,6 +7,7 @@ #include #include #include +#include "OneButton.h" #include //#include "LedController.hpp" @@ -19,7 +20,12 @@ Disp7219<1> disp(DIO_PIN, CLK_PIN, LAT_PIN); #include #include -#include +//#include +#include "GyverPID.h" +GyverPID regulator; +#include "PWMrelay.h" +#define TRIAC_PIN 15 +PWMrelay relay(TRIAC_PIN, true); // реле на 15 пине #define ONE_WIRE_BUS 2 @@ -30,7 +36,11 @@ Disp7219<1> disp(DIO_PIN, CLK_PIN, LAT_PIN); #define TOPIC "home/box/" -#define TRIAC_PIN 4 + +#define PLUS_BUT 16 +#define MINUS_BUT 0 +OneButton plusBut(PLUS_BUT, true, true); +OneButton minusBut(MINUS_BUT, true, true); WiFiClient espClient; PubSubClient client(espClient); @@ -40,8 +50,6 @@ Ticker wifiReconnectTimer; AsyncWebServer server(80); -//LedController lc; - OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); @@ -50,9 +58,9 @@ float temp_sp, temp1, temp2; float kp = 0, ki = 0, kd = 0; double Output = 0, Setpoint = 0, Input = 0; -PID myPID(&Input, &Output, &Setpoint, kp, ki, kd, DIRECT); -unsigned long WindowSize = 5000; -unsigned long windowStartTime; +// PID myPID(&Input, &Output, &Setpoint, kp, ki, kd, DIRECT); +// unsigned long WindowSize = 5000; +// unsigned long windowStartTime; long lastReconnectAttempt = 0; @@ -64,4 +72,9 @@ void onWifiConnect(const WiFiEventStationModeGotIP& event); void onWifiDisconnect(const WiFiEventStationModeDisconnected& event); void callback(char* topic, byte* payload, unsigned int length); boolean reconnect(); -void out_value(byte digit, float val); +void PlusButPress(); +void PlusButLongPress(); +void MinusButPress(); +void MinusButLongPress(); +void display(); +void pubData(const char* topic, float val); \ No newline at end of file diff --git a/Box-ESP12/platformio.ini b/Box-ESP12/platformio.ini index 0e702b1..9e88b8d 100644 --- a/Box-ESP12/platformio.ini +++ b/Box-ESP12/platformio.ini @@ -21,7 +21,9 @@ lib_deps = ottowinter/ESPAsyncWebServer-esphome @ ^2.1.0 ayushsharma82/WebSerial @ ^1.3.0 jwrw/ESP_EEPROM @ ^2.0.0 - br3ttb/PID @ ^1.2.1 + ;br3ttb/PID @ ^1.2.1 milesburton/DallasTemperature @ ^3.11.0 - thomasfredericks/Bounce2 @ ^2.72 gyverlibs/GyverSegment @ ^1.4.7 + mathertel/OneButton @ ^2.6.1 + gyverlibs/PWMrelay @ ^1.2 + gyverlibs/GyverPID @ ^3.3.2 diff --git a/Box-ESP12/src/main.cpp b/Box-ESP12/src/main.cpp index c0e5d8e..b12436c 100644 --- a/Box-ESP12/src/main.cpp +++ b/Box-ESP12/src/main.cpp @@ -37,50 +37,72 @@ void setup() { temp1 = sensors.getTempCByIndex(0); if(temp1 < -100) temp1 = -99; - EEPROM.begin(8); + EEPROM.begin(16); EEPROM.get(0, temp_sp); //if(isnan(temp_sp) || temp_sp == 0) temp_sp = 4.0f; //out_value(1, temp_sp); - EEPROM.get(2, kp); + EEPROM.get(4, kp); //if(isnan(kp) || kp == 0) kp = 2.0f; - EEPROM.get(4, ki); + EEPROM.get(8, ki); //if(isnan(ki) || ki == 0) ki = 2.0f; - EEPROM.get(6, kd); + EEPROM.get(12, kd); //if(isnan(kd)) kd = 0.0f; - // // myPID.SetTunings(kp, ki, kd); - // // Setpoint = temp_sp; - // // windowStartTime = millis(); - // // myPID.SetOutputLimits(0, WindowSize); - // // myPID.SetMode(AUTOMATIC); - // lastReconnectAttempt = 0; + Setpoint = temp_sp; + // windowStartTime = millis(); + // myPID.SetOutputLimits(0, WindowSize); + // myPID.SetMode(AUTOMATIC); + // myPID.SetTunings(kp, ki, kd); + // // lastReconnectAttempt = 0; + regulator.setDirection(NORMAL); // направление регулирования (NORMAL/REVERSE). ПО УМОЛЧАНИЮ СТОИТ NORMAL + regulator.setLimits(0, 255); // пределы (ставим для 8 битного ШИМ). ПО УМОЛЧАНИЮ СТОЯТ 0 И 255 + regulator.Kp = kp; + regulator.Ki = ki; + regulator.Kd = kd; + regulator.setpoint = temp_sp; + + plusBut.attachClick(PlusButPress); + plusBut.attachDuringLongPress(PlusButLongPress); + plusBut.setLongPressIntervalMs(1000); + minusBut.attachClick(MinusButPress); + minusBut.attachDuringLongPress(MinusButLongPress); + minusBut.setLongPressIntervalMs(1000); + pinMode(TRIAC_PIN, OUTPUT); Serial.println("setup end"); } void loop() { static unsigned long cRun = millis(); static bool measTemp = false; + static int secs = 0; ArduinoOTA.handle(); + plusBut.tick(); + minusBut.tick(); if(cRun + 1000 <= millis()){ cRun = millis(); Serial.print("WiFi: ");Serial.print(wifi); Serial.print(", MQTT: ");Serial.println(client.connected()); + if(measTemp) { temp1 = sensors.getTempCByIndex(0); if(temp1 < -100) temp1 = -99.0; - //out_value(0, temp1); - Input = temp1; + regulator.input = temp1; } else{ sensors.requestTemperatures(); } + Serial.print("Input: ");Serial.println(temp1); + Serial.print("SetPoint: ");Serial.println(temp_sp); + Serial.print("Regulator: ");Serial.println(regulator.getResultTimer()); measTemp = !measTemp; - disp.setCursor(1); - disp.printf("%4.1f%5.1f", temp_sp, temp1); - disp.update(); + display(); + if(++secs == 10){ + secs = 0; + pubData(TOPIC"temp1", temp1); + } } if (wifi && !client.connected()) { long now = millis(); @@ -96,18 +118,12 @@ void loop() { // Client connected client.loop(); } - Input = temp1; - if(Input > -99){ - myPID.Compute(); - - if (millis() - windowStartTime > WindowSize) - { //time to shift the Relay Window - windowStartTime += WindowSize; - } - if (Output < millis() - windowStartTime) digitalWrite(TRIAC_PIN, HIGH); - else digitalWrite(TRIAC_PIN, LOW); + if(temp1 > -99){ + relay.setPWM(regulator.getResultTimer()); + relay.tick(); } - else digitalWrite(TRIAC_PIN, LOW); + else + digitalWrite(TRIAC_PIN, LOW); } void connectToWifi() { @@ -155,22 +171,25 @@ void callback(char* topic, byte* payload, unsigned int length) { temp_sp = atof(val); EEPROM.put(0, temp_sp); EEPROM.commit(); - out_value(1, temp_sp); + regulator.setpoint = temp_sp; } if(strcmp(topic, TOPIC"kp") == 0){ kp = atof(val); - EEPROM.put(2, kp); + EEPROM.put(4, kp); EEPROM.commit(); + regulator.Kp = kp; } if(strcmp(topic, TOPIC"ki") == 0){ ki = atof(val); - EEPROM.put(4, ki); + EEPROM.put(8, ki); EEPROM.commit(); + regulator.Ki = ki; } if(strcmp(topic, TOPIC"kd") == 0){ kd = atof(val); - EEPROM.put(6, kd); + EEPROM.put(12, kd); EEPROM.commit(); + regulator.Kd = kd; } } @@ -217,20 +236,48 @@ boolean reconnect() { //digitalWrite(LED_MQ, HIGH); } -void out_value(byte digit, float val){ - // if(!isnan(val)){ - // int value = (val * 100.0) + 0.5; - // char ch = '0' + ((value / 100) % 10); - // lc.setChar(0, 2 + digit * 4, ch, false); - // ch = '0' + ((value / 10) % 10); - // lc.setChar(0 , 1 + digit * 4, ch, true); - // ch = '0' + (value % 10); - // lc.setChar(0 , 0 + digit * 4, ch, false); - // } - // else{ - // lc.setChar(0, 2 + digit * 4, 'E', false); - // lc.setRow(0, 1 + digit * 4, 0); - // lc.setRow(0, 0 + digit * 4, 0); - - // } +void PlusButPress(){ + temp_sp += 0.1f; + EEPROM.put(0, temp_sp); + EEPROM.commit(); + display(); + regulator.setpoint = temp_sp; } + +void PlusButLongPress(){ + temp_sp += 1.0f; + EEPROM.put(0, temp_sp); + EEPROM.commit(); + regulator.setpoint = temp_sp; +} + +void MinusButPress(){ + temp_sp -= 0.1f; + EEPROM.put(0, temp_sp); + EEPROM.commit(); + display(); + regulator.setpoint = temp_sp; +} + +void MinusButLongPress(){ + temp_sp -= 1.0f; + EEPROM.put(0, temp_sp); + EEPROM.commit(); + regulator.setpoint = temp_sp; +} + +void display(){ + disp.setCursor(1); + disp.printf("%4.1f%5.1f", temp_sp, temp1); + disp.update(); +} + +void setSP(){ + regulator.setpoint = temp_sp; +} + +void pubData(const char* topic, float val){ + char vout[7]; + sprintf(vout, "%.1f", val); + client.publish(topic, vout); +} \ No newline at end of file diff --git a/KuhLight_ESP/.vscode/extensions.json b/KuhLight_ESP/.vscode/extensions.json index 0f0d740..080e70d 100644 --- a/KuhLight_ESP/.vscode/extensions.json +++ b/KuhLight_ESP/.vscode/extensions.json @@ -1,7 +1,10 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/KuhLight_ESP/platformio.ini b/KuhLight_ESP/platformio.ini index 5b21ffa..115dac5 100644 --- a/KuhLight_ESP/platformio.ini +++ b/KuhLight_ESP/platformio.ini @@ -7,6 +7,8 @@ ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html +[platformio] +default_envs = wemos-d1 [env:esp07] platform = espressif8266 @@ -15,4 +17,15 @@ framework = arduino board_build.f_cpu = 26000000L board_build.ldscript = eagle.flash.1m.ld upload_protocol = espota -upload_port = 192.168.1.135 \ No newline at end of file +upload_port = 192.168.1.135 +[env:wemos-d1] +framework = arduino +platform = espressif8266 +board = d1_mini +upload_protocol = espota +upload_port = 192.168.1.135 + +lib_deps = + # RECOMMENDED + # Accept new functionality in a backwards compatible manner and patches + heman/AsyncMqttClient-esphome @ ^2.1.0 \ No newline at end of file diff --git a/KuhLight_ESP/src/main.cpp b/KuhLight_ESP/src/main.cpp index d7b8420..ee0fc65 100644 --- a/KuhLight_ESP/src/main.cpp +++ b/KuhLight_ESP/src/main.cpp @@ -110,13 +110,14 @@ void loop() { nSec++; //led_wrk = !led_wrk; //digitalWrite(LED_WRK, led_wrk); - if(nSec > 59){ + if(nSec > 29){ if(mqttClient.connected()){ char v[11]; - ultoa(millis(), v, 10); + dtostrf(millis() / 60000.0, 7, 1, v); + //ultoa(millis(), v, 10); digitalWrite(LED_WRK, LOW); led_ms = millis(); - mqttClient.publish(TOPIC"kuh/ltblmillis", 0, false, v); + mqttClient.publish(TOPIC"kuh/ltblminuts", 0, false, v); } nSec = 0; } @@ -141,6 +142,7 @@ void connectToWifi() { void connectToMqtt() { mqttClient.connect(); + mqttClient.setWill(TOPIC"kuh/lighttblOnline", 1, false); } void onWifiConnect(const WiFiEventStationModeGotIP& event) {