0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

nRF52840DKとDHT11を用いて温湿度を測る.

Last updated at Posted at 2021-10-17

はじめに

こちらのプロジェクトでBLEを用いたIoTを構築し,いちごを育てるというプロジェクトをゼミのメンバーと行っています.
その中でペリフェラルとなる,nRF5系を用いてセンサデータの収集を今回行ったので,記録を残す.
また,nRF系は日本語での記事が少ないため,新たな利用者の力になれば嬉しい.

前提

SEGGER EMBEDDED Studioは入っている状態とし,前回の記事であるnRF52840のSDKも入っていることを前提に行う.

温度を取得するコードについて

こちらにあるコードを利用する.

# include <stdbool.h>
# include <stdint.h>
# include <stdio.h>
# include "nrf.h"
# include "nrf_delay.h"
# include "app_error.h"
# include "bsp.h"

# include "nrf_log.h"
# include "nrf_log_ctrl.h"
# include "nrf_log_default_backends.h"


# define DHT11_PIN 7

int DHT11ReadByte(void)
{
  int ByteData=0,i;
  unsigned long int counter=0;
  for(i=0;i<8;i++)
  {
    ByteData = ByteData * 2;
    counter = 0;
    while(0==(nrf_gpio_pin_read(DHT11_PIN))) // may be 50us
    {
      counter++;
      nrf_delay_us(1);
      if ( 200 < counter  ){
        NRF_LOG_INFO("error1\n");
        return -1;
      }
    }
    counter = 0;
    while(1==(nrf_gpio_pin_read(DHT11_PIN)))
    {
      counter++;
      nrf_delay_us(1);
      if ( 200 < counter  ){
        NRF_LOG_INFO("error2\n");
        return -1;
      }
    }
    if(counter<30)  // 40... error
    {
            // 26-28us : Bit data "0"
    } else {
      ByteData = ByteData +1; // 70us : Bit data "1"
    }           
  }
  return ByteData;
}

void DHT11ReadData()
{   
  int counter,err,humiInt,humiDec,tempInt,tempDec,checkSum,decimal_value1,decimal_value2,decimal_value3;
  nrf_gpio_pin_set(DHT11_PIN);
  nrf_delay_ms(250);
  nrf_gpio_cfg_output(DHT11_PIN);
  nrf_gpio_pin_clear(DHT11_PIN);   // Send Start Signal
  nrf_delay_ms(20);   
  nrf_gpio_pin_set(DHT11_PIN);
  nrf_delay_us(40);                // Send Start Signal end
  nrf_gpio_cfg_input(DHT11_PIN, NRF_GPIO_PIN_PULLUP);
  nrf_delay_us(1);
  counter=0;
  while(nrf_gpio_pin_read(DHT11_PIN)==0) // Receive LOW answer ( 83us)
  {
    nrf_delay_us(1);
    counter++;
    if ( 200 < counter ){
        NRF_LOG_INFO("got wrong start1\n");
        return;
    }
  }
  counter=0;
  while(nrf_gpio_pin_read(DHT11_PIN)==1) // Recieve High answer ( 87us)
  {
    nrf_delay_us(1);
    counter++;
    if ( 200 < counter ){
      NRF_LOG_INFO("got wrong start2\n");
      return;
    }
  }
  humiInt = DHT11ReadByte();
  humiDec = DHT11ReadByte();
  tempInt = DHT11ReadByte();
  tempDec = DHT11ReadByte();
  checkSum = DHT11ReadByte();

  if (((humiInt + humiDec + tempInt + tempDec) & 0xff) != checkSum) 
  {
     NRF_LOG_INFO("got wrong values\n");
     return -1;
  } else {
    if ( 127 < tempDec ){
      tempInt=0-tempInt;
      tempDec=tempDec-128;
    }
    NRF_LOG_INFO("TEMP: %d.%d  HUMI: %d.%d",tempInt,tempDec,humiInt,humiDec);
  }
}


int main(void)
{
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();
    NRF_LOG_INFO("Temp/Humi sensor DHT11 example started.");
    nrf_gpio_cfg_input(DHT11_PIN, NRF_GPIO_PIN_PULLUP);
    SEGGER_RTT_WriteString(0, "DHT11 init complete\n");
    while (true)
    {
        DHT11ReadData();
        nrf_delay_ms(3000);
        NRF_LOG_FLUSH();
    }
}

ビルドとデバッグ

スクリーンショット 2021-10-17 23.10.41.png
Add New Projectを選択する.
次に nRF5_SDK_.../examples/peripheral/temperature/pca10056/blank/ses/temperature_pca10056.emProjectを選択する.
スクリーンショット 2021-10-17 23.13.42.png
Build temperature_pca10056を選択し,ビルドを行う.
スクリーンショット 2021-10-17 23.15.07.png
Goを行い,センサがしっかりと設置することができれば,温度が取れるはず.
まだセンサを設置できていないので今日はここまで.

DHT11_PIN 7がなんなのか.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?