Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot:examples:dht [2021/03/05 00:16] heiko.pikneren:iot:examples:dht [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
-====== DHT  example======+====== Temp/Hum example======
  
 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:
 <code>lib_deps = ITTIoT, DHT sensor library, Adafruit Unified Sensor</code> <code>lib_deps = ITTIoT, DHT sensor library, Adafruit Unified Sensor</code>
  
-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 #ff0000>After programming, if NAN appears instead of readings, the USB cable should be disconnected and reconnected!</fc>
 <code c> <code c>
-// Includes global variables and librarys that the Temperature & humidity shield uses+// Includes global variables and librarys that the DHT uses
 #include <Arduino.h> #include <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
 +#include <Ticker.h>
 #include <DHT.h> #include <DHT.h>
  
Line 23: Line 26:
 #define WIFI_PASSWORD "password" #define WIFI_PASSWORD "password"
  
-#define DHTPIN D4     // what pin we're connected to. Change this to D3 if your shield has one leg removed.+#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) #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("MQTT connected callback");   Serial.println("MQTT connected callback");
 +  // Send message to MQTT server to show that connection is established
   iot.log("IoT DHT example!");   iot.log("IoT DHT example!");
 } }
Line 38: Line 56:
 void setup() void setup()
 { {
 +  // Initialize serial port and send message
   Serial.begin(115200); // setting up serial connection parameter   Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
Line 43: Line 62:
   //iot.setConfig("wname", WIFI_NAME);   //iot.setConfig("wname", WIFI_NAME);
   //iot.setConfig("wpass", WIFI_PASSWORD);   //iot.setConfig("wpass", WIFI_PASSWORD);
-  iot.printConfig(); // print json config to serial+  iot.printConfig(); // print IoT json config to serial
   iot.setup(); // Initialize IoT library   iot.setup(); // Initialize IoT library
  
-  pinMode(16, OUTPUT); +  // Initialize DHT library 
-  dht.begin(); // activating temperature and humidity functions+  dht.begin(); 
 + 
 +  // Initialize Ticker interval and callback 
 +  timeTicker.attach(1, sendData);
 } }
  
Line 54: Line 76:
   iot.handle(); // IoT behind the plan work, it should be periodically called   iot.handle(); // IoT behind the plan work, it should be periodically called
  
-  float h = dht.readHumidity(); // reading humidity values +  if(sendDataFlag) 
-  float t = dht.readTemperature(); // reading temperature values+  { 
 +    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];
  
-  char buf[10]; // Put whatever length you need here, defining char variable +    // Convert temperature value messages to strings and send to the MQTT server 
-  String(t).toCharArray(buf,10); // converting temperature value into string +    String(t).toCharArray(buf,10); 
-  digitalWrite(16,HIGH); +    iot.publishMsg("temp",buf);
-  iot.publishMsg("temp",buf); // publishing temperature value to MQTT broker +
-  delay(2000); // wait 2 seconds+
  
-  digitalWrite(16,LOW); +    // Convert humidity value messages to strings and send to the MQTT server 
-  String(h).toCharArray(buf,10); // converting humidity value into string +    String(h).toCharArray(buf,10); 
-  iot.publishMsg("hum",buf); // publishing humidity value to MQTT broker +    iot.publishMsg("hum",buf); 
-  delay(2000); // wait 2 seconds+  }
 } }
  
 </code> </code>
en/iot/examples/dht.1614903362.txt.gz · Last modified: 2021/03/05 10:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0