Added BOX Balkon, LightTBL D1-Mini
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <WebSerial.h>
|
||||
#include <ESP_EEPROM.h>
|
||||
#include <PubSubClient.h>
|
||||
#include "OneButton.h"
|
||||
|
||||
#include <SPI.h>
|
||||
//#include "LedController.hpp"
|
||||
@@ -19,7 +20,12 @@ Disp7219<1> disp(DIO_PIN, CLK_PIN, LAT_PIN);
|
||||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
|
||||
#include <PID_v1.h>
|
||||
//#include <PID_v1.h>
|
||||
#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);
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user