在这记录minecraft的点点滴滴。

作者:linfengfeiwu2018-8-5 23:18分类: 笔记 标签: Arduino

上一个时钟只能显示时间,这次的这个又增加了温湿度,功能上更加完善。1602也改为了IIC的,方便接线。温湿度传感器使用的是DHT11,如果要精度再高点可以使用DHT22,他们用的是同一个库。

硬件连接(注意GPIO标号为板子后面的标示):

1602:

VCC —— VCC
GND —— GND
SCL —— SCL/GPIO4
SDA —— SDA/GPIO5

DHT11:

VCC —— VCC
GND —— GND

out——GPIO12


(D1 mini接线:

SCL —— D1
SDA —— D2

DHT11 out——D6

代码:

/*
   ESP8266+1602IIC wifi clock
   by: linfengfeiwu
   http://blog.readgroup.cn
*/

#include <ESP8266WiFi.h>
#include <Time.h>
#include <Timezone.h>
#include "NTP.h"

//DHT11 Sensor:
#include "DHT.h"
#define DHTPIN 12     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);
//I2C LCD:
#include <Wire.h> // Comes with Arduino IDE
#include <LiquidCrystal_I2C_DS3231.h>

// Set the LCD I2C address
#define I2C_ADDR    0x27  // Define I2C Address for the PCF8574T 
//---(Following are the PCF8574 pin assignments to LCD connections )----
// This are different than earlier/different I2C LCD displays

#define Rs_pin  0
#define Rw_pin  1
#define En_pin  2
#define BACKLIGHT_PIN  3
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

#define  LED_OFF  0
#define  LED_ON  1

/*-----( Declare objects )-----*/  
LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

// Set your WiFi login credentials
#define WIFI_SSID "xxx"       // 使用时请修改为当前你的 wifi ssid
#define WIFI_PASS "xxx"   // 使用时请修改为当前你的 wifi 密码

#define ledPin 14                          // 定义ledPin连接到GPIO14
//年
byte Nian[8] = {0b01000,0b01111,0b10010, 0b01111, 0b01010, 0b11111, 0b00010, 0b00010};
//月
byte Yue[8] = {0b01111, 0b01001, 0b01111, 0b01001, 0b01111, 0b01001, 0b10011, 0b00001};
//日
byte Ri[8] = {0b01111, 0b01001, 0b01001, 0b01111, 0b01001, 0b01001, 0b01111, 0b00000};
//摄氏度
byte du[8] = {0b11000,0b11011,0b00100, 0b00100, 0b00100, 0b00100, 0b00011, 0b00000};
// This clock is in the Mountain Time Zone
// Change this for your timezone
// 北京时间时区
#define STD_TIMEZONE_OFFSET +8    // Standard Time offset (-7 is mountain time)

// ***************************************************************
// TimeZone and Daylight Savings Time Rules
// ***************************************************************

// Define daylight savings time rules for the China
TimeChangeRule mySTD = {"", First,  Sun, Jan, 0, STD_TIMEZONE_OFFSET * 60};
Timezone myTZ(mySTD, mySTD);

WiFiClient client;

// This function is called once a second
void updateDisplay(void) {

  TimeChangeRule *tcr;        // Pointer to the time change rule

  // Read the current UTC time from the NTP provider
  time_t utc = now();

  // Convert to local time taking DST into consideration
  time_t localTime = myTZ.toLocal(utc, &tcr);
  int hhhh = dht.readHumidity();
  int tttt = dht.readTemperature();
  // Map time to pixel positions
  int weekdays=   weekday(localTime);
  int days    =   day(localTime);
  int months  =   month(localTime);
  int years   =   year(localTime);
  int seconds =   second(localTime);
  int minutes =   minute(localTime);
  int hours   =   hour(localTime) ;   //12 hour format use : hourFormat12(localTime)  isPM()/isAM()
  Serial.println("");
  Serial.print("Current local time:");
  Serial.print(days);
  Serial.print("/");
  Serial.print(months);
  Serial.print("/");
  Serial.print(years);
  Serial.print(" - ");
  Serial.print(hours);
  Serial.print(":");
  Serial.print(minutes);
  Serial.print(":");
  Serial.print(seconds);
  Serial.print(" - ");
  Serial.print(dayStr(weekdays));
  Serial.println("");
  
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print(years);
 lcd.write(byte(0));
 if(months<10)
        {
          lcd.print("0");
        }
 lcd.print(months);
 lcd.write(byte(1));
 if(days<10)
        {
          lcd.print("0");
        }
 lcd.print(days);
 lcd.write(byte(2));
 lcd.print(" "); 
 printWeek(weekdays);
 
  lcd.setCursor(0,1);
if(hours<10)
        {
          lcd.print("0");
        }
 lcd.print(hours);
 lcd.print(":");
 if(minutes<10)
        {
          lcd.print("0");
        }
 lcd.print(minutes);
 lcd.print(":");
 if(seconds<10)
        {
          lcd.print("0");
        }
 lcd.print(seconds);
 lcd.print(" ");
 lcd.print(tttt);
        lcd.write(byte(3));
        lcd.print(" "); 
        lcd.print(hhhh);
        lcd.print("%");
 
 
}

void setup() {

  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  delay(10);
  lcd.begin(16, 2);
    // Switch on the backlight
   lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
   lcd.setBacklight(LED_ON);
    lcd.createChar(0, Nian);
    lcd.createChar(1, Yue);
    lcd.createChar(2, Ri);
    lcd.createChar(3, du);
     dht.begin();
  // We start by connecting to a WiFi network
  initNTP(WIFI_SSID, WIFI_PASS);
}

// Previous seconds value
time_t previousSecond = 0;

void loop() {
  //  Update the display only if time has changed
  if (timeStatus() != timeNotSet) {
    if (second() != previousSecond) {
      previousSecond = second();
      // Update the display
      updateDisplay();
    }
  }
  
 
  delay(1000);
}

void printWeek(int week)
{
  switch(week)
  {
    case 1: lcd.print("Sun");break;
    case 2: lcd.print("Mon");break;
    case 3: lcd.print("Tue");break;
    case 4: lcd.print("Wed");break;
    case 5: lcd.print("Thr");break;
    case 6: lcd.print("Fri");break;
    case 7: lcd.print("Sat");break;
    default: lcd.print("E");break;
  } 
}
NTP.h以及库文件见:https://blog.readgroup.cn/post/76
IIC1602的使用及库文件见:https://blog.readgroup.cn/post/72


效果图:

微信截图_20180805233553.png

微信截图_20180805233608.png

温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,谢谢合作!
et_highlighter51

已有 0/3857 人参与

发表评论:

欢迎关注本站微信公众号~