【Follow me第二季第4期】一些联网获取的信息展示屏
[复制链接]
本帖最后由 zsy-s 于 2024-12-24 21:31 编辑
备忘
比较好的官方讲解:Installing a Board Package in the IDE 2 | Arduino Documentation
字体使用:Using Fonts | Adafruit GFX Graphics Library | Adafruit Learning System
在线创建字体: truetype2gfx - Converting fonts from TrueType to Adafruit GFX
解码汉字正确的网址:UTF-8编码转换器-ME2在线工具
背景
有一个tft0.96的屏幕,可以从网络获取一些信息,用于展示。
学习了几天汉字的显示,无奈水平不够,资料不多,不能显示汉字出来,所以遇到汉字的地方,做个判断,转换为英文显示。
程序结构
代码
开启无线网卡
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
Serial.println(fv);
// attempt to connect to Wifi network:
status = WiFi.begin(ssid, pass);
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
// wait 10 seconds for connection:
delay(1000);
}
Serial.println("Connected to wifi");
printWifiStatus();
连接wifi,准备使用获取天气信息接口
// if you get a connection, report back via serial:
if (client.connect(server, port)) {
Serial.print("connected to server ");
client.print("GET /v3/weather/weatherInfo?key=1682d31d8732a44f929068e6083c49ed&city=210200&extensions=all HTTP/1.1\r\n");
client.println("Host: restapi.amap.com");
client.println("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
client.println("Connection: close");
client.print("Upgrade-Insecure-Requests: 1\r\n");
client.println();
Serial.println("send data to server over!!");
}
初始化tft屏幕
tft.initR(INITR_MINI160x80_PLUGIN);
tft.setRotation(1);
tft.fillScreen(ConvertRGB(0x7F,0xFF,0xD4));
tft.fillRect(80,0,10,20,ST77XX_YELLOW);
获取 信息,并解析需要的信息
while (client.available()) {
dataWeb[posi] = client.read();
posi++;
}
Serial.write(dataWeb);
updateTm = strstr(dataWeb,"reporttime")+13;
date1 = strstr(updateTm,"date")+7;
week1 = strstr(date1,"week")+7;
dayweather = strstr(week1,"dayweather")+13;
temp1 = strstr(dayweather,"daytemp")+10;
temp2 = strstr(temp1,"nighttemp")+12;
buff = strchr(updateTm,'"');
buff[0] = 0;
buff = strchr(date1,'"');
buff[0] = 0;
buff = strchr(week1,'"');
buff[0] = 0;
buff = strchr(dayweather,'"');
buff[0] = 0;
buff = strchr(temp1,'"');
buff[0] = 0;
buff = strchr(temp2,'"');
buff[0] = 0;
Serial.println();
Serial.print("updateTm:");
Serial.println(updateTm);
Serial.print("date1:");
Serial.println(date1);
Serial.print("week1:");
Serial.println(week1);
Serial.print("dayweather:");
Serial.println(dayweather);
Serial.println(dayweather[0],HEX);
Serial.println(dayweather[1],HEX);
Serial.println(dayweather[2],HEX);
Serial.print("temp1:");
Serial.println(temp1);
Serial.print("temp2:");
Serial.println(temp2);
if(dayweather[0] == 0xe6){
dayweather = "sunshine";
}else if(dayweather[0] == 0xe5){
dayweather = "too cloudy";
}else{
dayweather = "Snowbound";
}
展示信息到tft屏幕
tft.setFont(&songti5pt7b);
tft.setTextWrap(true);
tft.setCursor(10, 30);
tft.setTextColor(ST77XX_BLACK);
tft.setTextSize(2);
Serial.println("\r\n***************************************************************");
writeText(0,"updateTm",updateTm);
writeText(13,"date",date1);
writeText(26,"week",week1);
writeText(39,"dayweather",dayweather);
writeText(52,"daytemp",temp1);
writeText(65,"nighttemp",temp2);
delay(6000000);
疑难杂症
看了几篇备忘的字体汉字博文,不会用,生成的字体显示不出汉字,都是空字符,难搞。
这个问题,待后期继续研究研究。
github上有个台湾省的人,弄了个繁体字的,教程还挺繁琐,感觉靠谱。
效果图
IMG_1553
代码
结语
2040 Nano的紧凑设计使其特别适合用于空间有限的嵌入式项目。这一点对于物联网应用、便携式设备、教育领域等都是一个很大的优势。尽管体积小巧,但它依然提供了强大的功能和出色的性能,是一款性价比极高的开发工具。再次感谢EEWorld平台,让我们有机会深入体验这款出色的开发工具,并与更多开发者一起交流与学习。EEWorld的平台让我们得以接触到更多的优质硬件和工具,是广大开发者不可或缺的宝贵资源。
|