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:dhtoled [2020/07/20 11:26] – external edit 127.0.0.1en:iot:examples:dhtoled [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 12: Line 12:
  
 The following is the code for the controller with the DHT shield. Needed libaries: The following is the code for the controller with the DHT shield. Needed libaries:
-<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
 +<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 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 D3     // Pin where DHT shield is connected. Change this to D4 if your shield has no legs 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)
  
Line 24: Line 30:
 DHT dht(DHTPIN, DHTTYPE); DHT dht(DHTPIN, DHTTYPE);
  
-// Message received +// Create an object for Ticker library 
-void iot_received(String topic, String msg) +Ticker timeTicker;
-{+
  
 +bool sendDataFlag;
 +
 +// Ticker library callback, which will occur 0.5 second interval.
 +void sendData()
 +{
 +  sendDataFlag=true;
 } }
  
Line 42: Line 53:
 { {
   // Initialize serial port and send message   // Initialize serial port and send message
-  Serial.begin(115200);+  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
-  // print IoT json config to serial +  //iot.setConfig("wname", WIFI_NAME); 
-  iot.printConfig(); +  //iot.setConfig("wpass", WIFI_PASSWORD); 
- +  iot.printConfig(); // print IoT json config to serial 
-  // Initialize IoT library +  iot.setup(); // Initialize IoT library
-  iot.setup();+
  
   // Initialize DHT library   // Initialize DHT library
   dht.begin();   dht.begin();
 +
 +  // Initialize Ticker interval and callback
 +  timeTicker.attach(1, sendData);
 } }
  
 void loop() void loop()
 { {
-  // IoT behind the plan work, it should be periodically called +  iot.handle(); // IoT behind the plan work, it should be periodically called
-  iot.handle();+
  
-  // Read humidity and temperature +  if(sendDataFlag) 
-  float h = dht.readHumidity(); +  { 
- float t = dht.readTemperature();+    sendDataFlag = false; 
 +    // Read humidity and temperature 
 +    float h = dht.readHumidity(); 
 +    float t = dht.readTemperature();
  
-  // Create a buffer to store strings to being sent later +    // Create a buffer to store strings to being sent later 
-  char buf[10];+    char buf[10];
  
-  // Convert messages to strings and send to the MQTT server +    // Convert temperature value messages to strings and send to the MQTT server 
- String(t).toCharArray(buf,10); +    String(t).toCharArray(buf,10); 
-  iot.publishMsg("temp",buf); +    iot.publishMsg("temp",buf);
- delay(1000);+
  
- 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(1000);+    iot.publishMsg("hum",buf); 
 +  }
 } }
  
Line 80: Line 95:
  
 The following is the code for the controller module with the OLED shield. The following is the code for the controller module with the OLED shield.
-<code>lib_deps = ITTIoT@1.0.5, MFRC522, Adafruit GFX Library, Adafruit SSD1306 Wemos Mini OLED</code>+<code>lib_deps = ITTIoT, Adafruit GFX Library, Adafruit SSD1306 Wemos Mini OLED, adafruit/Adafruit BusIO</code>
 <code c> <code c>
 +// Includes global variables and librarys that the OLED display uses
 #include <Arduino.h> #include <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
-#include <SPI.h> +#include <Adafruit_I2CDevice.h>
-#include <Wire.h>+
 #include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
 #include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
  
-// Change it according to the real name of the red IoT module where +#define WIFI_NAME "name" 
-// DHT shield is connected +#define WIFI_PASSWORD "password" 
-#define DHT_TOPIC "ESP53"+ 
 +// Change it according to the real name of the microcontroller where DHT shield is connected 
 +#define DHT_TOPIC "ESP30"
  
 // OLED reset pin is GPIO0 // OLED reset pin is GPIO0
Line 109: Line 126:
   if(topic == (DHT_TOPIC"/temp"))   if(topic == (DHT_TOPIC"/temp"))
   {   {
-    // Convert string to float +    t = msg.toFloat(); // Convert string to float
-    t = msg.toFloat();+
   }   }
  
Line 116: Line 132:
   if(topic == (DHT_TOPIC"/hum"))   if(topic == (DHT_TOPIC"/hum"))
   {   {
-    // Convert string to float +    h = msg.toFloat(); // Convert string to float
-    h = msg.toFloat();+
   }   }
 } }
Line 126: Line 141:
   // Send message to serial port to show that connection is established   // Send message to serial port to show that connection is established
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
-  // Subscribe to get temperature and humidity messages+  // Subscribe to topics to get temperature and humidity messages
   iot.subscribe(DHT_TOPIC"/temp");   iot.subscribe(DHT_TOPIC"/temp");
   iot.subscribe(DHT_TOPIC"/hum");   iot.subscribe(DHT_TOPIC"/hum");
Line 136: Line 151:
 { {
   // Initialize serial port and send message   // Initialize serial port and send message
-  Serial.begin(115200);+  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
Line 150: Line 165:
   display.display();   display.display();
  
-  // print IoT json config to serial +  //iot.setConfig("wname", WIFI_NAME); 
-  iot.printConfig(); +  //iot.setConfig("wpass", WIFI_PASSWORD); 
- +  iot.printConfig(); // print IoT json config to serial 
-  // Initialize IoT library +  iot.setup(); // Initialize IoT library
-  iot.setup();+
 } }
  
 void loop() void loop()
 { {
-  // IoT behind the plan work, it should be periodically called +  iot.handle();// IoT behind the plan work, it should be periodically called
-  iot.handle();+
  
-  // Display temperature and humidity readings to oLED screen +  // Display temperature and humidity readings to OLED screen 
-  display.clearDisplay(); +  display.clearDisplay(); // cleanse the OLED display 
-  display.setTextSize(1); +  display.setTextSize(1); // sets the text size 
-  display.setTextColor(WHITE); +  display.setTextColor(WHITE); // sets the text color 
-  display.setCursor(0,0); +  display.setCursor(0,0); // sets the cursor position 
-  display.println("Temp: "); +  display.println("Temp: "); // writes text 
-  display.println(t);+  display.println(t); // write temperature value
   display.setCursor(0,20);   display.setCursor(0,20);
   display.println("Hum: ");   display.println("Hum: ");
Line 174: Line 187:
   display.display();   display.display();
  
-  delay(200);+  delay(200); // Waiting 0.2 second
 } }
 +
 </code> </code>
en/iot/examples/dhtoled.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