-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsht31_mqtt_domoticz.ino
261 lines (228 loc) · 6.46 KB
/
sht31_mqtt_domoticz.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* version = "sht31_mqtt_domoticz 2.1";
const char* date_name = "2018-02-17 mike";
// SHT31 temperature and humidity sensor, connected to D1, D2
// on Wemos D1 mini. Connection according to
// http://www.esp8266learning.com/wemos-and-sht31.php
// Example use from server side for obtaining data from mqtt broker:
// mosquitto_sub -h localhost -t '#' -v
// Wifi settings
const char* ssid = "*****";
const char* password = "*****";
// device id in domoticz
//
// 67 - Garage
// 68 - Sovrum
//
const char* clientID = "67";
// The following parameters are only required for fixed IP,
// make sure WiFi.config() is uncommented as well for this to work
IPAddress ip(172, 16, 0, 67);
IPAddress gateway(172, 16, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(172, 16, 0, 18);
// How long in seconds to sleep until next check
int sleepDelay = 10;
// MQTT server and topic settings
const char* mqtt_server = "172.16.0.19";
//const char* mqtt_ID = "TempHum_";
const char* domoticzTopic = "domoticz/in";
const char* outTopic = "esp8266/out"; // info and errors to here
const char* inTopic = "esp8266/in"; // subscribe to this topic
Adafruit_SHT31 sht31 = Adafruit_SHT31();
WiFiClient espClient;
PubSubClient client(espClient);
/*
* Functions etc
*/
void setup_wifi()
{
delay(10);
Serial.println("WiFi setup started");
// Connect to WiFi network
//WiFi.config(ip, gateway, subnet, dns); // Only required for fixed IP
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address:");
Serial.println(WiFi.localIP());
Serial.println("");
}
void setup_sht()
{
// Connect to SHT31
if (! sht31.begin(0x44))
{
char msg[50] = "Error: Couldn't find SHT31..";
strcat(msg, clientID);
client.publish(outTopic, msg);
Serial.println(msg);
while (1) delay(1);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
// Check incoming message using really ugly code
String nstring = "";
Serial.println("Message arrived [" + String(topic) + "] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
nstring = String (nstring + (char)payload[i]);
}
nstring = String (nstring + '\0');
Serial.println("");
Serial.println("nstring = " + String(nstring));
int value;
if (String(topic + '\0' == inTopic)) {
value = nstring.toInt(); // (long) 0 is returned if unsuccessful)
if (value > 0 ) {
Serial.println(String(value) + " requested");
// print message to mqtt
String data = "status requested for " + String(clientID) + ". My ip is " + WiFi.localIP().toString();
int length = data.length();
char msgBuffer[length];
data.toCharArray(msgBuffer,length+1);
client.publish(outTopic, msgBuffer);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// create identifier for mqtt
char mqtt_ID[20] = "ESP8266Client-";
strcat(mqtt_ID, clientID);
// Attempt to connect
if (client.connect(mqtt_ID)) {
Serial.println("connected");
// Once connected, publish an announcement...
char msg[50] = "this is a new connection setup, from client ID ";
strcat(msg, clientID);
client.publish(outTopic, msg);
// ... and resubscribe
client.subscribe(inTopic);
} else {
Serial.println("failed, rc=" + String(client.state()) + ", trying again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
//pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
setup_sht();
}
void sht31_temp_hum()
{
// Get data
float temperature = sht31.readTemperature();
float humidity = sht31.readHumidity();
int error = 0;
// Set start of mqtt message
// Data will be: TEMP;HUM;HUM_STAT
char mqtt_msg[60] = "{ \"idx\" : ";
strcat(mqtt_msg, clientID);
strcat(mqtt_msg, ", \"nvalue\" : 0, \"svalue\" : \"");
if (! isnan(temperature))
{
// Temperature read
Serial.println("Temp *C = " + String(temperature));
char msg[25];
dtostrf(temperature , 5, 1, msg); // width including sign, "." . etc., precision/decimals
// remove blanks
int pos = 0;
for (int i = 0; i < sizeof(msg); i ++) {
if (msg[i] != ' ') {
msg[pos] = msg[i];
pos++;
}
}
// add data to mqtt message
strcat(mqtt_msg, msg);
}
else
{
error++;
char msg[50] = "Failed to read temperature, for client ID ";
strcat(msg, clientID);
client.publish(outTopic, msg);
Serial.println(msg);
}
strcat(mqtt_msg, ";");
if (! isnan(humidity))
{
// Humidity read
Serial.println("Hum. % = " + String(humidity));
char msg[25];
dtostrf(humidity , 5, 1, msg); // width including sign, "." etc., precision/decimals
// remove blanks
int pos = 0;
for (int i = 0; i < sizeof(msg); i ++) {
if (msg[i] != ' ') {
msg[pos] = msg[i];
pos++;
}
}
// add data to mqtt message
strcat(mqtt_msg, msg);
strcat(mqtt_msg, ";");
// Humidity status calculation
// 0=Normal
// 1=Comfortable
// 2=Dry
// 3=Wet
int hum_stat = 0;
if (humidity < 30){
hum_stat = 2;
}
else if (humidity > 70){
hum_stat = 3;
}
else if (humidity > 45 && humidity < 50){
hum_stat = 1;
}
Serial.println("Hum. status = " + String(hum_stat));
char buf[8];
sprintf(buf,"%d",hum_stat);
strcat(mqtt_msg, buf);
}
else
{
error++;
char msg[50] = "Failed to read humidity, for client ID ";
strcat(msg, clientID);
client.publish(outTopic, msg);
Serial.println(msg);
}
strcat(mqtt_msg, "\" }");
// publish to topic, unless both have failed
if (error != 2)
{
client.publish(domoticzTopic, mqtt_msg);
}
Serial.println();
}
void loop()
{
// Check if MQTT client is connected, otherwise reconnect
if (!client.connected()) {
reconnect();
}
// maintain the MQTT connection and check for incoming messages
client.loop();
// Get data from temp sensor (temperature + humidity) and send to mqtt queue
sht31_temp_hum();
delay(sleepDelay * 1000);
}