This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot:examples:dht [2021/03/05 00:16] – heiko.pikner | en:iot:examples:dht [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
DHT module must be connected to controller module or with sensor module. | DHT module must be connected to controller module or with sensor module. | ||
Line 13: | Line 13: | ||
< | < | ||
- | The example code above will print out current Tempeture and Humidity in the room | + | The example code above will print out current Tempeture and Humidity in the room. |
+ | |||
+ | <fc # | ||
<code c> | <code c> | ||
- | // Includes global variables and librarys that the Temperature & humidity shield | + | // Includes global variables and librarys that the DHT uses |
#include < | #include < | ||
#include < | #include < | ||
+ | #include < | ||
#include < | #include < | ||
Line 23: | Line 26: | ||
#define WIFI_PASSWORD " | #define WIFI_PASSWORD " | ||
- | #define DHTPIN | + | #define DHTPIN |
#define DHTTYPE DHT22 // DHT 22 (AM2302) | #define DHTTYPE DHT22 // DHT 22 (AM2302) | ||
// Create an object for DHT sensor | // Create an object for DHT sensor | ||
DHT dht(DHTPIN, DHTTYPE); | DHT dht(DHTPIN, DHTTYPE); | ||
+ | |||
+ | // Create an object for Ticker library | ||
+ | Ticker timeTicker; | ||
+ | |||
+ | bool sendDataFlag; | ||
+ | |||
+ | // Ticker library callback, which will occur 0.5 second interval. | ||
+ | void sendData() | ||
+ | { | ||
+ | sendDataFlag=true; | ||
+ | } | ||
+ | |||
+ | void iot_received(String topic, String msg) {} | ||
// Function started after the connection to the server is established. | // Function started after the connection to the server is established. | ||
void iot_connected() | void iot_connected() | ||
{ | { | ||
+ | // Send message to serial port to show that connection is established | ||
Serial.println(" | Serial.println(" | ||
+ | // Send message to MQTT server to show that connection is established | ||
iot.log(" | iot.log(" | ||
} | } | ||
Line 38: | Line 56: | ||
void setup() | void setup() | ||
{ | { | ||
+ | // Initialize serial port and send message | ||
Serial.begin(115200); | Serial.begin(115200); | ||
Serial.println(" | Serial.println(" | ||
Line 43: | Line 62: | ||
// | // | ||
// | // | ||
- | iot.printConfig(); | + | iot.printConfig(); |
iot.setup(); | iot.setup(); | ||
- | | + | |
- | dht.begin(); | + | dht.begin(); |
+ | |||
+ | | ||
+ | timeTicker.attach(1, | ||
} | } | ||
Line 54: | Line 76: | ||
iot.handle(); | iot.handle(); | ||
- | float h = dht.readHumidity(); | + | |
- | float t = dht.readTemperature(); | + | { |
+ | sendDataFlag = false; | ||
+ | // Read humidity and temperature | ||
+ | | ||
+ | float t = dht.readTemperature(); | ||
+ | |||
+ | | ||
+ | char buf[10]; | ||
- | char buf[10]; | + | |
- | String(t).toCharArray(buf, | + | String(t).toCharArray(buf, |
- | digitalWrite(16, | + | iot.publishMsg(" |
- | iot.publishMsg(" | + | |
- | delay(2000); | + | |
- | digitalWrite(16, | + | // Convert humidity value messages to strings and send to the MQTT server |
- | String(h).toCharArray(buf, | + | String(h).toCharArray(buf, |
- | iot.publishMsg(" | + | iot.publishMsg(" |
- | | + | |
} | } | ||
</ | </ |