Targa ventilaatori näidis

Antud näidiskood demonstreerib kuidas luua temperatuurianduri poolt juhitud ventilaatorit. Selle jaoks läheb vaja kaht kontrollerit. Ühega ühendame DHT ja teisega relee.

IoT controllers with DHT and relay shields

Kuid kood on üles laetud, saadab DHT kontroller iga kahe sekundi tagant temperatuuri ja niiskuse väärtused “temp” ja “hum”. Relee moodul kuulab “temp” ja “conf” teemasid. Relee kontroller lülitab relee sisse kui temperatuur tõuseb üle ettemääratud piirmäära (vaikimisi 28 kraadi). Piirmäära muutmiseks tuleb muuta “conf” väärtust (näiteks “25.5”)

Järgnev kood on reeleega kontrolleri jaoks. Vajaminevad teegid>

lib_deps = ITTIoT
// Includes global variables and librarys that the relay uses
#include <Arduino.h>
#include <ittiot.h>
 
#define WIFI_NAME "name"
#define WIFI_PASSWORD "password"
 
// Change it according to the real name of the red IoT module where
// temperature & humidity shield is connected
#define DHT_TOPIC "ESP30"
 
#define RELAY_PIN 5 // The relay has been connected to pin 5
float t; //for holding the received temperature float value
float tempLimit = 28; //the temperature limit on what the relay will switch
 
// Message received
void iot_received(String topic, String msg)
{
  t=msg.toFloat(); // Converts received temperature value into a real number
 
  // Check if topic contains configuration data (change the temperature limit value)
  if(topic == (DHT_TOPIC"/conf"))
  {
    //re-configures the temperature limit
    tempLimit=t;
  }
 
  // Check if topic contains temperature data
  if(topic == (DHT_TOPIC"/temp"))
  {
    // Relay switching according to the temperature limit value
    if(t >= tempLimit)
    {
      digitalWrite(RELAY_PIN, HIGH);
    }
    else
    {
      digitalWrite(RELAY_PIN, LOW);
    }
  }
}
 
// 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 temperature configuration messages
  iot.subscribe(DHT_TOPIC"/temp");
  iot.subscribe(DHT_TOPIC"/conf");
  // Send message to MQTT server to show that connection is established
  iot.log("Ventilator 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
 
  pinMode(RELAY_PIN, OUTPUT); // The relay pin is defined as output type
}
 
void loop()
{
  iot.handle(); // IoT behind the plan work, it should be periodically called
  delay(20); // Wait 0.2 second
}

Järgnev kood on DHT mooduli jaoks. Vajaminevad teegid:

lib_deps = ITTIoT, DHT sensor library, Adafruit Unified Sensor

After programming, if NAN appears instead of readings, the USB cable should be disconnected and reconnected!

// 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);
  }
}
et/iot/examples/smartventilator.txt · Last modified: 2023/01/29 10:00 by 127.0.0.1
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