電子發(fā)燒友App

硬聲App

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>在圖形LCD Phidg??et(LCD1100)上顯示天氣

在圖形LCD Phidg??et(LCD1100)上顯示天氣

2023-06-15 | zip | 0.00 MB | 次下載 | 免費

資料介紹

描述

?

?

在這個項目中,我們將使用在線天氣服務在圖形 LCD Phidg??et (LCD1100) 上顯示當?shù)靥鞖夂蜁r間。此項目的 C#Python 代碼可用。

補給品

使用以下硬件

  • VINT 集線器 Phidg??et (HUB0000_0)
  • 圖形 LCD Phidg??et (LCD1100_0)

第 1 步:設置

通過任何端口將您的圖形 LCD Phidg??et 連接到您的 VINT 集線器。

第 2 步:概述

?
pYYBAGNkXQSACzy4AAAxZRT-LG4102.jpg
?

如上所示,這個項目有兩個主要部分:

  • 訪問天氣數(shù)據(jù)
  • 顯示天氣數(shù)據(jù)

讓我們先來看看訪問數(shù)據(jù)。

第 3 步:訪問天氣數(shù)據(jù)

?
pYYBAGNkXQeAUiTdAABbz6ao2xU568.png
?

我們將使用OpenWeather訪問我們當?shù)氐奶鞖忸A報。他們提供免費訪問超過 200,000 個城市的天氣預報。支持以下格式:

  • JSON
  • XML
  • HTML

對于這個項目,我們將使用 XML 格式。

為了訪問天氣數(shù)據(jù),您必須創(chuàng)建一個免費帳戶并獲取 API 密鑰。

第 4 步:創(chuàng)建您的帳戶

?
poYBAGNkXQmAVfAGAAFvprnjruo663.png
?

按照此鏈接創(chuàng)建免費帳戶并獲取 API 密鑰。獲取 API 密鑰后,您可以通過以下 URL 訪問數(shù)據(jù):

api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}

將 {city name} 替換為您的城市,將 {API key} 替換為您的 API 密鑰。例如,這就是我們的樣子:

http://api.openweathermap.org/data/2.5/weather?q=Calgary&APPID=fakeid111111111111111111111111111&units=metric&mode=xml

嘗試在任何網(wǎng)絡瀏覽器中輸入您的 URL。您將看到 XML 格式的天氣數(shù)據(jù),如上所示。下一步是創(chuàng)建一個可以解析數(shù)據(jù)以便顯示的程序。

第 5 步:讀取/解析天氣數(shù)據(jù) - C#

對于 C#,您可以使用XMLTextReader類來快速解析 XML 數(shù)據(jù):

static string readXML(string arg1, string arg2)
{
String URLString = "http://api.openweathermap.org/data/2.5/weather?q=Calgary&APPID=fakeid111111111111111111111111111&units=metric&mode=xml";
XmlTextReader reader = new XmlTextReader(URLString);
reader.ReadToFollowing(arg1);
reader.MoveToAttribute(arg2);
return reader.Value;
}

您可以像這樣使用上面的函數(shù):

static void updateWeather(LCD lcd) {
string city = readXML("city", "name");
string temperature = readXML("temperature", "value");
string humidity = readXML("humidity", "value");
string windspeed = readXML("speed", "value");
string descript = readXML("weather", "value");
string iconID = readXML("weather", "icon");
}

還有其他可用的值(日出時間、日落時間、壓力等),但對于這個項目,我們將只顯示值的子集。

第 6 步:讀取/解析天氣數(shù)據(jù) - Python

對于 Python,您可以使用 ElementTree XML API 快速解析數(shù)據(jù):

import xml.etree.ElementTree as ET
from urllib.request import urlopen
url = urlopen('http://api.openweathermap.org/data/2.5/weather?q=Calgary&APPID=fakeid111111111111111111111111111&units=metric&mode=xml')
tree = ET.parse(url)
root = tree.getroot()
def readXML(arg1, arg2):
for item in root.iter(arg1):
return item.get(arg2)
print(readXML('city','name'))
print(readXML('temperature','value'))
print(readXML('humidity','value'))
print(readXML('speed','value'))
print(readXML('weather','value'))
print(readXML('weather','icon'))

還有其他可用的值(日出時間、日落時間、壓力等),但對于這個項目,我們將只顯示值的子集。

第 7 步:OpenWeather 圖標

您可能已經(jīng)注意到我們在上面存儲了iconID 。此值表示描述當前天氣的圖像。您可以在此處查看圖標。Graphic LCD Phidg??et 能夠顯示位圖,我們可以在我們的程序中輕松實現(xiàn)這些圖標。如果您還沒有探索過圖形 LCD Phidg??et 上的位圖,請查看這個項目。

網(wǎng)上有很多像素藝術程序,我們使用了 Piskel。由于簡單的“導出到 C 文件”選項,重新創(chuàng)建 OpenWeatherMap 圖標很容易。位圖的結果在下面的 github 存儲庫中提供。

第 8 步:顯示數(shù)據(jù) - C#

現(xiàn)在我們已經(jīng)收集了天氣數(shù)據(jù),最后一步是將其顯示在圖形 LCD Phidg??et 上:

static void updateWeather(LCD lcd)
{
string city = readXML("city", "name");
string temperature = readXML("temperature", "value");
string humidity = readXML("humidity", "value");
string windspeed = readXML("speed", "value");
string descript = readXML("weather", "value");
string iconID = readXML("weather", "icon");
if (temperature.Length > 5)
{
temperature = temperature.Remove(5);
}
//Temperature box
int x = (44 - ((temperature.Length * 6) + 12)) / 2;
lcd.WriteText(LCDFont.Dimensions_6x12, x, 15, temperature);
lcd.WriteText(LCDFont.User1, x + temperature.Length * 6, 15, "0");
lcd.WriteText(LCDFont.Dimensions_6x12, x + temperature.Length * 6 + 6, 15, "C");
//Weather image + descript box
byte[] temp;
if (iconID == "01d")
temp = _01d;
else if (iconID == "02d")
temp = _02d;
else if (iconID == "03d")
temp = _03d;
else if (iconID == "04d")
temp = _04d;
else if (iconID == "09d")
temp = _09d;
else if (iconID == "10d")
temp = _10d;
else if (iconID == "11d")
temp = _11d;
else if (iconID == "13d")
temp = _13d;
else if (iconID == "50d")
temp = _50d;
else if (iconID == "01n")
temp = _01n;
else if (iconID == "02n")
temp = _02n;
else if (iconID == "10n")
temp = _10n;
else
temp = unknown;
lcd.WriteBitmap(2, 31, 32, 32, temp);
lcd.WriteText(LCDFont.Dimensions_5x8, 40, 42, descript);
//Extra info box
lcd.WriteText(LCDFont.Dimensions_5x8, 50, 11, "Humidity: " + humidity + "%");
lcd.WriteText(LCDFont.Dimensions_5x8, 50, 20, "Wind: " + windspeed + "km/h");
}
static void redraw(LCD lcd)
{
lcd.Clear();
//draw borders around outside
lcd.DrawLine(0, 0, 127, 0);
lcd.DrawLine(0, 0, 0, 63);
lcd.DrawLine(127, 0, 127, 63);
lcd.DrawLine(0, 63, 127, 63);
//draw borders inside
lcd.DrawLine(0, 10, 128, 10);
lcd.DrawLine(43, 10, 43, 30);
lcd.DrawLine(1, 30, 127, 30);
lcd.WriteText(LCDFont.Dimensions_5x8, 1, 1, DateTime.Now.ToString(" ddd, MMM d hh:mm:ss tt"));
updateWeather(lcd);
lcd.Flush();
}

下面是對上面代碼的快速回顧:

重繪

此函數(shù)在圖形 LCD 上繪制主要邊框。它還打印當前時間并調(diào)用 updateWeather 程序。

更新天氣

此功能將來自 OpenWeather 服務的數(shù)據(jù)排列到圖形 LCD 上。

第 9 步:顯示數(shù)據(jù) - Python

現(xiàn)在我們已經(jīng)收集了天氣數(shù)據(jù),最后一步是將其顯示在圖形 LCD Phidg??et 上:

def updateWeather():
city = readXML('city', 'name')
temperature = readXML('temperature', 'value')
humidity = readXML('humidity', 'value')
windspeed = readXML('speed', 'value')
descript = readXML('weather', 'value')
iconID = readXML('weather', 'icon')
if(len(temperature) > 5):
temperature = temperature[:-1:] #remove last char so it fits
#temperature box
x = (44 - ((len(temperature) * 6) + 12)) / 2
x = int(x) #force to int
lcd.writeText(LCDFont.FONT_6x12, x, 15, temperature)
lcd.writeText(LCDFont.FONT_User1, x + len(temperature) * 6, 15, "0")
lcd.writeText(LCDFont.FONT_6x12, x + len(temperature) * 6 + 6, 15, "C")
#Weather icon + descript box
temp = []
if(iconID == "01d"):
temp = _01d
elif(iconID == "02d"):
temp = _02d
elif (iconID == "03d"):
temp = _03d
elif (iconID == "04d"):
temp = _04d
elif (iconID == "09d"):
temp = _09d
elif (iconID == "10d"):
temp = _10d
elif (iconID == "11d"):
temp = _11d
elif (iconID == "13d"):
temp = _13d
elif (iconID == "50d"):
temp = _50d
elif (iconID == "01n"):
temp = _01n
elif (iconID == "02n"):
temp = _02n
elif (iconID == "10n"):
temp = _10n
else:
temp = unknown
lcd.writeBitmap(2, 31, 32, 32, temp)
lcd.writeText(LCDFont.FONT_5x8, 40, 42, descript)
#Extra info box
lcd.writeText(LCDFont.FONT_5x8, 50, 11, "Humidity: " + humidity + "%")
lcd.writeText(LCDFont.FONT_5x8, 50, 20, "Wind: " + windspeed + "km/h")
def redraw():
lcd.clear()
#Draw borders around outside
lcd.drawLine(0, 0, 127, 0)
lcd.drawLine(0, 0, 0, 63)
lcd.drawLine(127, 0, 127, 63)
lcd.drawLine(0, 63, 127, 63)
#draw borders inside
lcd.drawLine(0, 10, 128, 10)
lcd.drawLine(43, 10, 43, 30)
lcd.drawLine(1, 30, 127, 30)
timeStr = datetime.now().strftime("%a, %b %d %I:%M:%S %p")
lcd.writeText(LCDFont.FONT_5x8, 1, 1, timeStr)
updateWeather()
lcd.flush()

下面是對上面代碼的快速回顧:redraw

此函數(shù)在圖形 LCD 上繪制主要邊框。它還打印當前時間并調(diào)用 updateWeather 程序。

更新天氣

此功能將來自 OpenWeather 服務的數(shù)據(jù)排列到圖形 LCD 上。

第 10 步:主循環(huán)

最后要做的是創(chuàng)建一個主循環(huán),按設定的時間表更新 LCD。以下是我們的推薦

  • 每秒:更新液晶顯示屏上的時間
  • 每 15 分鐘:更新 LCD 上的天氣狀態(tài)

創(chuàng)建一個每秒循環(huán)的無限循環(huán)。創(chuàng)建一個計數(shù)器來跟蹤循環(huán),當計數(shù)器達到 900(900 秒是 15 分鐘)時更新天氣。

第 11 步:繼續(xù)前進

該項目的完整代碼可在此處獲得:https ://github.com/phidgeteer/LCDWeather.git

如果您有任何疑問,請在下面發(fā)表評論!


下載該資料的人也在下載 下載該資料的人還在閱讀
更多 >

評論

查看更多

下載排行

本周

  1. 1山景DSP芯片AP8248A2數(shù)據(jù)手冊
  2. 1.06 MB  |  532次下載  |  免費
  3. 2RK3399完整板原理圖(支持平板,盒子VR)
  4. 3.28 MB  |  339次下載  |  免費
  5. 3TC358743XBG評估板參考手冊
  6. 1.36 MB  |  330次下載  |  免費
  7. 4DFM軟件使用教程
  8. 0.84 MB  |  295次下載  |  免費
  9. 5元宇宙深度解析—未來的未來-風口還是泡沫
  10. 6.40 MB  |  227次下載  |  免費
  11. 6迪文DGUS開發(fā)指南
  12. 31.67 MB  |  194次下載  |  免費
  13. 7元宇宙底層硬件系列報告
  14. 13.42 MB  |  182次下載  |  免費
  15. 8FP5207XR-G1中文應用手冊
  16. 1.09 MB  |  178次下載  |  免費

本月

  1. 1OrCAD10.5下載OrCAD10.5中文版軟件
  2. 0.00 MB  |  234315次下載  |  免費
  3. 2555集成電路應用800例(新編版)
  4. 0.00 MB  |  33566次下載  |  免費
  5. 3接口電路圖大全
  6. 未知  |  30323次下載  |  免費
  7. 4開關電源設計實例指南
  8. 未知  |  21549次下載  |  免費
  9. 5電氣工程師手冊免費下載(新編第二版pdf電子書)
  10. 0.00 MB  |  15349次下載  |  免費
  11. 6數(shù)字電路基礎pdf(下載)
  12. 未知  |  13750次下載  |  免費
  13. 7電子制作實例集錦 下載
  14. 未知  |  8113次下載  |  免費
  15. 8《LED驅動電路設計》 溫德爾著
  16. 0.00 MB  |  6656次下載  |  免費

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935054次下載  |  免費
  3. 2protel99se軟件下載(可英文版轉中文版)
  4. 78.1 MB  |  537798次下載  |  免費
  5. 3MATLAB 7.1 下載 (含軟件介紹)
  6. 未知  |  420027次下載  |  免費
  7. 4OrCAD10.5下載OrCAD10.5中文版軟件
  8. 0.00 MB  |  234315次下載  |  免費
  9. 5Altium DXP2002下載入口
  10. 未知  |  233046次下載  |  免費
  11. 6電路仿真軟件multisim 10.0免費下載
  12. 340992  |  191187次下載  |  免費
  13. 7十天學會AVR單片機與C語言視頻教程 下載
  14. 158M  |  183279次下載  |  免費
  15. 8proe5.0野火版下載(中文版免費下載)
  16. 未知  |  138040次下載  |  免費