小編經過多日的實驗,終於完成了溫溼度感測裝置! ! !
本篇將介紹如何製作溫溼度模組,未來會再提供如何將讀取後的溫濕度彙整成CSV檔案上傳到資料庫中,再由網頁顯示數據,並可使用Python程式語言來分析每日的溫溼度數據。
成品圖(下圖)
首先先來看看我們要使用的微控制器(下圖)
[圖片來源]https://goods.ruten.com.tw/item/show?21406251483512
NANO微控制器電路圖(下圖)
[圖片來源]https://sites.google.com/site/csjhmaker/arduino-ji-chu/arduino-jiao-wei/arduino-nano
DTH11 溫溼度感測器(下圖)
[圖片來源] https://www.amazon.co.uk/DaoRier-Arduino-Compatible-Temperature-Humidity/dp/B06X19VJ19
RTC DS1307 時間模組(下圖)
SD卡模組(下圖)
[圖片來源] https://www.taiwaniot.com.tw/product/1804272258a2-5/
記憶卡(下圖)
[圖片來源] https://24h.pchome.com.tw/prod/DGAG1E-A41500664?fq=/S/DGAG1E
溫溼度感測裝置電路圖(下圖)
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);
}
實驗成品圖(下圖)
大功告成~~~
留言列表