#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Wire.h>
#include <NTPClient.h> // Include NTPClient library
#include <TimeLib.h> // Include Arduino time library
#include <LiquidCrystal_I2C.h> // Include LiquidCrystal_I2C library
LiquidCrystal_I2C lcd(0x3f, 16, 2); // Configure LiquidCrystal_I2C library with 0x27 address, 16 columns and 2 rows
const char *ssid = "无线名称";
const char *password = "密码";
WiFiUDP ntpUDP;
// 'time.nist.gov' is used (default server) with +8 hour offset (28800 seconds) 60 seconds (60000 milliseconds) update interval
NTPClient timeClient(ntpUDP, "time.nist.gov", 28800, 60000);
char Time[] = "TIME:00:00:00";
char Date[] = "DATE:00/00/2000";
byte last_second, second_, minute_, hour_, day_, month_;
int year_;
void setup() {
Serial.begin(115200);
Wire.begin(0,2);
lcd.begin(); // Initialize I2C LCD module (SDA = GPIO0, SCL = GPIO2)
lcd.backlight(); // Turn backlight ON
lcd.setCursor(0, 0);
lcd.print(Time);
lcd.setCursor(0, 1);
lcd.print(Date);
WiFi.begin(ssid, password);
Serial.print("Connecting.");
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
Serial.println("connected");
timeClient.begin();
}
void loop() {
timeClient.update();
unsigned long unix_epoch = timeClient.getEpochTime(); // Get Unix epoch time from the NTP server
second_ = second(unix_epoch);
if (last_second != second_) {
minute_ = minute(unix_epoch);
hour_ = hour(unix_epoch);
day_ = day(unix_epoch);
month_ = month(unix_epoch);
year_ = year(unix_epoch);
Time[12] = second_ % 10 + 48;
Time[11] = second_ / 10 + 48;
Time[9] = minute_ % 10 + 48;
Time[8] = minute_ / 10 + 48;
Time[6] = hour_ % 10 + 48;
Time[5] = hour_ / 10 + 48;
Date[8] = day_ / 10 + 48; //日
Date[9] = day_ % 10 + 48; //日
Date[5] = month_ / 10 + 48; //月
Date[6] = month_ % 10 + 48; //月
Date[13] = (year_ / 10) % 10 + 48;
Date[14] = year_ % 10 % 10 + 48;
// Send time and date to serial monitor
Serial.println(Time);
Serial.println(Date);
// Display time and date on the 16x2 LCD
lcd.setCursor(0, 0);
lcd.print(Time);
lcd.setCursor(0, 1);
lcd.print(Date);
last_second = second_;
}
delay(200);
}
ESP-01应使用3.3V供电
LCD1602应使用5V供电
实际用的3.7V电池,饱和时电压4.0V+
运行时发热,比在面包板上标准供电时高.暂时没有烧毁,考虑给ESP-01加一个3.3V的降压板.