1.以前からの改善点
(Arduino)DHT11とDS1307のデータをSDカードに保存する(その1)で一応実現できたが、別のコードのほうが使い勝手が良い気がして、それを試してみた。
参考にしたのは、https://ht-deko.com/arduino/tiny_rtc.html
- PCの時刻と同期できるようになった。
- 保存ファイル名がyymdからyyyymmddになった。
2.コード(接続は前と異ならないので省略。)
#include <Wire.h>
#include <SimpleDHT.h>
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>
#define RTC_address 0x68
DS1307 RTC;
int pinDHT11= 7;
SimpleDHT11 dht11(pinDHT11);
File myFile;
void setup () {
Serial.begin(115200);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
//RTC.adjust(DateTime(__DATE__, __TIME__));
}
RTC.adjust(DateTime(__DATE__, __TIME__));
}
void loop () {
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000);
return;
}
DateTime now = RTC.now();
// Serial.print now for test.
Serial.print(now.year(),DEC);
//Serial.print('/');
if ( now.month() < 10 ){
Serial.print("0");
Serial.print(now.month(), DEC);
}else{
Serial.print(now.month(), DEC);
}
//Serial.print('/');
if( now.day() < 10 ){
Serial.print("0");
Serial.print(now.day(), DEC);
}else{
Serial.print(now.day(), DEC);
}
Serial.print("\t");
Serial.print("計測時間:");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print("\t");
Serial.print("Temp:");
Serial.print((int)temperature);
Serial.print("C");
Serial.print("\t");
Serial.print("Humi:");
Serial.print((int)humidity);
Serial.println('%');
//SD-card save
if (!SD.begin(4)) {
//Serial.println("initialization failed!");
while (1);
}
//Serial.println("initialization done.");
// file name format yyyymmdd.csv
if (( now.month() < 10 ) && ( now.day() < 10 )){
myFile=SD.open( String(now.year(),DEC) + "0" + String(now.month(),DEC) + "0" + String(now.day(),DEC) + ".csv",FILE_WRITE);
}else if( now.month() < 10 ){
myFile=SD.open( String(now.year(),DEC) + "0" + String(now.month(),DEC) + String(now.day(),DEC) + ".csv",FILE_WRITE);
}else if( now.day() < 10) {
myFile=SD.open( String(now.year(),DEC) + String(now.month(),DEC) + "0" + String(now.day(),DEC) + ".csv",FILE_WRITE);
}else{
myFile=SD.open( String(now.year(),DEC) + String(now.month(),DEC) + String(now.day(),DEC) + ".csv",FILE_WRITE);
}
if (myFile){
//write contents: time, temperature and humidity,
myFile.print(now.hour(),DEC);
myFile.print(":");
myFile.print(now.minute() ,DEC);
myFile.print(",");
myFile.print(temperature);
myFile.print(",");
myFile.println(humidity);
// close the file:
myFile.close();
//Serial.println("sdcard write done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening file");
Serial.print(now.year(),DEC);
//Serial.print('/');
if ( now.month() < 10 ){
Serial.print("0");
Serial.print(now.month(), DEC);
}else{
Serial.print(now.month(), DEC);
}
//Serial.print('/');
if( now.day() < 10 ){
Serial.print("0");
Serial.print(now.day(), DEC);
}else{
Serial.print(now.day(), DEC);
}
Serial.print("\t");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(",");
Serial.print((int)temperature);
Serial.print(",");
Serial.print((int)humidity);
Serial.println(",");
//Serial.print(':');
//Serial.print(now.second(), DEC);
Serial.println("file write failed!");
}
/* これ以降はテスト時に必要であっても、ファイルを開き読み込み、表示するのに時間がかかり、1日で1分以上の狂いが生じました。あくまでテスト時のものと考えたほうが良さそうです。
if (( now.month() <10 ) && ( now.day() < 10 )){
myFile=SD.open( String(now.year(),DEC) + "0" + String(now.month(),DEC) + "0" + String(now.day(),DEC) + ".csv");
}else if( now.month() < 10 ){
myFile=SD.open( String(now.year(),DEC) + "0" + String(now.month(),DEC) + String(now.day(),DEC) + ".csv");
}else if( now.day() < 10) {
myFile=SD.open( String(now.year(),DEC) + String(now.month(),DEC) + "0" + String(now.day(),DEC) + ".csv");
}else{
myFile=SD.open( String(now.year(),DEC) + String(now.month(),DEC) + String(now.day(),DEC) + ".csv");
}
if (myFile) {
Serial.print("re-open file:");
if (( now.month() < 10 ) && ( now.day() < 10 )){
Serial.println ( String(now.year(),DEC) + "0" + String(now.month(),DEC) + "0" + String(now.day(),DEC) + ".csv");
}else if( now.month() < 10 ){
Serial.println( String(now.year(),DEC) + "0" + String(now.month(),DEC) + String(now.day(),DEC) + ".csv");
}else if( now.day() < 10) {
Serial.println( String(now.year(),DEC) + String(now.month(),DEC) + "0" + String(now.day(),DEC) + ".csv");
}else{
Serial.println( String(now.year(),DEC) + String(now.month(),DEC) + String(now.day(),DEC) + ".csv");
}
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening file");
}
--- ここまで ------ */
delay(60000UL);
}
コメント