DHT ja OLED näidis

Antud näidis demonstreerib kuidas DHT andur on võimeline saatma temperatuuri ja niiskuse näidu OLED ekraanile kasutades selle jaoks ITT IoT raamistikku. Selle näite jaoks on vaja kahte kontrollerit: üht OLED-i ja teist DHT anduri jaoks.

DHT and OLED example

Kui kood on üles laetud, saadab DHT kontroller temperatuuri ja niiskuse näidu läbi mqtt OLED ekraanile.

Kõigepealt tuleb üks moodul tööle saada ning siis jätame selle mooduli tööle, kustutame arvutist selle mooduli koodi ära (soovi korral säilitame word-i vms dokumendis) ning samasse kohta loome teise mooduli koodi.

Järgnev kood on mõeldud sellele kontrollerile, mis on ühendatud DHT anduriga. Vajaminevad teegid:

lib_deps = ITTIoT, DHT sensor library, Adafruit Unified Sensor

Kui pärast programeerimist ilmub andmete asemel ekraanile NAN, tuleb USB kaabel lahti ühendada ning uuesti ühendada!

// 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);
  }
}

Järgnev kood on mõeldud kontrollerile, mis on ühendatud OLED ekraaniga. Vajaminevad teegid:

lib_deps = ITTIoT, Adafruit GFX Library, Adafruit SSD1306 Wemos Mini OLED, adafruit/Adafruit BusIO
// 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
}
et/iot/examples/dhtoled.txt · Last modified: 2024/12/12 07:29 by karllall
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