From 652b94d0c735942a6ed743c5691f421b9c0a2db3 Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Mon, 2 May 2022 15:51:13 +0300 Subject: [PATCH] Initial commit --- .gitignore | 1 + config.h.sample | 5 ++ readme.md | 15 +++++ well_control.ino | 144 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 .gitignore create mode 100644 config.h.sample create mode 100644 readme.md create mode 100644 well_control.ino diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e56cf2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.h diff --git a/config.h.sample b/config.h.sample new file mode 100644 index 0000000..9d6aa18 --- /dev/null +++ b/config.h.sample @@ -0,0 +1,5 @@ + +#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 diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..e35a8b2 --- /dev/null +++ b/readme.md @@ -0,0 +1,15 @@ +Управление колодцем: + +1. На пин D4 повешено реле насоса +2. На пин D8 - датчик AM2302 (температура + влажность) +3. На пин A0 - датчик давления. Так как он выдаёт 0-5V, используем делитель, получаем 0-2.5V + +Создаёт Modbus TCP Server со следующими регистрами: + +0 - температура (умноженная на 100) +1 - влажность (аналогично) +2 - давление (также, умноженное на 100) +3 - текущее состояние насоса (0 - выкл, 1 - вкл) +4 - режим управления (0 - авто, 1 - ручное) +5 - давление включения (умноженное на 100, по умолчанию 150) +6 - давление выключения (умноженное на 100, по умолчанию 250) \ No newline at end of file diff --git a/well_control.ino b/well_control.ino new file mode 100644 index 0000000..37320be --- /dev/null +++ b/well_control.ino @@ -0,0 +1,144 @@ +#include "DHT.h" +#include +#include +#include "config.h" + +#define TCP_PORT 15000 + +#define SENSOR_UPDATE_INTERVAL 500 + +#define MODE_AUTO 0 +#define MODE_MANUAL 1 + +#define REGISTER_CURRENT_TEMP 0 +#define REGISTER_CURRENT_HUM 1 +#define REGISTER_CURRENT_PRESS 2 +#define REGISTER_CUR_STATE 3 +#define REGISTER_MODE 4 +#define REGISTER_P_ON 5 +#define REGISTER_P_OFF 6 + +#define DHTPIN D3 +#define DHTTYPE AM2301 + +ModbusIP mb; +DHT dht(DHTPIN, DHTTYPE); + +char id[64]; + +unsigned long last_update; + +int p_on = 150; +int p_off = 250; +int cur_mode = MODE_AUTO; +int cur_state = 0; +int cur_p = 0; +int cur_t = 0; +int cur_hum = 0; + +void setup() +{ + pinMode(D4, OUTPUT); + pinMode(DHTPIN, INPUT); + pinMode(A0, INPUT); + Serial.begin(115200); + Serial.println("Initialized..."); + + 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); + // DHT sensor init + dht.begin(); + // Modbus init + mb.server(15000); + mb.addHreg(REGISTER_CURRENT_TEMP, cur_t); + mb.addHreg(REGISTER_CURRENT_HUM, cur_hum); + mb.addHreg(REGISTER_CURRENT_PRESS, cur_p); + mb.addHreg(REGISTER_CUR_STATE, cur_state); + mb.addHreg(REGISTER_MODE, cur_mode); + mb.addHreg(REGISTER_P_ON, p_on); + mb.addHreg(REGISTER_P_OFF, p_off); +} + + +void UpdateSensorData() +{ + float h = dht.readHumidity(); + float t = dht.readTemperature(); + Serial.print("read h "); + Serial.print(h); + Serial.print(" read t "); + Serial.println(t); + cur_hum = (int)(h * 100); + cur_t = (int)(t * 100); + int p_int = analogRead(A0); + // we have 10-bit ADC, so get value between 0 and 1 and then multiply by max voltage + // (it is 3.3) + float p_volt = (p_int / 1024.0f) * 3.3f; + // divide by maximal input voltage from sensor (it is 2.5) + // and then multiply by sensor max value (10 bar) - we will get pressure in bar + // finally multiply by 100 as we have int registers + cur_p = (int)(((p_volt / 2.5f) * 10) * 100); +} + +void loop() +{ + unsigned long current = millis(); + int u = 0; + if (current < last_update || current - last_update >= SENSOR_UPDATE_INTERVAL) + { + UpdateSensorData(); + last_update = current; + u = 1; + } + mb.task(); + p_on = mb.Hreg(REGISTER_P_ON); + p_off = mb.Hreg(REGISTER_P_OFF); + cur_mode = mb.Hreg(REGISTER_MODE); + if (cur_mode == MODE_AUTO) + { + if (cur_p < p_on) cur_state = 1; + else if (cur_p > p_off) cur_state = 0; + mb.Hreg(REGISTER_CUR_STATE, cur_state); + } + else + { + cur_state = mb.Hreg(REGISTER_CUR_STATE); + } + digitalWrite(D4, cur_state); + if (u) + { + Serial.print("Current pressure: "); + Serial.print(cur_p); + Serial.print(" t "); + Serial.print(cur_t); + Serial.print(" h "); + Serial.print(cur_hum); + Serial.print(" state "); + Serial.println(cur_state); + } + mb.Hreg(REGISTER_P_ON, p_on); + mb.Hreg(REGISTER_P_OFF, p_off); + mb.Hreg(REGISTER_MODE, cur_mode); + mb.Hreg(REGISTER_CURRENT_TEMP, cur_t); + mb.Hreg(REGISTER_CURRENT_HUM, cur_hum); + mb.Hreg(REGISTER_CURRENT_PRESS, cur_p); + if (WiFi.status() != WL_CONNECTED) + { + // no wifi + Serial.println("No WiFi connection"); + ESP.restart(); + } +}