-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTT_ESP8266_DHT11_json.ino
210 lines (178 loc) · 5.92 KB
/
MQTT_ESP8266_DHT11_json.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
/*
MQTT Sensor - Temperature and Humidity (DHT22) for Home-Assistant - NodeMCU (ESP8266)
https://home-assistant.io/components/sensor.mqtt/
Libraries :
- ESP8266 core for Arduino : https://github.com/esp8266/Arduino
- PubSubClient : https://github.com/knolleary/pubsubclient
- DHT : https://github.com/adafruit/DHT-sensor-library
- ArduinoJson : https://github.com/bblanchon/ArduinoJson
Sources :
- File > Examples > ES8266WiFi > WiFiClient
- File > Examples > PubSubClient > mqtt_auth
- File > Examples > PubSubClient > mqtt_esp8266
- File > Examples > DHT sensor library > DHTtester
- File > Examples > ArduinoJson > JsonGeneratorExample
- http://www.jerome-bernard.com/blog/2015/10/04/wifi-temperature-sensor-with-nodemcu-esp8266/
Schematic :
- https://github.com/mertenats/open-home-automation/blob/master/ha_mqtt_sensor_dht22/Schematic.png
- DHT22 leg 1 - VCC
- DHT22 leg 2 - D1/GPIO5 - Resistor 4.7K Ohms - GND
- DHT22 leg 4 - GND
- D0/GPIO16 - RST (wake-up purpose)
Configuration (HA) :
sensor 1:
platform: mqtt
state_topic: 'living/esp8266_12f'
name: 'Temperature'
unit_of_measurement: '°C'
value_template: '{{ value_json.temperature }}'
sensor 2:
platform: mqtt
state_topic: 'living/esp8266_12f'
name: 'Humidity'
unit_of_measurement: '%'
value_template: '{{ value_json.humidity }}'
Samuel M. - v1.1 - 08.2016
If you like this example, please add a star! Thank you!
https://github.com/mertenats/open-home-automation
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#include <ArduinoJson.h>
#define MQTT_VERSION MQTT_VERSION_3_1_1
#define LED_BUILTIN 2
// Wifi: SSID and password
const char* WIFI_SSID = "FQQ";
const char* WIFI_PASSWORD = "1qaz2wsx";
// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_SERVER_IP = "192.168.88.141";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
//const PROGMEM char* MQTT_USER = "[Redacted]";
//const PROGMEM char* MQTT_PASSWORD = "[Redacted]";
// MQTT: topic
const PROGMEM char* MQTT_SENSOR_TOPIC = "living/esp8266_12f";
// sleeping time
const PROGMEM uint16_t SLEEPING_TIME_IN_SECONDS = 1200; // 20 minutes x 60 seconds
// DHT - D1/GPIO12
#define DHTPIN 12
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// function called to publish the temperature and the humidity
void publishData(float p_temperature, float p_humidity) {
// create a JSON object
// doc : https://github.com/bblanchon/ArduinoJson/wiki/API%20Reference
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
// INFO: the data must be converted into a string; a problem occurs when using floats...
root["temperature"] = (String)p_temperature;
root["humidity"] = (String)p_humidity;
root.prettyPrintTo(Serial);
Serial.println("");
/*
{
"temperature": "23.20" ,
"humidity": "43.70"
}
*/
char data[200];
root.printTo(data, root.measureLength() + 1);
client.publish(MQTT_SENSOR_TOPIC, data, true);
yield();
}
// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
}
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("INFO: Attempting MQTT connection...");
// Generate client name based on MAC address and last 8 bits of microsecond counter
String MQTT_CLIENT_ID;
MQTT_CLIENT_ID += "esp8266_12f-";
uint8_t mac[6];
WiFi.macAddress(mac);
MQTT_CLIENT_ID += macToStr(mac);
MQTT_CLIENT_ID += "-";
MQTT_CLIENT_ID += String(micros() & 0xff, 16);
Serial.print("Connecting to ");
Serial.print(MQTT_SERVER_IP);
Serial.print(" as ");
Serial.println(MQTT_CLIENT_ID);
// Attempt to connect
//if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
if (client.connect((char *) MQTT_CLIENT_ID.c_str())) {
Serial.println("INFO: connected");
} else {
Serial.print("ERROR: failed, rc=");
Serial.print(client.state());
Serial.println("DEBUG: try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// init the serial
Serial.begin(115200);
dht.begin();
// init the WiFi connection
Serial.println();
Serial.println();
Serial.print("INFO: Connecting to ");
WiFi.mode(WIFI_STA);
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("INFO: WiFi connected");
Serial.print("INFO: IP address: ");
Serial.println(WiFi.localIP());
// init the MQTT connection
client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("ERROR: Failed to read from DHT sensor!");
return;
} else {
//Serial.println(h);
//Serial.println(t);
publishData(t, h);
// 让指示灯亮一下,证明有数据传输
digitalWrite(LED_BUILTIN, LOW);
delay(200);
digitalWrite(LED_BUILTIN, HIGH);
}
Serial.println("INFO: Closing the MQTT connection");
client.disconnect();
Serial.println("INFO: Closing the Wifi connection");
WiFi.disconnect();
ESP.deepSleep(SLEEPING_TIME_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
delay(500); // wait for deep sleep to happen
}