這個結構包含
DHT11:Vcc --> 5V, Gnd --> Gnd, Data --> D9
BMP085:Vcc --> 5V, Gnd --> Gnd, SDA --> A4, SCK --> A5, XCLR --> D8
BMP085:Vcc --> 5V, Gnd --> Gnd, SDA --> A4, SCK --> A5, XCLR --> D10
Relay:Vcc --> 5V, Gnd --> Gnd, IN1 --> D5, IN2 --> D6, 線路常開
1602LCD:Vcc --> 5V, Gnd --> Gnd, SDA --> A4, SCK --> A5
#include <Wire.h>// Comes with Arduino IDE
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;
#include <LiquidCrystal_I2C.h>
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 9
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2,1,0,4,5, 6,7, 3, POSITIVE);// Set the LCD I2C address
int pos[] = {0,0};
float t8,t10;
char *s = "Page 1", *t = "Page 2";
int pgCnt = 25;
#define DEBOUNCE_DELAY 200//解決物理按鈕多次切換問題
static unsigned long lastDebounceTime;
void setup(){
Serial.begin(9600);
pos[0] = 0;
pos[1] = 0;
bgBlink();
lcd.backlight();
lcd.setCursor(0,0);
pinMode(5,OUTPUT);
digitalWrite(5,HIGH);
pinMode(6,OUTPUT);
digitalWrite(6,HIGH);
pinMode(8,OUTPUT);
pinMode(10,OUTPUT);
digitalWrite(8,HIGH);
if (!bmp.begin()) {
while (1) {}
}
digitalWrite(8,LOW);
digitalWrite(10,HIGH);
if (!bmp.begin()) {
while (1) {}
}
digitalWrite(10,LOW);
pinMode(3, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
attachInterrupt(0, pgCntPlus, FALLING);
attachInterrupt(1, pgCntMinus, FALLING);
}
void loop(){
int chk = DHT11.read(DHT11PIN);
if(chk==DHTLIB_OK){
pos[0]=0; pos[1]=0; showMsg(pos, pgCnt );
pos[0]=0; pos[1]=1; showMsg(pos, "*C:");
pos[0]=3; pos[1]=1; showMsg(pos, (int)DHT11.temperature);
delay(200);
}
digitalWrite(8,HIGH);
bmp.bmp085Calibration();
t8 = bmp.readTemperature();
pos[0]=8;pos[1]=0;showMsg(pos, t8);
digitalWrite(8,LOW);
delay(200);
digitalWrite(10,HIGH);
bmp.bmp085Calibration();
t10 = bmp.readTemperature();
pos[0]=8;pos[1]=1;showMsg(pos, t10);
digitalWrite(10,LOW);
delay(200);
if(t8<(float)28){
digitalWrite(5,LOW);
}else{
digitalWrite(5,HIGH);
}
}
void pgCntPlus(){
unsigned long currentTime = millis();
if((currentTime - lastDebounceTime) > DEBOUNCE_DELAY){
lastDebounceTime = currentTime;
pgCnt++;
}
}
void pgCntMinus(){
unsigned long currentTime = millis();
if((currentTime - lastDebounceTime) > DEBOUNCE_DELAY){
lastDebounceTime = currentTime;
pgCnt--;
}
}
void bgBlink(){
lcd.begin(16,2);// initialize the lcd for 20 chars 4 lines and turn on backlight
for(int i = 0; i< 3; i++){
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
}
void showMsg(int *pos, int arrt){
//(col,row)
lcd.setCursor(pos[0],pos[1]);
lcd.print(arrt);
}
void showMsg(int *pos, float arrt){
//(col,row)
lcd.setCursor(pos[0],pos[1]);
lcd.print(arrt);
}
void showMsg(int *pos, char *c){
lcd.setCursor(pos[0],pos[1]);
while(*c){
lcd.print(*c++);
}
}
//字串相加(String Concatenation)
//將"abc"加上"def"變成"abcdef"
void strcat(char *s1, char *s2) {
int i, j;
for (i = strlen(s1), j = 0; s1[i] = s2[j]; i++, j++)
;
}
//字串長度
int strlen(char s[]) {
int i;
for (i = 0; s[i] != 0; i++)
;
return i;
}
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius){
return 1.8 * celsius + 32;
}
//Celsius to Kelvin conversion
double Kelvin(double celsius){
return celsius + 273.15;
}
// dewPoint function NOAA
// reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
// reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
//
double dewPoint(double celsius, double humidity){
// (1) Saturation Vapor Pressure = ESGG(T)
double RATIO = 373.15 / (273.15 + celsius);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
// factor -3 is to adjust units - Vapor Pressure SVP * humidity
double VP = pow(10, RHS - 3) * humidity;
// (2) DEWPOINT = F(Vapor Pressure)
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}
// delta max = 0.6544 wrt dewPoint()
// 6.9 x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity){
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity*0.01);
double Td = (b * temp) / (a - temp);
return Td;
}