畑定点観測ロガー(3) – 露点の取得

前回の 畑定点観測ロガー(2) – 気温と湿度の取得 で取得した気温と相対湿度から露点を計算によって求めます。
そのため、プログラムのみの変更で、回路等は 畑定点観測ロガー(2) – 気温と湿度の取得 と変わりありません。


ソースコード
dht22.h

//
//    FILE: dht22.h
// VERSION: 1.0.1.0
// PURPOSE: DHT22 library for Arduino
//          (Temperature & Humidity Sensor)
//
//     URL: http://mwlab.net/2012/05/arduino_logger_03.html
//
// HISTORY:
// see dht.cpp file
//

#ifndef dht22_h
#define dht22_h

#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif

#define DHT22_LIB_VERSION "1.0.1.0"

#define DHT22_OK               0
#define DHT22_ERROR_CHECKSUM   -1
#define DHT22_ERROR_TIMEOUT    -2

class DHT22
{
    public:
        int get(uint8_t pin);
        double humidity;
        double temperature;
        double dewpoint;

    private:
        uint8_t bits[5];  // buffer to receive data
        int read(uint8_t pin);
};
#endif

dht22.cpp

//
//    FILE: dht22.cpp
// VERSION: 1.0.1.0
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
//
// DATASHEET:
//   http://pub.idisk-just.com/fview/Ofo5BntkKx3FZmmLkRMOsMRNS439HU6_QbdmJ9kpFT_svST6HEDy-obI8avf2qKAantH3a90pVj1c27ziNZVvWiyI-AvRLFc.pdf
//
// HISTORY:
// 1.0.1.0 by Ugetsu Yobane (2012/10/10)
//   Add calculate Dewpoint
// 1.0.0.0 by Ugetsu Yobane (2012/05/20)
// rebuild by DHT library
//   http://arduino.cc/playground/Main/DHTLib
//

#include "dht22.h"

#define TIMEOUTCOUNT 10000

/////////////////////////////////////////////////////
//
// PUBLIC
//

// return values:
// DHT22_OK
// DHT22_ERROR_CHECKSUM
// DHT22_ERROR_TIMEOUT
int DHT22::get(uint8_t pin)
{
        // Read Values
        int rv = read(pin);
        if (rv != 0) return rv;
        
        // Test Checksum
        uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
        if (bits[4] != sum) return DHT22_ERROR_CHECKSUM;

        // Convert and Store
        humidity    = word(bits[0], bits[1]) * 0.1;

        int sign = 1;
        if (bits[2] & 0x80) // negative temperature
        {
                bits[2] = bits[2] & 0x7F;
                sign = -1;
        }
        temperature = sign * word(bits[2], bits[3]) * 0.1;

        // Calculate Dew point
        float k;
        k = log(humidity/100) + (17.62 * temperature) / (243.12 + temperature);
        dewpoint = 243.12 * k / (17.62 - k);

        return DHT22_OK;
}

/////////////////////////////////////////////////////
//
// PRIVATE
//

// return values:
// DHT22_OK
// DHT22_ERROR_TIMEOUT
int DHT22::read(uint8_t pin)
{
        // Init Buffervar to Receive Data
        uint8_t cnt = 7;
        uint8_t idx = 0;

        // Empty Buffer
        bits[0] = bits[1] = bits[2] = bits[3] = bits[4] = 0;

        // Start Singnal
        pinMode(pin, OUTPUT);
        digitalWrite(pin, LOW);
        delayMicroseconds(1500); // 1ms < x
        digitalWrite(pin, HIGH);
        delayMicroseconds(40); // 20us < x < 40us
        pinMode(pin, INPUT);

        // Get Acknowledge or Timeout
        unsigned int loopCnt = TIMEOUTCOUNT;
        while(digitalRead(pin) == LOW)
                if (loopCnt-- == 0) return DHT22_ERROR_TIMEOUT;

        loopCnt = TIMEOUTCOUNT;
        while(digitalRead(pin) == HIGH)
                if (loopCnt-- == 0) return DHT22_ERROR_TIMEOUT;

        // Read the Output - 40 bits => 5 bytes
        for (int i=0; i<40; i++)
        {
                loopCnt = TIMEOUTCOUNT;
                while(digitalRead(pin) == LOW)
                        if (loopCnt-- == 0) return DHT22_ERROR_TIMEOUT;

                unsigned long t = micros();

                loopCnt = TIMEOUTCOUNT;
                while(digitalRead(pin) == HIGH)
                        if (loopCnt-- == 0) return DHT22_ERROR_TIMEOUT;

                if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
                if (cnt == 0)   // next byte?
                {
                        cnt = 7;  
                        idx++;      
                }
                else cnt--;
        }

        return DHT22_OK;
}
&#91;/c&#93;

DHT22Tester.ino
&#91;c&#93;
//
//    FILE: DHT22Tester.ino
// PURPOSE: DHT22 library test sketch for Arduino
//

#include <dht22.h>

DHT22 DHT;

#define DHT22_PIN 2

void setup()
{
  Serial.begin(115200);
  Serial.print("DHT22 LIBRARY VERSION: ");
  Serial.println(DHT22_LIB_VERSION);
  Serial.println();
  Serial.println("State,\tTemperature (C),\tHumidity (%),\tDewpoint (C)");
  delay(2000); // Waits until a sensor is stabilized. 
}

void loop()
{
  // Read Data
  int chk = DHT.get(DHT22_PIN);
  switch (chk)
  {
  case DHT22_OK: 
    Serial.print("OK,\t");

    // Display Data
    Serial.print(DHT.temperature, 1);
    Serial.print(",\t");
    Serial.print(DHT.humidity, 1);
    Serial.print(",\t");
    Serial.println(DHT.dewpoint, 1);
    break;
  case DHT22_ERROR_CHECKSUM:
    Serial.println("Checksum error,\t");
    break;
  case DHT22_ERROR_TIMEOUT:
    Serial.println("Time out error,\t");
    break;
  default:
    Serial.println("Unknown error,\t");
    break;
  }
  delay(5000);
}

[wpdm_file id=2]

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です