-
Notifications
You must be signed in to change notification settings - Fork 1
/
dht22tomqtt.ino
129 lines (106 loc) · 3.23 KB
/
dht22tomqtt.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
/*
* This sketch is bassed upon the following two examples. Thingsboard showed how to connect a DHT22 via an ESP8266 to an MQTT broker:
* https://thingsboard.io/docs/samples/esp8266/temperature/
* Losant showed how to enable the deep sleep mode:
* https://www.losant.com/blog/making-the-esp8266-low-powered-with-deep-sleep
*/
#include "DHT.h"
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include "configvariables.h"
// DHT
#define DHTPIN 4
#define DHTTYPE DHT22
char thingsboardServer[] = MQTT_BROKER;
WiFiClient wifiClient;
IPAddress ip; // the IP address of your ESP8266
// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
PubSubClient client(wifiClient);
int status = WL_IDLE_STATUS;
void setup()
{
Serial.begin(9600);
dht.begin();
delay(10);
client.setServer( thingsboardServer, 1883 );
if ( !client.connected() ) {
reconnect();
}
getAndSendTemperatureAndHumidityData();
client.loop();
client.disconnect();
Serial.println("Sleep for 60 seconds");
ESP.deepSleep(60e6); // 60e6 is 60 seconds in microseconds
}
void loop()
{
}
void getAndSendTemperatureAndHumidityData()
{
Serial.println("Collecting temperature data.");
// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
String temperature = String(t);
String humidity = String(h);
String heat_index = String(hic);
// Just debug messages
Serial.print( "Sending temperature and humidity : [" );
Serial.print( temperature ); Serial.print( "," );
Serial.print( humidity );
Serial.print( "] -> " );
// Prepare a JSON payload string
String payload = "{";
payload += "\"temperature\":"; payload += temperature; payload += ",";
payload += "\"humidity\":"; payload += humidity; payload += ",";
payload += "\"heat_index\":"; payload += heat_index;
payload += "}";
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
client.publish( MQTT_TOPIC, attributes );
Serial.println( attributes );
}
void InitWiFi()
{
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
WiFi.begin(WIFI_AP, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
Serial.println("Got IP");
ip = WiFi.localIP();
Serial.println(ip);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
status = WiFi.status();
if ( status != WL_CONNECTED) {
InitWiFi();
}
Serial.print("Connecting to MQTT Broker ...");
// Attempt to connect (clientId, username, password)
if ( client.connect(MQTT_CLIENT_ID, MQTT_TOKEN, NULL) ) {
Serial.println( "[DONE]" );
} else {
Serial.print( "[FAILED] [ rc = " );
Serial.print( client.state() );
Serial.println( " : retrying in 5 seconds]" );
// Wait 5 seconds before retrying
delay( 5000 );
}
}
}