close

小編經過多日的實驗,終於完成了溫溼度感測裝置! ! !

本篇將介紹如何製作溫溼度模組,未來會再提供如何將讀取後的溫濕度彙整成CSV檔案上傳到資料庫中,再由網頁顯示數據,並可使用Python程式語言來分析每日的溫溼度數據。

 

成品圖(下圖)
S__30973956.jpg

 

首先先來看看我們要使用的微控制器(下圖)

圖片來源:https://goods.ruten.com.tw/item/show?21406251483512

[圖片來源]https://goods.ruten.com.tw/item/show?21406251483512

 

NANO微控制器電路圖(下圖)

圖片來源:https://sites.google.com/site/csjhmaker/arduino-ji-chu/arduino-jiao-wei/arduino-nano

[圖片來源]https://sites.google.com/site/csjhmaker/arduino-ji-chu/arduino-jiao-wei/arduino-nano

 

DTH11 溫溼度感測器(下圖)

61Bp2IxHTtL._SX425_.jpg

[圖片來源] https://www.amazon.co.uk/DaoRier-Arduino-Compatible-Temperature-Humidity/dp/B06X19VJ19

 

RTC DS1307 時間模組(下圖)

DS1307.jpg

[圖片來源] https://www.taiwaniot.com.tw/product/%E5%BE%AE%E5%9E%8B-rtc-ds1307-%E6%99%82%E9%96%93%E6%A8%A1%E7%B5%84-i2c-%E4%BB%8B%E9%9D%A2/

 

SD卡模組(下圖)

51vNrSpS5L.jpg
[圖片來源] https://www.taiwaniot.com.tw/product/1804272258a2-5/

 

記憶卡(下圖)
000001_1499752117.png
[圖片來源] https://24h.pchome.com.tw/prod/DGAG1E-A41500664?fq=/S/DGAG1E

 

溫溼度感測裝置電路圖(下圖)

tempurature_bb.png

 

Arduino程式碼:

#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"

#include <SPI.h>
#include <SD.h>
#include <DHT.h>
#include <RTClib.h>

#define I2C_ADDRESS 0x3C
#define RST_PIN -1
SSD1306AsciiWire oled;

#define DHTPIN 2
#define DHTTYPE DHT11   // DHT 11
#define OLED_RESET 5

DHT dht(DHTPIN, DHTTYPE);

const int chipSelect = 4; 

File myFile;

RTC_DS1307 rtc;

void setup() {
  dht.begin();

  //initializing Serial monitor
  Serial.begin(9600);

  Wire.begin();
  Wire.setClock(400000L);
  dht.begin();

  #if RST_PIN >= 0
    oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
  #else // RST_PIN >= 0
    oled.begin(&Adafruit128x64, I2C_ADDRESS);
  #endif // RST_PIN >= 0

  oled.setFont(System5x7);
  
  // setup for the RTC
  while(!Serial); // for Leonardo/Micro/Zero
    if(! rtc.begin()) {
      Serial.println("Couldn't find RTC");
      while (1);
    } else {
      // following line sets the RTC to the date & time this sketch was compiled
      rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    }
    if(! rtc.isrunning()) {
      Serial.println("RTC is NOT running!");
    }
    
  // setup for the SD card
  Serial.print("Initializing SD card...");

  if(!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
    
  //open file
  myFile=SD.open("DATA.txt", FILE_WRITE);

  // if the file opened ok, write to it:
  if (myFile) {
    Serial.println("File opened ok");
    // print the headings for our data
    myFile.println("Date,Time,Temperature ºC");
  }
  myFile.close();
}

void loggingTime() {
  DateTime now = rtc.now();
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(',');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print(",");
  }
  myFile.close();
  delay(1000);  
}

void loggingTemperature() {
  float t = dht.readTemperature();

  if  (isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  } else {
    oled.clear();
    oled.setCursor(0,0);
    oled.print("Tmp:");
    oled.println(t);
  }
  
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C");
  
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    Serial.println("open with success");
    myFile.print(t);
    myFile.println(",");
  }
  myFile.close();
}

void loop() {
  loggingTime();
  loggingTemperature();
  delay(5000);
}

 

實驗成品圖(下圖)
S__30973956.jpg

 

大功告成~~~

 

 

 

arrow
arrow
    創作者介紹
    創作者 sky 的頭像
    sky

    sky的異想世界

    sky 發表在 痞客邦 留言(0) 人氣()