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.

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:

lib_deps = ITTIoT@1.0.5, DHT sensor library, Adafruit Unified Sensor
#include <Arduino.h>
#include <ittiot.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 DHTTYPE DHT22   // DHT 22  (AM2302)
 
// Create an object for DHT sensor
DHT dht(DHTPIN, DHTTYPE);
 
// Message received
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("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);
  Serial.println("Booting");
 
  // print IoT json config to serial
  iot.printConfig();
 
  // Initialize IoT library
  iot.setup();
 
  // Initialize DHT library
  dht.begin();
}
 
void loop()
{
  // IoT behind the plan work, it should be periodically called
  iot.handle();
 
  // 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 messages to strings and send to the MQTT server
	String(t).toCharArray(buf,10);
  iot.publishMsg("temp",buf);
	delay(1000);
 
	String(h).toCharArray(buf,10);
  iot.publishMsg("hum",buf);
	delay(1000);
}

The following is the code for the controller module with the OLED shield.

lib_deps = ITTIoT@1.0.5, Adafruit GFX Library, Adafruit SSD1306 Wemos Mini OLED, adafruit/Adafruit BusIO
#include <Arduino.h>
#include <ittiot.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
// Change it according to the real name of the red IoT module where
// DHT shield is connected
#define DHT_TOPIC "ESP53"
 
// 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"))
  {
    // Convert string to float
    t = msg.toFloat();
  }
 
  // Check if topic contains humidity data
  if(topic == (DHT_TOPIC"/hum"))
  {
    // Convert string to float
    h = msg.toFloat();
  }
}
 
// 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 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);
  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();
 
  // print IoT json config to serial
  iot.printConfig();
 
  // Initialize IoT library
  iot.setup();
}
 
void loop()
{
  // IoT behind the plan work, it should be periodically called
  iot.handle();
 
  // Display temperature and humidity readings to oLED screen
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Temp: ");
  display.println(t);
  display.setCursor(0,20);
  display.println("Hum: ");
  display.println(h);
  display.display();
 
  delay(200);
}
en/iot/examples/dhtoled.txt · Last modified: 2020/11/18 21:39 by heikopikner
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