2015年9月29日 星期二

DS1302_DS18x20_DHT11_2004LCD



溫度,濕度再加上時間,本想再加上SD卡,使它有Datalogger的功能,無耐與I2CLCD的libraries相衝吧?

#include "stdio.h"
#include "DS1302.h"
#include "OneWire.h"
#include "DallasTemperature.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "dht.h"

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

LiquidCrystal_I2C lcd(0x3f,20,4);
dht DHT;
#define DHT11_PIN 3

namespace {

const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock

DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}
void printTime() {
  // Get the current time and date from the chip.
  Time t = rtc.time();

  // Name the day of the week.
  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  // Print the formatted string to serial so we can see the time.
  Serial.println(buf);
 }
}  // namespace

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  //Serial.println("Dallas Temperature IC Control Library");

  // Start up the library
  sensors.begin();

  lcd.init();
  lcd.backlight();
  //lcd.print("Room Temperature");
}

void loop(void)
{
  printTime();
  lcd.setCursor(0,0);
  Time t = rtc.time();
  char buf[50];
  snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d",
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);
  //Serial.println(buf);
  lcd.print(buf);
  //Serial.print("Requesting temperatures...");
  //sensors.requestTemperatures();
  //Serial.println("DONE");
  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0));
  Serial.print("Temperature for the device 2 (index 1) is: ");
  Serial.println(sensors.getTempCByIndex(1));
  lcd.setCursor(0,1);
  lcd.print("DS18S20 A: ");
  lcd.print(sensors.getTempCByIndex(0));
  lcd.print("C");
  lcd.setCursor(0,2);
  lcd.print("DS18B20 B: ");
  lcd.print(sensors.getTempCByIndex(1));
  lcd.print("C");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  delay(1000);
  Serial.println(DHT.humidity, 1);
  Serial.println(DHT.temperature, 1);
  lcd.setCursor(0,3);
  lcd.print("DHT11:  ");
  lcd.print(DHT.humidity, 0);
  lcd.print("%  ");
  // lcd.print("Temp: ");
  lcd.print(DHT.temperature,0);
  lcd.print("C");
  delay(1000);
}

沒有留言:

張貼留言