From f55f9dd714da0e96a2badbf724f66af89461b9ce Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Sun, 8 Oct 2023 16:06:40 +0300 Subject: [PATCH] Initial commit --- config.h | 8 +++ config.h.sample | 8 +++ defrost_v3.ino | 145 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 config.h create mode 100644 config.h.sample create mode 100644 defrost_v3.ino diff --git a/config.h b/config.h new file mode 100644 index 0000000..819e7e4 --- /dev/null +++ b/config.h @@ -0,0 +1,8 @@ + +#define WIFI_NAME "ftc_zaozerye" +#define WIFI_PASS "prevedlocalmedved" +#define REMOTE_IP "192.168.0.10" +#define REMOTE_PORT 7000 +#define MY_IP 192,168,0,21 +#define MY_GW 192,168,0,1 +#define MY_NET 255,255,255,0 diff --git a/config.h.sample b/config.h.sample new file mode 100644 index 0000000..22910e6 --- /dev/null +++ b/config.h.sample @@ -0,0 +1,8 @@ + +#define WIFI_NAME "your wi-fi name" +#define WIFI_PASS "you wi-fi password" +#define REMOTE_ADDR "IP address of host to send data to" +#define REMOTE_PORT 7000 +#define MY_IP 192,168,0,21 +#define MY_GW 192,168,0,1 +#define MY_NET 255,255,255,0 diff --git a/defrost_v3.ino b/defrost_v3.ino new file mode 100644 index 0000000..9b28534 --- /dev/null +++ b/defrost_v3.ino @@ -0,0 +1,145 @@ +#include +#include +#include "config.h" + +// network + +IPAddress my_ip(MY_IP); +IPAddress gateway(MY_GW); +IPAddress subnet(MY_NET); + +// modbus consts and works +#define TCP_PORT 15000 +#define POLL_INTERVAL 10000 +#define T_UPDATE_INTERVAL 500 + +#define MODE_AUTO 0 +#define MODE_MANUAL 1 + +#define REGISTER_ON_TEMP 0 +#define REGISTER_OFF_TEMP 1 +#define REGISTER_MODE 2 +#define REGISTER_CUR_TEMP 3 +#define REGISTER_CUR_STATE 4 +#define REGISTER_SECONDARY_TEMP 5 + +ModbusIP mb; + +char id[64]; + +long last_poll; +long last_update; + +int t_on = 100; +int t_off = 1000; +int cur_mode = MODE_AUTO; +int cur_t = 0; +int cur_state = 0; + +void setup() +{ + pinMode(D2, OUTPUT); + Serial.begin(115200); + Serial.println("Initialised..."); + + WiFi.mode(WIFI_STA); + WiFi.config(my_ip, gateway, subnet); + WiFi.setAutoReconnect(true); + WiFi.begin(WIFI_NAME, WIFI_PASS); + Serial.print("Connecting"); + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + } + Serial.println(); + + Serial.print("Connected, IP address: "); + Serial.println(WiFi.localIP()); + uint8_t macAddr[6]; + WiFi.macAddress(macAddr); + sprintf(id, "%02x:%02x:%02x:%02x:%02x:%02x", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]); + Serial.printf("Connected, mac address: %s\n", id); + last_poll = millis(); + + mb.server(15000); + mb.addHreg(REGISTER_ON_TEMP, t_on); + mb.addHreg(REGISTER_OFF_TEMP, t_off); + mb.addHreg(REGISTER_MODE, cur_mode); + mb.addHreg(REGISTER_CUR_TEMP, cur_t); + mb.addHreg(REGISTER_CUR_STATE, cur_state); + mb.addHreg(REGISTER_SECONDARY_TEMP, cur_t); +} + +// thermistor +#define THERMISTORPIN A0 +#define THERMISTORNOMINAL 10000 +#define TEMPERATURENOMINAL 25 +#define NUMSAMPLES 5 +#define BCOEFFICIENT 3950 +#define SERIESRESISTOR 10000 + +void UpdateCurrent() +{ + float average = 0; + for (int i = 0; i < NUMSAMPLES; i++) + { + average += analogRead(THERMISTORPIN); + delay(10); + } + average /= NUMSAMPLES; + average = 1023 / average - 1; + average = SERIESRESISTOR / average; + float steinhart; + steinhart = average / THERMISTORNOMINAL; // (R/Ro) + steinhart = log(steinhart); // ln(R/Ro) + steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro) + steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To) + steinhart = 1.0 / steinhart; // инвертируем + steinhart -= 273.15; // конвертируем в градусы по Цельсию + cur_t = int(steinhart * 100); + last_update = millis(); +} + +void loop() +{ + unsigned long current = millis(); + int u = 0; + if (current < last_update || current - last_update >= T_UPDATE_INTERVAL) + { + UpdateCurrent(); + u = 1; + } + mb.task(); + t_on = mb.Hreg(REGISTER_ON_TEMP); + t_off = mb.Hreg(REGISTER_OFF_TEMP); + cur_mode = mb.Hreg(REGISTER_MODE); + if (cur_mode == 0) + { + if (cur_t < t_on) cur_state = 1; + else if (cur_t > t_off) cur_state = 0; + mb.Hreg(REGISTER_CUR_STATE, cur_state); + } + else + { + cur_state = mb.Hreg(REGISTER_CUR_STATE); + } + digitalWrite(D2, cur_state); + if (u) + { + Serial.println("Cur state"); + Serial.println(cur_state); + Serial.println(cur_t); + } + mb.Hreg(REGISTER_ON_TEMP, t_on); + mb.Hreg(REGISTER_OFF_TEMP, t_off); + mb.Hreg(REGISTER_MODE, cur_mode); + mb.Hreg(REGISTER_CUR_TEMP, cur_t); + mb.Hreg(REGISTER_SECONDARY_TEMP, cur_t); + if (WiFi.status() != WL_CONNECTED) + { + // no wifi + Serial.println("No WiFi connection"); + ESP.restart(); + } +}