티스토리 뷰

Arduino

(macOS)[python] Arduino UNO DHT11 sensor DAQ

jinozpersona 2022. 11. 10. 09:33

Intro

OS : MacOS, Monterey, v12.6

HW : Arduino UNO R3 board, DHT11 Sensor

SW : Visual Studio Code, so-called vscode

python : 3.10.7

 

 

- Arduino 연결 부터 확인하려면 다음 포스트 참고

Arduino UNO DHT11 sensor 연결 및 모니터링

 

- DAS : Data Aquisition System

- DAQ: Data Aquisition, 데이터 수집

- DAQ는 온도, 습도, 압력, 유량 등의 여러 센서 데이터를 하나의 보드로 통합 수집

 

Requirements

vscode

vscode extension : Arduino

 

 

Arduino UNO R3 : DHT11 Sensor 연결도

 

 

 

1. Arduino: Initialize

2. Arduino: Library Manager

 

# 참고 : (macOS)[python] pyserial을 이용한 Arduino UNO R3 제어 with vscode - 2/2

 

3. Arduino Coding & Upload

ard_dht11.ino
#include <DHT_U.h>
#include <DHT.h>

// Digital pin connected to the DHT sensor
#define DHTPIN 2
// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);
int baudrate = 9600;

void setup() {
  Serial.begin(baudrate);
  // Serial.println(F("DHT11 Sensor Start!"));
  // delay(100);
  dht.begin();
}

void loop(){
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
	
  // 참고 : Arduino Serial Monitor 출력용
  // Serial.print(F("Humidity: "));
  // Serial.print(h);
  // Serial.print(F("%  Temperature: "));
  // Serial.print(t);
  // Serial.print(F("°C  Heat Index: "));
  // Serial.print(hic);
  // Serial.println(F("°C "));
  
  // 간단하게 습도, 온도(h,t) 데이터만 받아서 python 터미널에서 coding
  Serial.print(h);
  Serial.println(t);
  delay(1000);
}

Arduino: Verify

Arduino: Upload

 

 

4. python Coding & DAQ

py_dht11.py
import os
import serial
from time import localtime, strftime

serial_port = '/dev/tty.usbmodem141101'
serial_baudrate = 9600
ard = serial.Serial(serial_port,serial_baudrate)
fname = ['data_save.csv','data_save.txt']
if os.path.exists(fname[0]):
    os.remove(fname[0])
if os.path.exists(fname[1]):
    os.remove(fname[1])

cnt = 1
while True:
    td = strftime('%Y-%m-%d', localtime())
    tt = strftime('%H:%M:%S', localtime())
    
    datas_raw = ard.readline()
    # print(datas_raw)
    
    ## ard.decode() : readline을 decoding
    datas = datas_raw.decode()[:len(datas_raw)-2]

    print('--- Arduino Output ---\n{}\n'.format(datas))
    
    print('--- python Terminal Output ---')
    print('Count : {}'.format(cnt))
    print('Humidity[%] : {}\nTemperature[oC] : {}\n'.format(datas[:5],datas[5::]))
    
    with open('data_save.txt','a') as tf:
        tf.write("{} {},{}: {},{}\n".format(cnt,td,tt,datas[0:5],datas[5::]))
        tf.close()

    with open('data_save.csv','a') as cf:
        cf.write("{},{},{},{}\n".format(td,tt,datas[0:5],datas[5::]))
        cf.close()
    cnt = cnt + 1

 

text, csv 2가지 타입으로 저장

결과#1 txt

1 2022-11-10,17:41:45: 36.00,25.60
2 2022-11-10,17:41:47: 36.00,25.60
3 2022-11-10,17:41:48: 37.00,25.50
4 2022-11-10,17:41:49: 37.00,25.50
5 2022-11-10,17:41:50: 37.00,25.50

결과#2 csv

2022-11-10 17:41:45 36 25.6
2022-11-10 17:41:47 36 25.6
2022-11-10 17:41:48 37 25.5
2022-11-10 17:41:49 37 25.5
2022-11-10 17:41:50 37 25.5

 

# 참고 : python Terminal 실행창

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함