#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h>
#include <RTClib.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
#define SENSOR_PIN A0
#define DHTPIN 2 // what pin we're connected to
#define ONE_WIRE_BUS 4
#define CS_SD 10
#define DHTTYPE DHT22 // DHT 22 (AM2302)
RTC_DS3231 rtc; //Create rtc object
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire device
DallasTemperature sensors(&oneWire); // Pass oneWire reference to DallasTemperature library
File myFile; // Create a file to store the data
DHT dht(DHTPIN, DHTTYPE); // Create dht object
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
DateTime now;
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
int numberOfDevices; // Number of temperature devices found
float t1,t2,t3,t4,t5;
String date;
String time;
float humidity;
float db;
void setup(void) {
sensors.begin(); // Start up the library
Serial.begin(9600);
pinMode (SENSOR_PIN, INPUT); // Set the signal pin as input
while (!rtc.begin()) {
Serial.println("Connecting to RTC");
delay(500);
}
dht.begin();
lcd.init();
lcd.backlight();
lcd.print("Hello World");
pinMode(CS_SD, OUTPUT);
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// setup for the SD card
Serial.print("Initializing SD card...");
if(!SD.begin(CS_SD)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
//open file
myFile = SD.open("logger.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,T1,T2,T3,T4,T5,Humidity");
}
myFile.close();
}
void loop(void) {
readClock();
readTemp();
humidity = dht.readHumidity();
readDecibel();
//printData();
displayData();
writeToSD();
delay(5000);
}
void readTemp() {
// Send the command to get temperatures
sensors.requestTemperatures();
t1 = sensors.getTempCByIndex(0);
t2 = sensors.getTempCByIndex(1);
t3 = sensors.getTempCByIndex(2);
t4 = sensors.getTempCByIndex(3);
t5 = sensors.getTempCByIndex(4);
}
void writeToSD() {
//open file
myFile = SD.open("logger.txt", FILE_WRITE);
// if the file opened ok, write to it:
if (myFile) {
Serial.println("File opened ok");
myFile.print(date);
myFile.print(",");
myFile.print(time);
myFile.print(",");
myFile.print(t1);
myFile.print(",");
myFile.print(t1);
myFile.print(",");
myFile.print(t2);
myFile.print(",");
myFile.print(t3);
myFile.print(",");
myFile.print(t4);
myFile.print(",");
myFile.print(t5);
myFile.print(",");
myFile.println(humidity);
}
myFile.close();
}
void readClock () {
now = rtc.now();
date = now.timestamp(DateTime::TIMESTAMP_DATE);
time = now.timestamp(DateTime::TIMESTAMP_TIME);
}
void readDecibel() {
unsigned long startMillis= millis(); // Start of sample window
float peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0; //minimum value
unsigned int signalMin = 1024; //maximum value
// collect data for 50 mS
while (millis() - startMillis < sampleWindow) {
sample = analogRead(SENSOR_PIN); //get reading from microphone
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
db = map(peakToPeak,20,900,49.5,90); //calibrate for deciBels
}
void displayData() {
lcd.setCursor(0,0);
lcd.print("T1:");
lcd.print(t1);
lcd.print(" T2:");
lcd.print(t2);
lcd.setCursor(0,1);
lcd.print("T3:");
lcd.print(t3);
lcd.print(" T4:");
lcd.print(t4);
lcd.setCursor(0,2);
lcd.print("T5:");
lcd.print(t5);
lcd.print(" H :");
lcd.print(humidity);
lcd.setCursor(0,3);
lcd.print("db:");
lcd.print(db);
}
void printData() {
Serial.print(date);
Serial.print(",");
Serial.print(time);
Serial.print(",");
//print the temperature in Celsius
Serial.print("T1: ");
Serial.print(t1);
Serial.print((char)176);//shows degrees character
Serial.print(",");
Serial.print("T2: ");
Serial.print(t2);
Serial.print((char)176);//shows degrees character
Serial.print(",");
Serial.print("T3: ");
Serial.print(t3);
Serial.print((char)176);//shows degrees character
Serial.print(",");
Serial.print("T4: ");
Serial.print(t4);
Serial.print((char)176);//shows degrees character
Serial.print(",");
Serial.print("T5: ");
Serial.print(t5);
Serial.print((char)176);//shows degrees character
Serial.print(",");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("Loudness: ");
Serial.print(db);
Serial.println("dB");
}