Ydle - version 0.5

Node Capteur DHT22 et relai

Ce node permet de mesurer la température et l'humidité à l'aide d'une sonde DHT22 et suivant le taux d'humidité il permet d'activer par l'intermédiaire d'un relay un extracteur/ventilateur.

Exemple de sketch: node_humidity_relay_ydle_dht22.zip

/* Author : Fabrice Scheider AKA Denia
* Modifications: Xylerk, EricDele
* Description : Node envoyant humidité et température en utilisant le protocole radio ydle.
*               Le relai permet de piloter un extracteur en fonction du taux d'humidité
* Note : J'utilise dans cette librairie un DHT22, il est donc nécessaire que vous l'ayez dans
* votre répertoire librairies pour que le sketch compile.
* Licence : CC-BY-SA
*/
 
#include <TimerOne.h>
#include "Ydle.h"
#include "DHT.h"
 
#define RX_PIN 12
#define TX_PIN 10
#define BT_PIN 3
 
#define DELAY_SEND 15000
 
#define DHTPIN 2     // what pin DHT22 we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
 
const int RELAY = 4; // Relay pin setup
const int DELAY = 1000; // Delay between sending a frame
boolean relayStatus = 0; // Relay status
int humidityThreshold = 70; // Humidity threshold for switching relay
unsigned long last_send, cur_time;  // Following the time
 
ydle y(RX_PIN, TX_PIN, BT_PIN);
 
DHT dht(DHTPIN, DHTTYPE);
 
void setup()
{
  Serial.begin(9600);
  Serial.println("DHTxx test!");
  // Initialisation of the Read Interrupt
  y.init_timer();
  // Set RELAY pin in out mode
  pinMode(RELAY, OUTPUT); 
  // Begin with a switch off
  digitalWrite(RELAY,1); 
  // Init DHT Sensor
  dht.begin();     
  // Set the first time
  cur_time = millis();
  last_send = cur_time;  
}
 
void loop()
{
  // Read a frame
  y.receive();
  // Test if the Node was Initialized
  if(y.initialized())
  {
    // Store current time
    cur_time = millis();
    // Test if the delay between the last send is ok
    if(cur_time - last_send >= DELAY_SEND)
    {
      // Store the current time as last send time
      last_send = cur_time;
 
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h = dht.readHumidity();
      float t = dht.readTemperature();
 
      // check if returns are valid, if they are NaN (not a number) then something went wrong!
      if (isnan(t) || isnan(h))
      {
        Serial.println("Failed to read from DHT");
      }
      else
      {
        // Make the frame
        Frame_t frame;
        // Set the frame with a ACKnowledge
        y.dataToFrame(&frame, YDLE_TYPE_STATE_ACK);
        // Add in the frame the humidity
        y.addData(&frame, YDLE_DATA_HUMIDITY, 40*h);
        Serial.println(" -- envoie de la trame radio contenant l'humiditée");
        // Send the frame
        y.send(&frame);  
        // Wait a Delay to avoid crc error      
        delay (DELAY);
        // Do the same for the temperature
        y.dataToFrame(&frame, YDLE_TYPE_STATE_ACK);
        y.addData(&frame, YDLE_DATA_DEGREEC, 20*t);
        Serial.println(" -- envoie de la trame radio contenant la température");
        y.send(&frame);       
        delay (DELAY);
        Serial.print("Humidity: ");
        Serial.print(h);
        Serial.print(" %\t");
        Serial.print("Temperature: ");
        Serial.print(t);
        Serial.println(" *C");
        // Test what we need to do with the relay
        if ( h < humidityThreshold && relayStatus != 1) // Below the limit and relay is working
        {
          relayStatus = 1;
          // Switch off relay
          digitalWrite(RELAY,relayStatus); 
          // Send the frame to inform the Master of a relay switch off
          y.dataToFrame(&frame, YDLE_TYPE_STATE_ACK);
          y.addData(&frame, YDLE_DATA_STATE, 0);
          y.send(&frame);
          Serial.println("Trame radio RELAI envoyée => OFF");
        }
        else if ( h > humidityThreshold && relayStatus != 0 ) // Upside the limit and relay is not working
        {
          relayStatus = 0;
          // Switch on relay
          digitalWrite(RELAY,relayStatus); 
          // Send the frame to inform the Master of a relay switch on
          y.dataToFrame(&frame, YDLE_TYPE_STATE_ACK);
          y.addData(&frame, YDLE_DATA_STATE, 1);
          y.send(&frame);          
          Serial.println("Trame radio RELAI envoyée => ON");
        }
        Serial.print("relayStatus : ");
        if ( relayStatus ) // Test relay status
        {
          Serial.println("OFF"); // show the state
        }
        else
        {
          Serial.println("ON"); // show the state
        }
      }
    }  
  }
  else
  {
    // Node not initialized
    Serial.println("Pas initialisé");
  }
}

Autres langues
Traductions de cette page:
QR Code
QR Code Node Capteur DHT22 et relai (generated for current page)