Initial versiob
This commit is contained in:
@@ -0,0 +1 @@
|
||||
config.h
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
#include "DHT.h"
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ModbusIP_ESP8266.h>
|
||||
#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);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
Group SENSOR_NAME_Weather "Weather (SENSOR_NAME)"
|
||||
Number SENSOR_NAME_t "SENSOR_NAME temperature [%.1f °C]" (SENSOR_NAME_Weather) <temperature> {channel="modbus:data:SENSOR_NAME:D:SENSOR_NAME_t:number"}
|
||||
Number SENSOR_NAME_h "SENSOR_NAME humidity [%.1f%%]" (SENSOR_NAME_Weather) <humidity> {channel="modbus:data:SENSOR_NAME:D:SENSOR_NAME_h:number"}
|
||||
@@ -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)"]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user