#include <Wire.h>
#include <DHT.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#define DHTPIN A2
#define DHTTYPE DHT11
int ledPin = 5;
int dhtPin = A2;
int value = LOW;
long previousMillis = 0;
long interval = 3000;
String sendLine = "";
File myFile;
String filename = "temp12.txt";
DHT dht(DHTPIN,DHTTYPE);
SoftwareSerial mySerial(2, 3);
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3, POSITIVE);
//LiquidCrystal_I2C lcd(0x27,16,2);
void setup()
{
Serial.begin(115200);
mySerial.begin(9600);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(dhtPin, OUTPUT);
lcd.begin(16,2);
lcd.setBacklight(HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Starting");
}
void loop()
{
if (millis() - previousMillis > interval)
{ previousMillis = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa.
if (value == LOW) value = HIGH; else value = LOW;
digitalWrite(ledPin, value); // sets the LED on
float h = dht.readHumidity();
float t = dht.readTemperature();
sendLine = String("Temp: ")+ t ;
delay(10);
mySerial.print(sendLine+"\n");
lcd.setCursor(0,0);
lcd.print(sendLine);
sdWrite(filename,sendLine);
}
}
void sdWrite(String file, String line)
{
myFile = SD.open(file, FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.println(line);
// close the file:
myFile.close();
Serial.println("Write ok");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}