Wio LTE for Arduino에서 온습도 센서 사용

Wio LTE for Arduino에서 온습도 센서(Grove Temperature & Humidity Sensor Pro)를 사용하는 방법입니다.
여기에있는 프로그램을 사용했습니다.
DHT22

Ubuntu 21.04에 설치한 Arduino 1.8.15에서 동작을 확인했습니다.
센서가 D38에 연결되었습니다.

프로그램

temperature_humidity/temperature_humidity.ino
// ---------------------------------------------------------------
/*
    temperature_humidity.ino

                    Jul/27/2021
*/
// ---------------------------------------------------------------
#include <WioLTEforArduino.h>

#define SENSOR_PIN    (WIOLTE_D38)

int TemperatureAndHumidityPin;
// ---------------------------------------------------------------
void setup()
{
    SerialUSB.println("*** temperature_humidity *** Jul/27/2021 PM 16:06");
    TemperatureAndHumidityBegin(SENSOR_PIN);
}

// ---------------------------------------------------------------
void loop()
{
    float temp;
    float humi;

    if (!TemperatureAndHumidityRead(&temp, &humi)) {
        SerialUSB.println("ERROR! TemperatureAndHumidityRead");
    }
else
    {
    SerialUSB.print("Current humidity = ");
    SerialUSB.print(humi);
    SerialUSB.print("%  ");
    SerialUSB.print("temperature = ");
    SerialUSB.print(temp);
    SerialUSB.println("C");
    }

    delay(2000);
}

// ---------------------------------------------------------------
void TemperatureAndHumidityBegin(int pin)
{
    TemperatureAndHumidityPin = pin;
    DHT22Init(TemperatureAndHumidityPin);
}

// ---------------------------------------------------------------
bool TemperatureAndHumidityRead(float* temperature, float* humidity)
{
  byte data[5];
  float ff = NAN;

  DHT22Start(TemperatureAndHumidityPin);
  for (int i = 0; i < 5; i++) data[i] = DHT22ReadByte(TemperatureAndHumidityPin);

  DHT22Finish(TemperatureAndHumidityPin);

  if (!DHT22Check(data, sizeof (data))) return false;
  ff = data[0];
  ff *= 256;
  ff += data[1];
  ff *= 0.1;
  *humidity = ff;

  ff = data[2] & 0x7F;
  ff *= 256;
  ff += data[3];
  ff *= 0.1;
  if (data[2] & 0x80) {
    ff *= -1;
  }
  *temperature = ff;

  return true;
}

// ---------------------------------------------------------------
void DHT22Init(int pin)
{
    digitalWrite(pin, HIGH);
    pinMode(pin, OUTPUT);
}

// ---------------------------------------------------------------
void DHT22Start(int pin)
{
  // Host the start of signal
  digitalWrite(pin, LOW);
  delay(18);

  // Pulled up to wait for
  pinMode(pin, INPUT);
  while (!digitalRead(pin)) ;

  // Response signal
  while (digitalRead(pin)) ;

  // Pulled ready to output
  while (!digitalRead(pin)) ;
}

// ---------------------------------------------------------------
byte DHT22ReadByte(int pin)
{
  byte data = 0;

  for (int i = 0; i < 8; i++) {
    while (digitalRead(pin)) ;

    while (!digitalRead(pin)) ;
    unsigned long start = micros();

    while (digitalRead(pin)) ;
    unsigned long finish = micros();

    if ((unsigned long)(finish - start) > 50) data |= 1 << (7 - i);
  }

  return data;
}

// ---------------------------------------------------------------
void DHT22Finish(int pin)
{
  // Releases the bus
  while (!digitalRead(pin)) ;
  digitalWrite(pin, HIGH);
  pinMode(pin, OUTPUT);
}

// ---------------------------------------------------------------
bool DHT22Check(const byte* data, int dataSize)
{
  if (dataSize != 5) return false;

  byte sum = 0;
  for (int i = 0; i < dataSize - 1; i++) {
    sum += data[i];
  }

  return data[dataSize - 1] == sum;
}

// ---------------------------------------------------------------

실행 결과

좋은 웹페이지 즐겨찾기