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 [2021/03/05 12:06] heiko.pikneren:iot:examples:dhtoled [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
 +====== DHT and OLED example ======
  
 +This example demonstrates how DHT senor can send temperature and humidity readings to the OLED screen with the ITT IoT framework.
 +For this example you will need two controllers. One with the OLED shield and the other with the DHT sensor shield.
 +
 +{{:en:iot:examples:20200610_200205.jpg?300|DHT and OLED example}}
 +
 +Once the code has been uploaded the DHT controller will send temperature and humidity messages via MQTT to the OLED module. 
 +
 +To upload the code you need to create two projects in PlatformIO environment.
 +One for the controller with the OLED shield and the other for the DHT sensor shield.
 +
 +The following is the code for the controller with the DHT shield. Needed libaries:
 +<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>
 +// Includes global variables and librarys that the DHT uses
 +#include <Arduino.h>
 +#include <ittiot.h>
 +#include <Ticker.h>
 +#include <DHT.h>
 +
 +#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)
 +
 +// 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;
 +}
 +
 +// 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("MQTT connected callback");
 +  // Send message to MQTT server to show that connection is established
 +  iot.log("IoT DHT example!");
 +}
 +
 +void setup()
 +{
 +  // Initialize serial port and send message
 +  Serial.begin(115200); // setting up serial connection parameter
 +  Serial.println("Booting");
 +
 +  //iot.setConfig("wname", WIFI_NAME);
 +  //iot.setConfig("wpass", WIFI_PASSWORD);
 +  iot.printConfig(); // print IoT json config to serial
 +  iot.setup(); // Initialize IoT library
 +
 +  // Initialize DHT library
 +  dht.begin();
 +
 +  // Initialize Ticker interval and callback
 +  timeTicker.attach(1, sendData);
 +}
 +
 +void loop()
 +{
 +  iot.handle(); // IoT behind the plan work, it should be periodically called
 +
 +  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,10);
 +    iot.publishMsg("temp",buf);
 +
 +    // Convert humidity value messages to strings and send to the MQTT server
 +    String(h).toCharArray(buf,10);
 +    iot.publishMsg("hum",buf);
 +  }
 +}
 +
 +</code>
 +
 +The following is the code for the controller module with the OLED shield.
 +<code>lib_deps = ITTIoT, Adafruit GFX Library, Adafruit SSD1306 Wemos Mini OLED, adafruit/Adafruit BusIO</code>
 +<code c>
 +// Includes global variables and librarys that the OLED display uses
 +#include <Arduino.h>
 +#include <ittiot.h>
 +#include <Adafruit_I2CDevice.h>
 +#include <Adafruit_GFX.h>
 +#include <Adafruit_SSD1306.h>
 +
 +#define WIFI_NAME "name"
 +#define WIFI_PASSWORD "password"
 +
 +// Change it according to the real name of the microcontroller where DHT shield is connected
 +#define DHT_TOPIC "ESP30"
 +
 +// OLED reset pin is GPIO0
 +#define OLED_RESET 0
 +
 +// Create an object for OLED screen
 +Adafruit_SSD1306 display(OLED_RESET);
 +
 +// Define variables to store humidity and temperature values
 +float h;
 +float t;
 +
 +// Message received
 +void iot_received(String topic, String msg)
 +{
 +  // Check if topic contains temperature data
 +  if(topic == (DHT_TOPIC"/temp"))
 +  {
 +    t = msg.toFloat(); // Convert string to float
 +  }
 +
 +  // Check if topic contains humidity data
 +  if(topic == (DHT_TOPIC"/hum"))
 +  {
 +    h = msg.toFloat(); // Convert string to float
 +  }
 +}
 +
 +// 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("MQTT connected callback");
 +  // Subscribe to topics to get temperature and humidity messages
 +  iot.subscribe(DHT_TOPIC"/temp");
 +  iot.subscribe(DHT_TOPIC"/hum");
 +  // Send message to MQTT server to show that connection is established
 +  iot.log("IoT OLED example!");
 +}
 +
 +void setup()
 +{
 +  // Initialize serial port and send message
 +  Serial.begin(115200); // setting up serial connection parameter
 +  Serial.println("Booting");
 +
 +  // Initialize OLED with the I2C addr 0x3C (for the 64x48)
 +  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
 +
 +  // Display "booting..." message
 +  display.clearDisplay();
 +  display.setTextSize(1);
 +  display.setTextColor(WHITE);
 +  display.setCursor(0,0);
 +  display.println("Booting...");
 +  display.display();
 +
 +  //iot.setConfig("wname", WIFI_NAME);
 +  //iot.setConfig("wpass", WIFI_PASSWORD);
 +  iot.printConfig(); // print IoT json config to serial
 +  iot.setup(); // Initialize IoT library
 +}
 +
 +void loop()
 +{
 +  iot.handle();// IoT behind the plan work, it should be periodically called
 +
 +  // Display temperature and humidity readings to OLED screen
 +  display.clearDisplay(); // cleanse the OLED display
 +  display.setTextSize(1); // sets the text size
 +  display.setTextColor(WHITE); // sets the text color
 +  display.setCursor(0,0); // sets the cursor position
 +  display.println("Temp: "); // writes text
 +  display.println(t); // write temperature value
 +  display.setCursor(0,20);
 +  display.println("Hum: ");
 +  display.println(h);
 +  display.display();
 +
 +  delay(200); // Waiting 0.2 second
 +}
 +
 +</code>
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