-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp_hum.ino
234 lines (176 loc) · 4.92 KB
/
temp_hum.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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#include <DS3231.h>
#include <ArduinoJson.h>
#define DHTTYPE DHT11
#define DHTPIN 2
const char* ssid = "Cloud_2";
const char* ssid_password = "";
const char* mqtt_server = "192.168.0.110";
const char* mqtt_device_name = "temp_hum_03";
const char* outTopic = "home/out";
const char* inTopic = "home/in";
const char* debugTopic = "home/debug";
const char* tempSensorName = "temp_sensor_03";
const char* humiditySensorName = "humidity_sensor_03";
const char* roomName = "green_bedroom";
const char* CMD_STATE = "state";
const char* NAME_ALL = "all";
WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);
int humidity, temp;
int lastSentHumidity = 0, lastSentTemp = 0, lastSentWifiStrength = 0;
long wifiStrength = 0;
boolean wifi_connecting = false, wifi_connected = false, wifi_error = false;
boolean mqtt_connecting = false, mqtt_connected = false, mqtt_error = false;
void setup() {
delay(100);
Serial.begin(115200);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
dht.begin();
}
void loop() {
setup_wifi();
if (wifi_connected) {
setup_mqtt();
}
bool valuesUpdated = updateValues();
if (valuesUpdated && mqtt_connected) {
sendLastValues();
}
// debugPrint();
client.loop();
delay(500);
}
void setup_mqtt() {
mqtt_connected = client.connected();
if (mqtt_connected) {
return;
}
if (!mqtt_connecting) {
mqtt_connecting = true;
if (client.connect(mqtt_device_name)) {
mqtt_error = false;
mqtt_connecting = false;
mqtt_connected = true;
client.subscribe(inTopic);
} else {
mqtt_error = true;
mqtt_connecting = false;
mqtt_connected = false;
}
}
}
void setup_wifi() {
wifi_connected = WiFi.status() == WL_CONNECTED;
wifi_error = WiFi.status() == WL_CONNECT_FAILED;
if (wifi_connected || wifi_error) {
wifi_connecting = false;
}
if (wifi_connected) {
return;
}
if (!wifi_connecting) {
wifi_connecting = true;
WiFi.begin(ssid, ssid_password);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
if (!eq(topic, inTopic)) {
return;
}
DynamicJsonBuffer jsonBuffer;
char jsonMessageBuffer[256];
JsonObject& message = jsonBuffer.parseObject(payload);
if (!message.success())
{
client.publish(debugTopic, mqtt_device_name);
client.publish(debugTopic, "cannot parse message");
return;
}
// message.printTo(jsonMessageBuffer, sizeof(jsonMessageBuffer));
// Serial.print("<-- ");
// Serial.println(jsonMessageBuffer);
const char* messageName = message["name"];
if (!eq(messageName, tempSensorName) && !eq(messageName, humiditySensorName) && !eq(messageName, NAME_ALL)) {
return;
}
const char* messageCmd = message["cmd"];
if (eq(messageCmd, CMD_STATE)) {
sendLastValues();
}
}
void sendLastValues() {
sendTemp();
sendHumidity();
lastSentWifiStrength = wifiStrength;
}
void sendTemp() {
DynamicJsonBuffer jsonBuffer;
char jsonMessageBuffer[256];
JsonObject& root = jsonBuffer.createObject();
root["name"] = tempSensorName;
root["type"] = "temp_sensor";
root["room"] = roomName;
root["signal"] = wifiStrength;
root["state"] = 1;
root["value"] = temp;
root.printTo(jsonMessageBuffer, sizeof(jsonMessageBuffer));
Serial.println(jsonMessageBuffer);
client.publish(outTopic, jsonMessageBuffer);
lastSentTemp = temp;
}
void sendHumidity() {
DynamicJsonBuffer jsonBuffer;
char jsonMessageBuffer[256];
JsonObject& root = jsonBuffer.createObject();
root["name"] = humiditySensorName;
root["type"] = "humidity_sensor";
root["room"] = roomName;
root["signal"] = wifiStrength;
root["state"] = 1;
root["value"] = humidity;
root.printTo(jsonMessageBuffer, sizeof(jsonMessageBuffer));
Serial.println(jsonMessageBuffer);
client.publish(outTopic, jsonMessageBuffer);
lastSentHumidity = humidity;
}
bool updateValues() {
// float to int conversion here
int t = dht.readTemperature();
int h = dht.readHumidity();
if (t > 0 && t < 99) {
temp = t;
}
if (h > 0 && h < 99) {
humidity = h;
}
if (wifi_connected) {
wifiStrength = WiFi.RSSI();
} else {
wifiStrength = 0;
}
return lastSentHumidity != humidity || lastSentTemp != temp || abs(lastSentWifiStrength - wifiStrength) > 3;
}
boolean eq(const char* a1, const char* a2) {
return strcmp(a1, a2) == 0;
}
void debugPrint() {
Serial.print("wifi connecting = ");
Serial.println(wifi_connecting);
Serial.print("wifi connected = ");
Serial.println(wifi_connected);
Serial.print("wifi error = ");
Serial.println(wifi_error);
Serial.print("mqtt connecting = ");
Serial.println(mqtt_connecting);
Serial.print("mqtt connected = ");
Serial.println(mqtt_connected);
Serial.print("mqtt error = ");
Serial.println(mqtt_error);
Serial.print("mqtt state = ");
Serial.println(client.state());
}