This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot:examples:dht [2018/02/27 14:18] – Somepub | 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 5: | Line 5: | ||
| {{: | {{: | ||
| {{: | {{: | ||
| + | |||
| + | NB! If your DHT shield has one leg removed as in the picture below, please change the #define DHTPIN D4 to D3 in the program code. | ||
| + | |||
| + | {{: | ||
| Needed libraries: | Needed libraries: | ||
| - | < | + | < |
| - | 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. |
| - | <code c> | + | |
| + | <fc # | ||
| + | <code c> | ||
| + | // Includes global variables and librarys that the DHT uses | ||
| #include < | #include < | ||
| #include < | #include < | ||
| + | #include < | ||
| #include < | #include < | ||
| - | #define DHTPIN | + | |
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | #define DHTPIN | ||
| #define DHTTYPE DHT22 // DHT 22 (AM2302) | #define DHTTYPE DHT22 // DHT 22 (AM2302) | ||
| + | // 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. | ||
| 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 29: | Line 56: | ||
| void setup() | void setup() | ||
| { | { | ||
| - | Serial.begin(115200); | + | |
| + | | ||
| Serial.println(" | Serial.println(" | ||
| - | | + | |
| - | iot.setup(); | + | // |
| - | | + | // |
| + | | ||
| + | iot.setup(); | ||
| + | |||
| + | | ||
| dht.begin(); | dht.begin(); | ||
| + | // Initialize Ticker interval and callback | ||
| + | timeTicker.attach(1, | ||
| } | } | ||
| void loop() | void loop() | ||
| { | { | ||
| - | iot.handle(); | + | iot.handle(); |
| - | float h = dht.readHumidity(); | + | |
| - | float t = dht.readTemperature(); | + | { |
| + | sendDataFlag = false; | ||
| + | // Read humidity and temperature | ||
| + | | ||
| + | float t = dht.readTemperature(); | ||
| - | | + | // Create a buffer to store strings to being sent later |
| - | String(t).toCharArray(buf, | + | |
| - | digitalWrite(16,HIGH); | + | // Convert temperature value messages to strings and send to the MQTT server |
| - | iot.publishMsg(" | + | String(t).toCharArray(buf,10); |
| - | delay(2000); | + | iot.publishMsg(" |
| - | digitalWrite(16, | + | |
| - | String(h).toCharArray(buf, | + | // Convert humidity value messages to strings and send to the MQTT server |
| - | iot.publishMsg(" | + | |
| - | delay(2000); | + | iot.publishMsg(" |
| + | } | ||
| } | } | ||
| - | |||
| </ | </ | ||