#include "DHT.h"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <SD.h>
#define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
#define BMP_CS 10
#define DHT11PIN 9
#define SD_CS 8
#define DHT11_SW 7
#define BMP280_SW 6
const int BlueLed = 5;
const int GreenLed = 4;
const int RedLed = 3;
#define DHTTYPE DHT11
File myFile;
DHT dht(DHT11PIN, DHTTYPE);
Adafruit_BMP280 bme(BMP_CS);
int temperature = 0;
int humidity = 0;
int bmptemperature = 0;
int bmpaltitude = 0;
int bmppressure = 0;
uint32_t lasttime;
uint32_t nowtime;
uint32_t storelasttime;
bool dht11flag=1;
bool bmp280flag=1;
void setup(void) {
Serial.begin(9600);
pinMode(BlueLed, OUTPUT);
pinMode(GreenLed, OUTPUT);
pinMode(RedLed, OUTPUT);
pinMode(DHT11_SW, INPUT_PULLUP);
pinMode(BMP280_SW, INPUT_PULLUP);
Serial.println(F("BMP280 test"));
if (!bme.begin()) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
dht.begin();
myFile = SD.open("test.txt", FILE_WRITE);
myFile.close();
//delay(1000);
lasttime = millis();
storelasttime = millis();
}
void bmp280()
{
Serial.print("bmp280 Temperature = ");
bmptemperature = bme.readTemperature();
Serial.print(bmptemperature);
Serial.println(" *C");
bmppressure = bme.readPressure();
Serial.print("bmp280 Pressure = ");
Serial.print(bmppressure);
Serial.println(" Pa");
Serial.print("bmp280 Approx altitude = ");
bmpaltitude = bme.readAltitude(1013.25);
// this should be adjusted to your local forcast
Serial.print(bmpaltitude);
Serial.println(" m");
}
void dht11()
{
float h,t;
//int chk = DHT11.read(DHT11PIN);
h = dht.readHumidity();
t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
humidity = h;
temperature = t;
Serial.print("dht11 Humidity : ");
//Serial.println((float)DHT11.humidity, 2);
Serial.print(humidity);
Serial.println(" %");
Serial.print("dht11 Temperature : ");
//Serial.println((float)DHT11.temperature, 2);
Serial.print(temperature);
Serial.println(" *C");
if (temperature<25){
digitalWrite(BlueLed,HIGH); // turn on blue led
digitalWrite(RedLed,LOW); // turn off green led
digitalWrite(GreenLed,LOW); // turn off red led
}
else if((temperature>=25)&&(temperature<30)) {
digitalWrite(BlueLed,LOW);
digitalWrite(RedLed,LOW);
digitalWrite(GreenLed,HIGH);
}
else if (temperature>=30) {
digitalWrite(BlueLed,LOW);
digitalWrite(RedLed,HIGH);
digitalWrite(GreenLed,LOW);
}
}
void loop(void)
{
nowtime = millis();
if((nowtime-lasttime)>5000)//5s once read sensor
{
lasttime = nowtime;
if(bmp280flag==1)
bmp280();
if(dht11flag==1)
dht11();
}
if((nowtime-storelasttime)>120000)//2min store the data
{
storelasttime = nowtime;
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
Serial.print("Writing to test.txt...");
if(bmp280flag==1)
{
myFile.print("bmp T:");
myFile.print(bmptemperature);
myFile.print(" P:");
myFile.println(bmppressure);
}
if(dht11flag==1)
{
myFile.print("dht T:");
myFile.print(temperature);
myFile.print(" H:");
myFile.println(humidity);
}
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
//dht11 on/off function
if(digitalRead(DHT11_SW) == 0)
{
delay(10);
if(digitalRead(DHT11_SW) == 0)
{
if(dht11flag==1)
{
dht11flag=0;
Serial.println("dht11 sensor close!");
}
else
{
dht11flag=1;
Serial.println("dht11 sensor open!");
}
while((digitalRead(DHT11_SW) == 0));
}
}
//bmp280 on/off function
if(digitalRead(BMP280_SW) == 0)
{
delay(10);
if(digitalRead(BMP280_SW) == 0)
{
if(bmp280flag==1)
{
bmp280flag=0;
Serial.println("bmp280 sensor close!");
}
else
{
bmp280flag=1;
Serial.println("bmp280 sensor open!");
}
while((digitalRead(BMP280_SW) == 0));
}
}
//reading bluetooth command
while (Serial.available()>0){
char in = Serial.read();
if (in == 'A'){
}
else if (in == 'B'){
}
else if (in == 'C'){
}
}
}