米思齐tft_spi模块库演示程序。心知天气、阿里云时钟、WiFi信号强度检测、1分钟滚屏、更新天气时间为15分钟、加入天气图标。更新天气次数。断网检测 。此程序为tft_eSPI图形库演示、如感觉好可以自行优化。
ili9431tft_espi库是用于ESP32和ESP8266芯片的TFT LCD驱动程序库,可用于控制ILI9341和ILI9431 TFT显示屏。以下是配置ili9431tft_espi库的步骤:
-
下载并安装Arduino IDE。
-
在Arduino IDE中,导航到“文件”>“首选项”>“附加开发板管理器网址”并输入以下网址:
https://dl.espressif.com/dl/package_esp32_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json
-
点击“确定”按钮,关闭“首选项”窗口。
-
导航到“工具”>“开发板”>“开发板管理器”。
-
在搜索栏中输入“ESP32”或“ESP8266”,然后选择相应的开发板并安装。
-
连接ILI9341或ILI9431 TFT显示屏到ESP32或ESP8266芯片。
-
在Arduino IDE中,打开一个示例程序(例如:ILI9431test)。
-
在示例程序中,确保指定正确的引脚连接和显示屏类型:
#define TFT_DC 15
#define TFT_CS 14
#define TFT_RST -1
#define TFT_MOSI 23
#define TFT_CLK 18
#define TFT_MISO -1ESP_ILI9341 tft = ESP_ILI9341(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_CLK, TFT_MISO);
-
上传示例程序到开发板。
-
测试ILI9341或ILI9431 TFT显示屏是否能够正常工作。
程序图
完整代码如下:
#include <zh_font20.h>
#include <FxLED_32.h>
#include <weathz.h>
#include <textmatrix.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include <ESP8266WiFi.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <TJpg_Decoder.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
volatile int shu;
volatile int start_xz;
volatile unsigned long dingshi_xz;
volatile unsigned long now_xz;
volatile int code_day;
volatile int code_ye;
volatile bool clear;
int8_t timeZone = 8;
const PROGMEM char *ntpServer = "ntp1.aliyun.com";
char x_map[4] = "" ;
char x_night[12] = "" ;
char code_night[4] = "" ;
char x_day[12] = "" ;
char iconday[4] = "" ;
char x_wdhigh[6] = "" ;
char x_wdlow[6] = "" ;
char x_humidity[5] = "" ;
char x_wind_direction[20] = "" ;
char x_direction_degree[5] = "" ;
char x_scale[3] = "" ;
char x_speed[5] = "" ;
char x_rainfall[6] = "" ;
char x_precip[6] = "" ;
TFT_eSPI tft = TFT_eSPI();
bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap)
{
if (y >= tft.height()) return 0;
tft.pushImage(x, y, w, h, bitmap);
return 1;
}
void dispaly_clear() {
if (clear == false) {
tft.fillScreen(TFT_BLACK);
clear = true;
}
}
void nowifi() {
int num = 0;
while (!(WiFi.status() == WL_CONNECTED)) {
dispaly_clear();
tft.drawRect(28,98,180,11,TFT_WHITE);
tft.fillRect(30,100,num,7,TFT_YELLOW);
tft.loadFont(zh_font20);
tft.setCursor(70,120);
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.println("网络失败!!");
tft.setCursor(60,150);
tft.print("请配置网络!!!");
tft.unloadFont();//卸载字库占用内存
delay(80);
num++;
if (num >= 178) {
num = 0;
tft.fillScreen(TFT_BLACK);
}
if (WiFi.status() == WL_CONNECTED == 1) {
tft.fillScreen(TFT_BLACK);
break;
}
}
}
void shibai() {
tft.fillScreen(TFT_BLACK);
tft.loadFont(zh_font20);
tft.setCursor(30,120);
tft.setTextColor(TFT_PURPLE,TFT_BLACK);
tft.print("请配置 wifi 网络");
tft.unloadFont();//卸载字库占用内存
}
String getzhenshu(int zhs) {
String s = "";
if (zhs < 10) {
s = String("0") + String(zhs);
} else {
s = zhs;
}
return s;
}
uint8_t getwifiriis() {
uint32_t db = WiFi.RSSI();
if (db <= -100) {
return 0;
} else if (db >= -50) {
return 100;} else {
return (2 * (db + 100));
}
return db;
}
String getday_yyr() {
String s = "";
s = String(NTP.getDateYear()) + String("年") + String(getzhenshu(NTP.getDateMonth())) + String("月") + String(getzhenshu(NTP.getDateDay())) + String("日");
return s;
}
String gettime_m() {
String s = "";
s = String(getzhenshu(NTP.getTimeSecond()));
return s;
}
String gettime_hf() {
String s = "";
s = String(getzhenshu(NTP.getTimeHour24())) + String(":") + String(getzhenshu(NTP.getTimeMinute()));
return s;
}
String getweek() {
String s = "";
switch (NTP.getDateWeekday()) {
case 1:
s = "星期一";
break;
case 2:
s = "星期二";
break;
case 3:
s = "星期三";
break;
case 4:
s = "星期四";
break;
case 5:
s = "星期五";
break;
case 6:
s = "星期六";
break;
case 7:
s = "星期日";
break;
default:
break;
}
return s;
}
TFT_eSprite spr = TFT_eSprite(&tft);
TFT_eSprite clk = TFT_eSprite(&tft);
void tft_weathr() {
tft.setTextColor(TFT_SILVER ,TFT_BLACK);
tft.setCursor(0,1,1);
tft.print(String("up data:") + String(shu));
tft.loadFont(zh_font20);
tft.setTextColor(TFT_YELLOW,TFT_BLACK);
tft.drawString(String("白天:") + String(x_day),1,155);
tft.drawString(String("夜间:") + String(x_night),120,155);
tft.drawString(String("高温:") + String(String(x_wdhigh) + String("℃")),1,176);
tft.drawString(String("低温:") + String(String(x_wdlow) + String("℃")),120,176);
tft.drawString(String("湿度:") + String(x_humidity),1,197);
tft.drawString(String("等级:") + String(x_scale),120,197);
tft.drawString(String("风速:") + String(x_speed),1,218);
tft.drawString(String("角度:") + String(x_direction_degree),120,218);
tft.drawString(String("雨量:") + String(x_rainfall),1,239);
tft.drawString(String("雨概率:") + String(x_precip),120,239);
tft.drawString(String("风向:") + String(x_wind_direction),1,260);
tft.unloadFont();//卸载字库占用内存
TJpgDec.drawJpg(15,96,weatherico(code_day), sizeof(wico10));
TJpgDec.drawJpg(130,96,weatherico(code_ye), sizeof(wico10));
}
void weathr_up() {
// 40分钟更新一次
if (millis() - dingshi_xz > 1500000 || start_xz == 0) {
dingshi_xz = millis();
shu++;
String weather;
WiFiClient client;
HTTPClient http;
BearSSL::WiFiClientSecure wificlient;
wificlient.setInsecure();
http.begin(client,"http://api.seniverse.com/v3/weather/daily.json?key=你的心知天气密钥&location=ip&language=zh-Hans&unit=c&start=0&days=1");
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d", httpCode);
Serial.println();
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
weather += payload;
}
}
http.end();
delay(10);
StaticJsonDocument<1024> doc;
DeserializationError error = deserializeJson(doc,weather);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
JsonObject results_0 = doc["results"][0];;
JsonObject results_0_location = results_0["location"];;
strcpy(x_map , results_0_location["name"]);
JsonObject results_0_daily_0 = results_0["daily"][0];;
strcpy(x_day , results_0_daily_0["text_day"]);
strcpy(iconday , results_0_daily_0["code_day"]);
strcpy(x_night , results_0_daily_0["text_night"]);
strcpy(code_night , results_0_daily_0["code_night"]);
strcpy(x_wdhigh , results_0_daily_0["high"]);
strcpy(x_wdlow , results_0_daily_0["low"]);
strcpy(x_humidity , results_0_daily_0["humidity"]);
// 风向 东南西北风
// 风向文字
strcpy(x_wind_direction , results_0_daily_0["wind_direction"]);
// 风向角度,范围0~360
strcpy(x_direction_degree , results_0_daily_0["wind_direction_degree"]);
// 风力等级
strcpy(x_scale , results_0_daily_0["wind_scale"]);
// 风速/m
strcpy(x_speed , results_0_daily_0["wind_speed"]);
// 降水量,0.00 单位mm
strcpy(x_rainfall , results_0_daily_0["rainfall"]);
// 降水量,0.00 单位mm
strcpy(x_precip , results_0_daily_0["precip"]);
tft.fillScreen(TFT_BLACK);
start_xz = 1;
if (NTP.getTimeHour24() == 0) {
shu = 0;
}
}
}
void time_man() {
clk.setColorDepth(8);
clk.loadFont(zh_font20);
clk.createSprite(140,21);
clk.fillSprite(TFT_BLACK);
clk.setTextDatum(CC_DATUM);
clk.setTextColor(TFT_YELLOW,TFT_BLACK);
clk.drawString((getday_yyr()),68,11);
clk.pushSprite(50,14);
clk.deleteSprite();
clk.unloadFont();
clk.setColorDepth(8);
clk.setTextFont(7);
clk.createSprite(135,48);
clk.fillSprite(TFT_BLACK);
clk.setTextDatum(CC_DATUM);
clk.setTextColor(TFT_GREEN,TFT_BLACK);
clk.drawString((gettime_hf()),67,24);
clk.pushSprite(20,42);
clk.deleteSprite();
clk.unloadFont();
clk.setColorDepth(8);
clk.loadFont(zh_font20);
clk.createSprite(61,21);
clk.fillSprite(TFT_BLACK);
clk.setTextDatum(CC_DATUM);
clk.setTextColor(TFT_WHITE,TFT_BLACK);
clk.drawString((getweek()),30,11);
clk.pushSprite(173,40);
clk.deleteSprite();
clk.unloadFont();
clk.setColorDepth(8);
clk.loadFont(FxLED_32);
clk.createSprite(38,30);
clk.fillSprite(TFT_BLACK);
clk.setTextDatum(CC_DATUM);
clk.setTextColor(TFT_ORANGE,TFT_BLACK);
clk.drawString((gettime_m()),19,16);
clk.pushSprite(160,59);
clk.deleteSprite();
clk.unloadFont();
}
void wifi_riis() {
spr.setColorDepth(8);
spr.createSprite(10,10);
spr.fillSprite(TFT_BLACK);
spr.setTextDatum(CC_DATUM);
for(int i = 0;i<4;i ++){
for(int j = 0;j<(2 * (i + 1));j++){
if (getwifiriis() > i * 25 || j == 0) {
spr.drawPixel((2 + 2 * i),(8 - j),TFT_GREEN);
}
}
}
spr.pushSprite(231,1);
spr.deleteSprite();
clk.setColorDepth(8);
clk.setTextFont(1);
clk.createSprite(22,10);
clk.fillSprite(TFT_BLACK);
clk.setTextDatum(CC_DATUM);
clk.setTextColor(TFT_WHITE,TFT_BLACK);
clk.drawString(String(((String)(getwifiriis()))) + String("%"),11,5);
clk.pushSprite(207,1);
clk.deleteSprite();
clk.unloadFont();
}
void setup(){
shu = 0;
start_xz = 0;
dingshi_xz = 0;
now_xz = 0;
code_day = 0;
code_ye = 0;
clear = false;
Serial.begin(9600);
tft.begin();
tft.setRotation(0);
tft.fillScreen(TFT_BLACK);
tfts.init();
setupScrollArea(TOP_FIXED_AREA, BOT_FIXED_AREA);
WiFi.mode(WIFI_STA);
int cnt = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (cnt++ >= 10){
WiFi.beginSmartConfig();
while (1) {
delay(1000);
if (WiFi.smartConfigDone()) {
Serial.println();
Serial.println("SmartConfig: Success");
break;
}
Serial.print("|");
shibai();//无判断条件:可不创建函数。加判断条件:请创建shibai();。
}
}
}
WiFi.printDiag(Serial);
Serial.println(WiFi.localIP());
Serial.println("网络配置成功。可以上网啦!");
NTP.setInterval (600);
NTP.setNTPTimeout (1500);
NTP.begin (ntpServer, timeZone, false);
TJpgDec.setJpgScale(1);
TJpgDec.setSwapBytes(true);
TJpgDec.setCallback(tft_output);
}
void loop(){
code_day = atoi(iconday);
code_ye = atoi(code_night);
nowifi();
wifi_riis();
weathr_up();
time_man();
tft_weathr();
clear = false;
if (String((gettime_m())).equals(String("00"))) {
scroll_slow(62,30);
}
}