This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| et:iot:examples:dht [2024/07/04 16:09] – rojarl | et:iot:examples:dht [2024/11/28 10:42] (current) – karllall | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Temp ja niiskuse näidis ====== | ||
| + | DHT moodul peab olema ühendatud kontrolleri või anduri mooduliga. | ||
| + | |||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | NB! Kui Teie DHT moodulil on üks jalg eemaldatud nagu alloleval pildil, tuleb koodis muuta ümber #define DHTPIN D4 D3-ks. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Vajaminevad teegid: | ||
| + | < | ||
| + | |||
| + | Näidiskood prindib välja antud hetke temperatuuri ja niiskuse väärtused. | ||
| + | |||
| + | <fc # | ||
| + | <code c> | ||
| + | // Includes global variables and librarys that the DHT uses | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | #define DHTPIN D3 // Pin where DHT shield is connected. Change this to D4 if the shield has no legs removed. | ||
| + | #define DHTTYPE DHT22 // DHT 22 (AM2302) | ||
| + | |||
| + | // Create an object for DHT sensor | ||
| + | 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() | ||
| + | { | ||
| + | // Send message to serial port to show that connection is established | ||
| + | Serial.println(" | ||
| + | // Send message to MQTT server to show that connection is established | ||
| + | iot.log(" | ||
| + | } | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | // Initialize serial port and send message | ||
| + | Serial.begin(115200); | ||
| + | Serial.println(" | ||
| + | |||
| + | // | ||
| + | // | ||
| + | iot.printConfig(); | ||
| + | iot.setup(); | ||
| + | |||
| + | // Initialize DHT library | ||
| + | dht.begin(); | ||
| + | |||
| + | // Initialize Ticker interval and callback | ||
| + | timeTicker.attach(1, | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | iot.handle(); | ||
| + | |||
| + | if(sendDataFlag) | ||
| + | { | ||
| + | sendDataFlag = false; | ||
| + | // Read humidity and temperature | ||
| + | float h = dht.readHumidity(); | ||
| + | float t = dht.readTemperature(); | ||
| + | |||
| + | // Create a buffer to store strings to being sent later | ||
| + | char buf[10]; | ||
| + | |||
| + | // Convert temperature value messages to strings and send to the MQTT server | ||
| + | String(t).toCharArray(buf, | ||
| + | iot.publishMsg(" | ||
| + | |||
| + | // Convert humidity value messages to strings and send to the MQTT server | ||
| + | String(h).toCharArray(buf, | ||
| + | iot.publishMsg(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||