From 9075fb1c037d0a29d6da61718dd06a93b2c20bab Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Wed, 7 Sep 2022 23:11:20 +0300 Subject: [PATCH] Initial versiob --- .gitignore | 1 + config.h.sample | 9 +++++ make_config.pl | 27 ++++++++++++++ thermo_sensor_v2.ino | 81 +++++++++++++++++++++++++++++++++++++++++ thermo_sensor_v2.items | 3 ++ thermo_sensor_v2.things | 6 +++ 6 files changed, 127 insertions(+) create mode 100644 .gitignore create mode 100644 config.h.sample create mode 100644 make_config.pl create mode 100644 thermo_sensor_v2.ino create mode 100644 thermo_sensor_v2.items create mode 100644 thermo_sensor_v2.things 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..4ac3887 --- /dev/null +++ b/config.h.sample @@ -0,0 +1,9 @@ + +#define WIFI_NAME "your wi-fi name" +#define WIFI_PASS "you wi-fi password" +// IP configuration. Comma-separated to make it convenient +// to substitute them into constructor. +#define MY_IP 192,168,0,25 +#define MY_GW 192,168,0,1 +#define MY_NET 255,255,255,0 + diff --git a/make_config.pl b/make_config.pl new file mode 100644 index 0000000..84d5217 --- /dev/null +++ b/make_config.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl + +if (scalar(@ARGV) < 1) +{ + die("Usage: make_config.pl SENSOR_NAME"); +} +my $name = shift @ARGV; + +Process("thermo_sensor_v2.things", $name.".things"); +Process("thermo_sensor_v2.items", $name.".items"); + +sub Process +{ + my $src = shift; + my $dst = shift; + my $fh; + open($fh, "< ".$src); + my $ofh; + open($ofh, "> ".$dst); + while (<$fh>) + { + $_ =~ s/SENSOR_NAME/$name/g; + print($ofh $_); + } + close($fh); + close($ofh); +} \ No newline at end of file diff --git a/thermo_sensor_v2.ino b/thermo_sensor_v2.ino new file mode 100644 index 0000000..b3df161 --- /dev/null +++ b/thermo_sensor_v2.ino @@ -0,0 +1,81 @@ +#include "DHT.h" +#include +#include +#include "config.h" + +#define DHTPIN 5 +#define DHTTYPE DHT22 +#define READ_INTERVAL 100 +#define TCP_PORT 15000 + +IPAddress my_ip(MY_IP); +IPAddress gateway(MY_GW); +IPAddress subnet(MY_NET); + +ModbusIP mb; +DHT dht(DHTPIN, DHTTYPE); +char id[64]; + +#define REGISTER_CURRENT_TEMP 0 +#define REGISTER_CURRENT_HUM 1 + +int cur_t = 0; +int cur_hum = 0; + + +void setup() +{ + Serial.begin(115200); + Serial.println("Initialised..."); + pinMode(DHTPIN, INPUT); + + dht.begin(); + + 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); + + // Modbus init + mb.server(15000); + mb.addHreg(REGISTER_CURRENT_TEMP, cur_t); + mb.addHreg(REGISTER_CURRENT_HUM, cur_hum); +} + +void UpdateDHTData() +{ + float h = dht.readHumidity(); + float t = dht.readTemperature(); + cur_hum = (int)(h * 100); + cur_t = (int)(t * 100); +} + +unsigned long last_read = 0; + +void loop() +{ + unsigned long current = millis(); + if (current < last_read || current - last_read >= READ_INTERVAL) + { + UpdateDHTData(); + last_read = current; + } + mb.task(); + mb.Hreg(REGISTER_CURRENT_TEMP, cur_t); + mb.Hreg(REGISTER_CURRENT_HUM, cur_hum); +} diff --git a/thermo_sensor_v2.items b/thermo_sensor_v2.items new file mode 100644 index 0000000..5c62279 --- /dev/null +++ b/thermo_sensor_v2.items @@ -0,0 +1,3 @@ +Group SENSOR_NAME_Weather "Weather (SENSOR_NAME)" +Number SENSOR_NAME_t "SENSOR_NAME temperature [%.1f °C]" (SENSOR_NAME_Weather) {channel="modbus:data:SENSOR_NAME:D:SENSOR_NAME_t:number"} +Number SENSOR_NAME_h "SENSOR_NAME humidity [%.1f%%]" (SENSOR_NAME_Weather) {channel="modbus:data:SENSOR_NAME:D:SENSOR_NAME_h:number"} diff --git a/thermo_sensor_v2.things b/thermo_sensor_v2.things new file mode 100644 index 0000000..c695949 --- /dev/null +++ b/thermo_sensor_v2.things @@ -0,0 +1,6 @@ +Bridge modbus:tcp:SENSOR_NAME [ host="192.168.0.24", port=15000, id=1 ] { + Bridge poller D[start = 0, length = 2, type = "holding", refresh = 500] { + Thing data SENSOR_NAME_t[readStart = "0", readValueType = "int16", readTransform="JS(divide100.js)"] + Thing data SENSOR_NAME_h[readStart = "1", readValueType = "int16", readTransform="JS(divide100.js)"] + } +}