代码全景
先给Mixly的全部模块放出来

实际效果

使用材料

  • ESP8266
  • 2004LCD液晶屏
  • 电位器(用于调整液晶显示对比度)
  • 面包板
  • 杜邦线

程序流程图

接线图

开发板不一样问题不大,IO口都是一样的,图里的1602与2004接口是相同的,仅供参考。

使用的库

#include <LiquidCrystal.h>
#include <WiFiManager.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <ESP8266WiFi.h>
#include <ESP8266_Seniverse.h>
#include <SimpleTimer.h>

部分代码

时间刷新函数

void timeRefresh() { /*在时间与日期小于10的时候int转String出来的是个位数不美观,手动+0*/
  if (NTP.getDateMonth() < 10) {
    monthrd = String("0") + String(NTP.getDateMonth());

  } else {
    monthrd = NTP.getDateMonth();

  }
  if (NTP.getDateDay() < 10) {
    dayrd = String("0") + String(NTP.getDateDay());

  } else {
    dayrd = NTP.getDateDay();

  }
  if (NTP.getTimeHour24() < 10) {
    hourrd = String("0") + String(NTP.getTimeHour24());

  } else {
    hourrd = NTP.getTimeHour24();

  }
  if (NTP.getTimeMinute() < 10) {
    minuterd = String("0") + String(NTP.getTimeMinute());

  } else {
    minuterd = NTP.getTimeMinute();

  }
  if (NTP.getTimeSecond() < 10) {
    secondrd = String("0") + String(NTP.getTimeSecond());

  } else {
    secondrd = NTP.getTimeSecond();

  }
  Daterd = String("     ") + String(String(NTP.getDateYear()) + String("-")) + String(String(monthrd) + String("-")) + String(dayrd);
  Timerd = String("      ") + String(String(hourrd) + String(":")) + String(String(minuterd) + String(":")) + String(secondrd);
}

LCD刷新

void lcdFresh() {
  mylcd.clear();
  mylcd.setCursor(1-1, 1-1);
  mylcd.print(Daterd);
  mylcd.setCursor(1-1, 2-1);
  mylcd.print(Timerd);
  mylcd.setCursor(1-1, 3-1);
  mylcd.print(Weatherrd);
  mylcd.setCursor(1-1, 4-1);
  mylcd.print(Temprd);
}

天气刷新

void weatherFresh() { /*LCD2004不支持显示中文,所以要获取天气代码并从字符串数组中获取天气现象*/
  if (weatherNow.update()) { 
    Weatherrd = String("Now:") + String(weatherCode[weatherNow.getWeatherCode()]);
  }
  if (weatherNow.update()) {
    Temprd = String("Temp:") + String(String(weatherNow.getDegree()) + String(" Degree"));
  }
}

全部代码

#include <LiquidCrystal.h>
#include <WiFiManager.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <ESP8266WiFi.h>
#include <ESP8266_Seniverse.h>
#include <SimpleTimer.h>

String weatherCode[]={"Sunny", "Clear", "Fair", "Fair", "Cloudy", "Partly Cloudy", "Partly Cloudy", "Mostly Cloudy", "Mostly Cloudy", "Overcast", "Shower", "Thundershower", "Thundershower Hail", "Light Rain", "Moderate Rain", "Heavy Rain", "Storm", "Heavy Storm", "Severe Storm", "Ice Rain", "Sleet", "Snow Flurry", "Light Snow", "Moderate Snoe", "Heavy Snow", "Snow Storm", "Dust", "Sand", "Duststorm", "Sandstorm", "Foggy", "Haze", "Windy", "Blustery", "Hurricane", "Tropical Storm", "Tornado", "Cold", "Hot"};

String monthrd;
String dayrd;
String hourrd;
String minuterd;
String secondrd;
String Daterd;
String Timerd;
String Weatherrd;
String Temprd;
LiquidCrystal mylcd(0,2,4,5,12,13);
WiFiServer server(80);
int8_t timeZone = 8;
const PROGMEM char *ntpServer = "ntp1.aliyun.com";
WeatherNow weatherNow;
SimpleTimer timer;

void timeRefresh() {
  if (NTP.getDateMonth() < 10) {
    monthrd = String("0") + String(NTP.getDateMonth());

  } else {
    monthrd = NTP.getDateMonth();

  }
  if (NTP.getDateDay() < 10) {
    dayrd = String("0") + String(NTP.getDateDay());

  } else {
    dayrd = NTP.getDateDay();

  }
  if (NTP.getTimeHour24() < 10) {
    hourrd = String("0") + String(NTP.getTimeHour24());

  } else {
    hourrd = NTP.getTimeHour24();

  }
  if (NTP.getTimeMinute() < 10) {
    minuterd = String("0") + String(NTP.getTimeMinute());

  } else {
    minuterd = NTP.getTimeMinute();

  }
  if (NTP.getTimeSecond() < 10) {
    secondrd = String("0") + String(NTP.getTimeSecond());

  } else {
    secondrd = NTP.getTimeSecond();

  }
  Daterd = String("     ") + String(String(NTP.getDateYear()) + String("-")) + String(String(monthrd) + String("-")) + String(dayrd);
  Timerd = String("      ") + String(String(hourrd) + String(":")) + String(String(minuterd) + String(":")) + String(secondrd);
}

void weatherFresh() {
  if (weatherNow.update()) {
    Weatherrd = String("Now:") + String(weatherCode[weatherNow.getWeatherCode()]);

  }
  if (weatherNow.update()) {
    Temprd = String("Temp:") + String(String(weatherNow.getDegree()) + String(" Degree"));

  }
}

void Simple_timer_1() {
  timeRefresh();
  lcdFresh();
}

void lcdFresh() {
  mylcd.clear();
  mylcd.setCursor(1-1, 1-1);
  mylcd.print(Daterd);
  mylcd.setCursor(1-1, 2-1);
  mylcd.print(Timerd);
  mylcd.setCursor(1-1, 3-1);
  mylcd.print(Weatherrd);
  mylcd.setCursor(1-1, 4-1);
  mylcd.print(Temprd);
}

void Simple_timer_2() {
  weatherFresh();
}

void lcdFail() {
  mylcd.clear();
  mylcd.setCursor(1-1, 1-1);
  mylcd.print("Failed to Connect.");
  mylcd.setCursor(1-1, 2-1);
  mylcd.print("Trying to reconnect.");
  mylcd.setCursor(1-1, 3-1);
  mylcd.print("Please check your");
  mylcd.setCursor(1-1, 4-1);
  mylcd.print("WiFi config.");
}

void setup(){
  monthrd = "N/A";
  dayrd = "N/A";
  hourrd = "N/A";
  minuterd = "N/A";
  secondrd = "N/A";
  Daterd = "N/A";
  Timerd = "N/A";
  Weatherrd = "N/A";
  Temprd = "N/A";
  Serial.begin(9600);
  mylcd.begin(20,4);
  WiFi.mode(WIFI_STA);
  WiFiManager wm;
  bool res;
  res=wm.autoConnect();
  NTP.setInterval (600);
  NTP.setNTPTimeout (1500);
  NTP.begin (ntpServer, timeZone, false);
  weatherNow.config("Secret_key", "zhengzhou", "c", "zh-Hans");
  mylcd.setCursor(1-1, 1-1);
  mylcd.print("Hello World");
  mylcd.setCursor(1-1, 2-1);
  mylcd.print("This is 2004 LCD");
  mylcd.setCursor(1-1, 3-1);
  mylcd.print("Displaying");
  mylcd.setCursor(1-1, 4-1);
  mylcd.print("Wangzi Studio");
  delay(1000);
  timeRefresh();
  weatherFresh();
  timer.setInterval(1000L, Simple_timer_1);

  timer.setInterval(60000L, Simple_timer_2);

}

void loop(){

  timer.run();

}