Dieses kleine Projekt soll mit Hilfe einiger Sensoren die Zimmer-Umgebung für längere Zeit messen, damit später eine (grafische) Auswertung gemacht werden kann.
Der zu bauende Temperatur-Logger soll folgende Aufgaben lösen:
Der Aufbau ist eine Zusammenstellung einiger schon verwendeter Module:
Die Kombination der Komponenten in der Software war recht problemlos, nur ist durch den Einsatz einiger Libraries der Speicher sehr schnell weniger geworden. Letztendlich ist durch einige Einsparungen jedoch noch genug für dynamische Variablen vorhanden.
/**
* Used pins:
* A0 TMP36
* A1 LDR
* A5 SCL (RTC)
* A4 SDA (RTC)
* 8 DHT22
* 9 LED
* 10 CS (SD card module)
* 11 MOSI (SD card module)
* 12 MISO (SD card module)
* 13 CLK/SCK (SD card module)
*/
#define MEASURING_DELAY 300000 // time in milliseconds between measuring events (should be 5 minutes = 300 seconds = 300000 milliseconds)
#define TMP36_AREF_VOLTAGE 5.0 // depends if we´re using 3.3V or 5.0V for operating the TMP36
#define TMP36_PIN A0
#define DHT22_PIN 8
#define LDR_PIN A1
#define STATUS_LED_PIN 9
#define SDCARD_CS_PIN 10
#define DS1307_ADDRESS 0x68 // I2C Address
#include <DHT.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
const char *SD_FILE = "log.csv";
File logFile;
unsigned long lastMeasuringTime = 0;
int sensorValue = 0;
int tmp36Temperature = 0;
int ldrBrightness = 0;
int dh22Humidity = 0;
int dht22Temperature = 0;
DHT dht22(DHT22_PIN, DHT22);
void setup()
{
Serial.begin(9600);
Serial.flush();
pinMode(STATUS_LED_PIN, OUTPUT);
if (!SD.begin(SDCARD_CS_PIN)) {
Serial.println("ERR");
while (true); // stop further execution
}
String logData = String("DATETIME;TEMP1;TEMP2;HUMIDITY;BRIGHTNESS");
Serial.println(logData);
logFile = SD.open(SD_FILE, FILE_WRITE);
if (logFile) {
logFile.println(logData);
logFile.close();
} else {
Serial.println("ERR");
}
dht22.begin();
Wire.begin();
}
void loop()
{
if ((millis() - lastMeasuringTime) > MEASURING_DELAY) {
lastMeasuringTime = millis();
digitalWrite(STATUS_LED_PIN, HIGH);
sensorValue = analogRead(TMP36_PIN);
tmp36Temperature = (int)getTemperature(getVoltage(sensorValue));
ldrBrightness = (int)analogRead(LDR_PIN);
dht22Temperature = (int)dht22.readTemperature();
dh22Humidity = (int)dht22.readHumidity();
String logData = String(getDateTime() + ";" + tmp36Temperature + ";" + dht22Temperature + ";" + dh22Humidity + ";" + ldrBrightness);
Serial.println(logData);
logFile = SD.open(SD_FILE, FILE_WRITE);
if (logFile) {
logFile.println(logData);
logFile.close();
} else {
Serial.println("ERR");
}
digitalWrite(STATUS_LED_PIN, LOW);
}
}
/**
* Converts the 0 to 1023 value from a sensor value into a 0.0 to 5.0
* value that is the true voltage being read at that sensor.
*/
float getVoltage(int sensorValue)
{
return (sensorValue * TMP36_AREF_VOLTAGE / 1024);
}
/**
* Converting the voltage to degrees Celsius.
* The formula comes from the datasheet of the TMP36
*/
float getTemperature(float voltage)
{
return (voltage - 0.5) * 100.0;
}
/**
* Converts a decimal (Base-10) integer to BCD (Binary-coded decimal)
*/
int decToBcd(int value)
{
return ((value/10*16) + (value%10));
}
/**
* Converts a BCD (Binary-coded decimal) to decimal (Base-10) integer
*/
int bcdToDec(int value)
{
return ((value/16*10) + (value%16));
}
String getDateTime()
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
byte nowSeconds = bcdToDec(Wire.read());
byte nowMinute = bcdToDec(Wire.read());
byte nowHour = bcdToDec(Wire.read() & 0b111111);
byte nowWeekDay = bcdToDec(Wire.read());
byte nowMonthDay = bcdToDec(Wire.read());
byte nowMonth = bcdToDec(Wire.read());
byte nowYear = bcdToDec(Wire.read());
char data[20] = "";
sprintf(data, "20%02d-%02d-%02d %02d:%02d:%02d", nowYear, nowMonth, nowMonthDay, nowHour, nowMinute, nowSeconds);
return String(data);
}
Nach ca. 24h Laufzeit ist sind folgende Daten gesammelt worden: