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 [2020/07/20 11:26] – external edit 127.0.0.1en: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 11: Line 11:
  
 Needed libraries: Needed libraries:
-<code>lib_deps = ITTIoT@1.0.5, 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
-<code c>+
  
 +<fc #ff0000>After programming, if NAN appears instead of readings, the USB cable should be disconnected and reconnected!</fc>
 +<code c>
 +// 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>
-#define DHTPIN D4     // what pin we're connected to. Change this to D3 if your shield has one leg removed.+ 
 +#define WIFI_NAME "name" 
 +#define WIFI_PASSWORD "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) #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("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 33: Line 56:
 void setup() void setup()
 { {
-  Serial.begin(115200);+  // Initialize serial port and send message 
 +  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
-  iot.printConfig(); // print json config to serial //Peale Serial.begin ja enne iot.setup + 
-  iot.setup(); +  //iot.setConfig("wname", WIFI_NAME); 
-  pinMode(16, OUTPUT);+  //iot.setConfig("wpass", WIFI_PASSWORD); 
 +  iot.printConfig(); // print IoT json config to serial 
 +  iot.setup(); // Initialize IoT library 
 + 
 +  // Initialize DHT library
   dht.begin();   dht.begin();
  
 +  // Initialize Ticker interval and callback
 +  timeTicker.attach(1, sendData);
 } }
  
 void loop() void loop()
 { {
-  iot.handle();+  iot.handle(); // IoT behind the plan work, it should be periodically called
  
-  float h = dht.readHumidity(); +  if(sendDataFlag) 
- float t = dht.readTemperature();+  { 
 +    sendDataFlag = false; 
 +    // Read humidity and temperature 
 +    float h = dht.readHumidity(); 
 +    float t = dht.readTemperature();
  
-  char buf[10]; // Put whatever length you need here +    // Create a buffer to store strings to being sent later 
- String(t).toCharArray(buf,10);+    char buf[10];
  
-  digitalWrite(16,HIGH); +    // Convert temperature value messages to strings and send to the MQTT server 
-  iot.publishMsg("temp",buf); +    String(t).toCharArray(buf,10); 
- delay(2000); +    iot.publishMsg("temp",buf);
-  digitalWrite(16,LOW);+
  
- String(h).toCharArray(buf,10); +    // Convert humidity value messages to strings and send to the MQTT server 
-  iot.publishMsg("hum",buf); +    String(h).toCharArray(buf,10); 
- delay(2000);+    iot.publishMsg("hum",buf); 
 +  }
 } }
- 
  
 </code> </code>
en/iot/examples/dht.1595244384.txt.gz · Last modified: 2020/07/20 09: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