概述
將這個緊湊的PyPortal氣象站放在您的桌子上或固定在冰箱上,您將永遠知道大自然的作用!
使用CircuitPython,該項目可查詢Open Weather Maps網站API即可找到您所在位置的當前天氣,并與信息圖標一起顯示。
3D打印盒和磁性襯里可將其安裝到冰箱或其他黑色金屬表面上。
Adafruit PyPortal-CircuitPython支持的Internet顯示
產品ID:4116
PyPortal,我們易于使用的物聯網設備這樣您就可以在幾分鐘內為“物聯網”創建所有事物。制作自定義觸摸屏界面。..
$ 54.95
進貨
添加到購物車
產品ID:1994
我們的5V 2A USB電源適配器是為Raspberry Pi,BeagleBone或其他需要耗電的單板計算機供電的理想選擇! 。..
$ 7.95
入庫價
添加到購物車
USB A/微型電纜-2m
產品ID:2185
這是您的標準USB A-Plugto Micro-USB電纜。它的長度為2米,因此對于那些較長的擴展名,您將擁有足夠的繩索。
$ div 4.95
入庫價
添加到購物車
其他工具和材料
您可能想為PyPortal Weather Station創建可選的安裝盒。為此,您需要:
3D打印機
細絲
PyPortal Case模型文件
M2.5螺釘
如果您無權使用3D打印機,則可以選擇使用在線服務(例如3D集線器)按需為您打印。
磁性回銷
產品ID:1170
這些磁性回銷具有兩塊:一根帶有膠條的金屬棒和一塊帶有兩個強稀土磁體的塑料塊。將金屬欄粘貼到您的FLORA項目上-。..
$ 1.95
存貨
添加到購物車
黑色尼龍螺絲和固定套件– M2.5螺紋
產品ID:3299
該M2.5螺絲套件總共380件,是您工作站上的必備工具。您將有足夠的空間螺絲,螺母和六角形支腳來助您一臂之力。..
$ 16.95
進貨
添加到購物車
Ultimaker 2+ 3D打印機
產品ID:2673
Ultimaker 2+是我們市場上最喜歡的3D打印機之一。這是一臺構建良好的開源緊湊型計算機,具有出色的用戶體驗。每英寸。..
缺貨
缺貨
PLA燈絲3D打印機-直徑2.85毫米-丁香-1千克
產品ID:3733
擁有沒有燈絲的3D打印機就像是沒有紙張或墨水的普通打印機。雖然許多打印機都帶有一些燈絲,但很有可能。..
缺貨
缺貨股票
安裝CircuitPython
CircuitPython是MicroPython的派生版,旨在簡化低成本微控制器上的實驗和教學。它不需要任何前期桌面軟件下載,因此比以往任何時候都更容易獲得原型。只需復制并編輯 CIRCUITPY “閃存”驅動器上的文件即可進行迭代。
以下說明將向您展示如何安裝CircuitPython。如果您已經安裝了CircuitPython,但希望對其進行更新或重新安裝,則同樣的步驟也適用!
設置CircuitPython快速入門!
請遵循以下快速逐步操作以獲取超快速的Python功能:)
通過CircuitPython.org為該板下載最新版本的CircuitPython
點擊上面的鏈接,為PyPortal下載最新版本的CircuitPython。
下載并將其保存到您的桌面(或方便的任何地方)。
使用已知良好的USB電纜將PyPortal插入計算機。
A很多人最終使用僅可充電的USB電纜,這非常令人沮喪!因此,請確保您擁有知道對數據同步有用的USB電纜。
雙擊中間頂部頂部的重置按鈕(洋紅色箭頭)在板上,您將看到NeoPixel RGB LED(綠色箭頭)變為綠色。如果它變成紅色,請檢查USB電纜,嘗試使用另一個USB端口,等等。注意:USB接口旁邊的紅色小LED指示燈將呈紅色閃爍。沒關系!
如果第一次雙擊不起作用,請重試。有時可能需要一些嘗試才能使節奏正確!
您將看到一個名為 PORTALBOOT 。
將 adafruit-circuitpython-pyportal- .uf2 文件拖到 PORTALBOOT。
LED將閃爍。然后, PORTALBOOT 驅動器將消失,并且將出現一個名為 CIRCUITPY 的新磁盤驅動器。
如果未在板上添加任何代碼,唯一存在的文件是 boot_out.txt 。這是絕對正常的!是時候添加您的 code.py 并開始使用它了!
就這樣,您完成了! :)
PyPortal默認文件
單擊下面的按鈕,下載PyPortal附帶的文件的zip。
PyPortal默認文件
PyPortal CircuitPython設置
要在CircuitPython中使用PyPortal的所有出色功能,必須首先安裝許多庫。本頁涵蓋了該過程。
Adafruit CircuitPython捆綁包
下載Adafruit CircuitPython庫捆綁包。您可以在此處找到最新版本:
最新的Adafruit CircuitPython庫包
下載 adafruit-circuitpython-bundle-4。 x-mpy-*。zip 捆綁zip文件,然后解壓縮同名文件夾。在內部,您會找到一個 lib 文件夾。您有兩個選擇:
您可以將 lib 文件夾添加到您的 CIRCUITPY 驅動器中。這將確保您擁有所有驅動程序。但是這將占用8 MB磁盤上的一堆空間
根據需要添加每個庫,這將減少空間使用量,但您需要
至少,我們推薦以下庫,實際上我們比推薦的要多。基本上是必需的。因此,立即抓住它們并將它們安裝到 CIRCUITPY/lib !
adafruit_esp32spi -這是一個庫,可通過ESP32進行互聯網訪問使用(您猜對了!)SPI傳輸。 Internet所需的任何東西都需要使用它
adafruit_requests -該庫使我們能夠執行HTTP請求并從服務器獲取響應。 GET/POST/PUT/PATCH-它們都在這里!
adafruit_pyportal -這是我們友好的包裝器庫,可處理很多項目,顯示圖形和文本,從互聯網上獲取數據。
adafruit_touchscreen -一個用于從電阻式觸摸屏讀取觸摸的庫。
adafruit_cursorcontrol -用于讀取PyGamer和PyBadge游戲桿和按鈕的庫。為您處理所有圖形,按鈕閱讀和操縱桿移動。
adafruit_io -該庫有助于將PyPortal連接到我們的免費數據記錄和查看服務
adafruit_imageload -任何圖形所需的圖像顯示助手!
adafruit_display_text -毫不奇怪,它在屏幕上顯示文本
adafruit_bitmap_font -我們擁有精美的字體支持,并且易于制作新字體。該庫讀取并解析字體文件。
adafruit_slideshow -用于制作圖像幻燈片-方便快速顯示圖形和聲音
neopixel -用于控制板載新像素
adafruit_adt7410 -用于從板載Analog Devices ADT7410精密溫度傳感器讀取溫度的庫
adafruit_sdcard -支持從板載SD卡插槽中讀取/寫入數據。
adafruit_bus_device -對I2C/SPI的低級支持
Internet連接!
一旦您安裝了CircuitPython安裝程序和庫,我們就可以將您的電路板連接到Internet。請注意,目前不支持訪問企業級安全的WiFi網絡,僅支持需要SSID和密碼的WiFi網絡。
要建立連接,您需要先創建一個秘密文件。
什么是機密文件?
我們希望人們在構建CircuitPython WiFi小部件時共享大量項目。我們要避免的是人們不小心共享了他們的密碼或秘密令牌和API密鑰。因此,我們設計了所有示例,以使用 CIRCUITPY 驅動器中的secrets.py文件來保存秘密/私有/自定義數據。這樣,您就可以共享您的主項目,而不必擔心意外共享私有內容。
您的 secrets.py 文件應如下所示:
下載:文件
復制代碼
# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it
secrets = {
‘ssid’ : ‘home ssid’,
‘password’ : ‘my password’,
‘timezone’ : “America/New_York”, # http://worldtimeapi.org/timezones
‘github_token’ : ‘fawfj23rakjnfawiefa’,
‘hackaday_token’ : ‘h4xx0rs3kret’,
}
# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it
secrets = {
‘ssid’ : ‘home ssid’,
‘password’ : ‘my password’,
‘timezone’ : “America/New_York”, # http://worldtimeapi.org/timezones
‘github_token’ : ‘fawfj23rakjnfawiefa’,
‘hackaday_token’ : ‘h4xx0rs3kret’,
}
Inside是一個名為secrets的python字典,每個條目都有一行。每個條目都有一個條目名稱(例如‘ssid’),然后是一個冒號以將其與條目鍵‘home ssid’分開,最后是一個逗號,
至少您需要ssid和password用于您的本地WiFi設置。在創建項目時,您可能需要更多的令牌和密鑰,只需一次添加一行即可。例如,請參見其他令牌,例如用于訪問github或hackaday API的令牌。其他非機密數據(例如您的時區)也可以在此處,僅因為其稱為機密的信息并不意味著您不能在其中包含常規的自定義數據!
有關正確的時區字符串,請查看http://worldtimeapi.org/timezones,請記住,如果未列出您的城市,請在同一時區中查找城市,例如,波士頓,紐約,費城,華盛頓特區和邁阿密都與紐約位于同一時間。
當然,不要共享您的 secrets.py -將其保存在GitHub,Discord或其他項目共享站點之外。
連接到WiFi
好,現在您已設置好機密-您可以連接到Internet。讓我們使用ESP32SPI和Requests庫-您需要訪問CircuitPython捆綁包并安裝:
adafruit_bus_device
adafruit_esp32spi
adafruit_requests
neopixel
進入您的lib文件夾。完成后,使用Mu或您喜歡的編輯器加載以下示例:
下載:項目郵編或 esp32spi_simpletest.py | 在Github上查看
復制代碼
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
print(“ESP32 SPI webclient test”)
TEXT_URL = “http://wifitest.adafruit.com/testwifi/index.html”
JSON_URL = “http://api.coindesk.com/v1/bpi/currentprice/USD.json”
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
requests.set_socket(socket, esp)
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
print(“ESP32 found and in idle mode”)
print(“Firmware vers.”, esp.firmware_version)
print(“MAC addr:”, [hex(i) for i in esp.MAC_address])
for ap in esp.scan_networks():
print(“ %s RSSI: %d” % (str(ap[‘ssid’], ‘utf-8’), ap[‘rssi’]))
print(“Connecting to AP.。.”)
while not esp.is_connected:
try:
esp.connect_AP(b‘MY_SSID_NAME’, b‘MY_SSID_PASSWORD’)
except RuntimeError as e:
print(“could not connect to AP, retrying: ”,e)
continue
print(“Connected to”, str(esp.ssid, ‘utf-8’), “ RSSI:”, esp.rssi)
print(“My IP address is”, esp.pretty_ip(esp.ip_address))
print(“IP lookup adafruit.com: %s” % esp.pretty_ip(esp.get_host_by_name(“adafruit.com”)))
print(“Ping google.com: %d ms” % esp.ping(“google.com”))
#esp._debug = True
print(“Fetching text from”, TEXT_URL)
r = requests.get(TEXT_URL)
print(‘-’*40)
print(r.text)
print(‘-’*40)
r.close()
print()
print(“Fetching json from”, JSON_URL)
r = requests.get(JSON_URL)
print(‘-’*40)
print(r.json())
print(‘-’*40)
r.close()
print(“Done!”)
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
print(“ESP32 SPI webclient test”)
TEXT_URL = “http://wifitest.adafruit.com/testwifi/index.html”
JSON_URL = “http://api.coindesk.com/v1/bpi/currentprice/USD.json”
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
requests.set_socket(socket, esp)
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
print(“ESP32 found and in idle mode”)
print(“Firmware vers.”, esp.firmware_version)
print(“MAC addr:”, [hex(i) for i in esp.MAC_address])
for ap in esp.scan_networks():
print(“ %s RSSI: %d” % (str(ap[‘ssid’], ‘utf-8’), ap[‘rssi’]))
print(“Connecting to AP.。.”)
while not esp.is_connected:
try:
esp.connect_AP(b‘MY_SSID_NAME’, b‘MY_SSID_PASSWORD’)
except RuntimeError as e:
print(“could not connect to AP, retrying: ”,e)
continue
print(“Connected to”, str(esp.ssid, ‘utf-8’), “ RSSI:”, esp.rssi)
print(“My IP address is”, esp.pretty_ip(esp.ip_address))
print(“IP lookup adafruit.com: %s” % esp.pretty_ip(esp.get_host_by_name(“adafruit.com”)))
print(“Ping google.com: %d ms” % esp.ping(“google.com”))
#esp._debug = True
print(“Fetching text from”, TEXT_URL)
r = requests.get(TEXT_URL)
print(‘-’*40)
print(r.text)
print(‘-’*40)
r.close()
print()
print(“Fetching json from”, JSON_URL)
r = requests.get(JSON_URL)
print(‘-’*40)
print(r.json())
print(‘-’*40)
r.close()
print(“Done!”)
,并將其保存到您的板上,名稱為code.py。
第一個連接示例不使用機密文件-您將首先輸入SSID/密碼來驗證連接性!
然后轉到此行
esp.connect_AP(b‘MY_SSID_NAME’, b‘MY_SSID_PASSWORD’)
并更改MY_SSID_NAME和 MY_SSID_PASSWORD 設置為您的訪問點名稱和密碼,并將其保留在‘’引號內。 (此示例不使用機密文件,但它也非常獨立,因此,如果其他事情似乎不起作用,則可以始終重新加載該文件。您應該獲得如下內容:
示例代碼。..
使用SPI端口和3個控制引腳通過SPI初始化ESP32:
下載:文件
復制代碼
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
告訴我們的requests庫我們正在使用的套接字的類型(套接字類型隨連接類型而變化-在此示例中,我們將使用adafruit_esp32spi_socket)。還將接口設置為
下載:文件
復制代碼
esp requests
驗證找到ESP32,檢查固件和MAC地址
下載:文件
復制代碼
requests.set_socket(socket, esp) requests.set_socket(socket, esp)
掃描它可以看到的所有訪問點,并打印出名稱和信號強度:
下載:文件
復制代碼
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
print(“ESP32 found and in idle mode”)
print(“Firmware vers.”, esp.firmware_version)
print(“MAC addr:”, [hex(i) for i in esp.MAC_address]) if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
print(“ESP32 found and in idle mode”)
print(“Firmware vers.”, esp.firmware_version)
print(“MAC addr:”, [hex(i) for i in esp.MAC_address])
連接到我們在此處定義的AP,然后打印出來本地IP地址,嘗試進行域名查找并ping google.com以檢查網絡連接(請注意,有時ping失敗或需要一段時間,這沒什么大不了的)
下載:文件
復制代碼
for ap in esp.scan_networks():
print(“ %s RSSI: %d” % (str(ap[‘ssid’], ‘utf-8’), ap[‘rssi’])) for ap in esp.scan_networks():
print(“ %s RSSI: %d” % (str(ap[‘ssid’], ‘utf-8’), ap[‘rssi’]))
好的,現在我們進入真正有趣的部分。使用SAMD51或其他大容量RAM(超過32 KB)設備,我們可以做很多巧妙的事情。例如,我們可以像請求一樣實現一個接口,這使得獲取數據非常簡單
從Web URL調用 print(“Connecting to AP.。.”)
esp.connect_AP(b‘MY_SSID_NAME’, b‘MY_SSID_PASSWORD’)
print(“Connected to”, str(esp.ssid, ‘utf-8’), “ RSSI:”, esp.rssi)
print(“My IP address is”, esp.pretty_ip(esp.ip_address))
print(“IP lookup adafruit.com: %s” % esp.pretty_ip(esp.get_host_by_name(“adafruit.com”)))
print(“Ping google.com: %d ms” % esp.ping(“google.com”))
中讀取所有文本-您可以傳入 print(“Connecting to AP.。.”)
esp.connect_AP(b‘MY_SSID_NAME’, b‘MY_SSID_PASSWORD’)
print(“Connected to”, str(esp.ssid, ‘utf-8’), “ RSSI:”, esp.rssi)
print(“My IP address is”, esp.pretty_ip(esp.ip_address))
print(“IP lookup adafruit.com: %s” % esp.pretty_ip(esp.get_host_by_name(“adafruit.com”)))
print(“Ping google.com: %d ms” % esp.ping(“google.com”))
用于SSL連接的URL
下載:文件
復制代碼
requests.get https
或者,如果數據使用結構化JSON,則可以獲取json pre -解析為可以輕松查詢或遍歷的Python字典。 (同樣,僅適用于nRF52840,M4和其他高RAM板)
下載:文件
復制代碼
TEXT_URL = “http://wifitest.adafruit.com/testwifi/index.html”
print(“Fetching text from”, TEXT_URL)
r = requests.get(TEXT_URL)
print(‘-’*40)
print(r.text)
print(‘-’*40)
r.close() TEXT_URL = “http://wifitest.adafruit.com/testwifi/index.html”
print(“Fetching text from”, TEXT_URL)
r = requests.get(TEXT_URL)
print(‘-’*40)
print(r.text)
print(‘-’*40)
r.close()
Requests
我們已經為Web接口編寫了一個類似請求的庫,名為Adafruit_CircuitPython_Requests。該庫允許您發送HTTP/1.1請求,而無需“設計”它們,并提供了有用的方法來解析服務器的響應。
下載:Project Zip 或 requests_simpletest.py | 在Github上查看
復制代碼
JSON_URL = “http://api.coindesk.com/v1/bpi/currentprice/USD.json”
print(“Fetching json from”, JSON_URL)
r = requests.get(JSON_URL)
print(‘-’*40)
print(r.json())
print(‘-’*40)
r.close() JSON_URL = “http://api.coindesk.com/v1/bpi/currentprice/USD.json”
print(“Fetching json from”, JSON_URL)
r = requests.get(JSON_URL)
print(‘-’*40)
print(r.json())
print(‘-’*40)
r.close()
代碼首先設置ESP32SPI接口。然后,它使用ESP32 # adafruit_requests usage with an esp32spi_socket
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
print(“Connecting to AP.。.”)
while not esp.is_connected:
try:
esp.connect_AP(b‘MY_SSID_NAME’, b‘MY_SSID_PASSWORD’)
except RuntimeError as e:
print(“could not connect to AP, retrying: ”,e)
continue
print(“Connected to”, str(esp.ssid, ‘utf-8’), “ RSSI:”, esp.rssi)
# Initialize a requests object with a socket and esp32spi interface
requests.set_socket(socket, esp)
TEXT_URL = “http://wifitest.adafruit.com/testwifi/index.html”
JSON_GET_URL = “http://httpbin.org/get”
JSON_POST_URL = “http://httpbin.org/post”
print(“Fetching text from %s”%TEXT_URL)
response = requests.get(TEXT_URL)
print(‘-’*40)
print(“Text Response: ”, response.text)
print(‘-’*40)
response.close()
print(“Fetching JSON data from %s”%JSON_GET_URL)
response = requests.get(JSON_GET_URL)
print(‘-’*40)
print(“JSON Response: ”, response.json())
print(‘-’*40)
response.close()
data = ‘31F’
print(“POSTing data to {0}: {1}”.format(JSON_POST_URL, data))
response = requests.post(JSON_POST_URL, data=data)
print(‘-’*40)
json_resp = response.json()
# Parse out the ‘data’ key from json_resp dict.
print(“Data received from server:”, json_resp[‘data’])
print(‘-’*40)
response.close()
json_data = {“Date” : “July 25, 2019”}
print(“POSTing data to {0}: {1}”.format(JSON_POST_URL, json_data))
response = requests.post(JSON_POST_URL, json=json_data)
print(‘-’*40)
json_resp = response.json()
# Parse out the ‘json’ key from json_resp dict.
print(“JSON Data received from server:”, json_resp[‘json’])
print(‘-’*40)
response.close()
和# adafruit_requests usage with an esp32spi_socket
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
print(“Connecting to AP.。.”)
while not esp.is_connected:
try:
esp.connect_AP(b‘MY_SSID_NAME’, b‘MY_SSID_PASSWORD’)
except RuntimeError as e:
print(“could not connect to AP, retrying: ”,e)
continue
print(“Connected to”, str(esp.ssid, ‘utf-8’), “ RSSI:”, esp.rssi)
# Initialize a requests object with a socket and esp32spi interface
requests.set_socket(socket, esp)
TEXT_URL = “http://wifitest.adafruit.com/testwifi/index.html”
JSON_GET_URL = “http://httpbin.org/get”
JSON_POST_URL = “http://httpbin.org/post”
print(“Fetching text from %s”%TEXT_URL)
response = requests.get(TEXT_URL)
print(‘-’*40)
print(“Text Response: ”, response.text)
print(‘-’*40)
response.close()
print(“Fetching JSON data from %s”%JSON_GET_URL)
response = requests.get(JSON_GET_URL)
print(‘-’*40)
print(“JSON Response: ”, response.json())
print(‘-’*40)
response.close()
data = ‘31F’
print(“POSTing data to {0}: {1}”.format(JSON_POST_URL, data))
response = requests.post(JSON_POST_URL, data=data)
print(‘-’*40)
json_resp = response.json()
# Parse out the ‘data’ key from json_resp dict.
print(“Data received from server:”, json_resp[‘data’])
print(‘-’*40)
response.close()
json_data = {“Date” : “July 25, 2019”}
print(“POSTing data to {0}: {1}”.format(JSON_POST_URL, json_data))
response = requests.post(JSON_POST_URL, json=json_data)
print(‘-’*40)
json_resp = response.json()
# Parse out the ‘json’ key from json_resp dict.
print(“JSON Data received from server:”, json_resp[‘json’])
print(‘-’*40)
response.close()
對象初始化request對象。
下載:文件
復制代碼
socket esp
帶有請求的HTTP GET
代碼向Adafruit的WiFi測試網站-http:/發出HTTP GET請求/wifitest.adafruit.com/testwifi/index.html。
為此,我們會將URL傳遞到import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
print(“Connecting to AP.。.”)
while not esp.is_connected:
try:
esp.connect_AP(b‘MY_SSID_NAME’, b‘MY_SSID_PASSWORD’)
except RuntimeError as e:
print(“could not connect to AP, retrying: ”,e)
continue
print(“Connected to”, str(esp.ssid, ‘utf-8’), “ RSSI:”, esp.rssi)
# Initialize a requests object with a socket and esp32spi interface
requests.set_socket(socket, esp)。我們還將將來自服務器的響應保存到名為import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
print(“Connecting to AP.。.”)
while not esp.is_connected:
try:
esp.connect_AP(b‘MY_SSID_NAME’, b‘MY_SSID_PASSWORD’)
except RuntimeError as e:
print(“could not connect to AP, retrying: ”,e)
continue
print(“Connected to”, str(esp.ssid, ‘utf-8’), “ RSSI:”, esp.rssi)
# Initialize a requests object with a socket and esp32spi interface
requests.set_socket(socket, esp)的變量。
盡管我們從服務器請求數據,但我們還是希望服務器回應。由于我們已經保存了服務器的requests.get(),因此可以將其讀回。對我們來說幸運的是,請求將服務器的響應自動解碼為人類可讀的文本,您可以通過調用response將其讀回。
最后,我們將執行一些操作通過調用response進行清理。這樣會關閉,刪除和收集響應的數據。
下載:文件
復制代碼
response.text response.close()
一些服務器使用文本響應,而某些服務器則使用由屬性-值對組成的json格式的數據進行響應。/p》
CircuitPython_Requests可以將來自服務器的JSON格式的響應轉換為CPython print(“Fetching text from %s”%TEXT_URL)
response = requests.get(TEXT_URL)
print(‘-’*40)
print(“Text Response: ”, response.text)
print(‘-’*40)
response.close()對象。
我們還可以獲取和解析 json 數據。我們將發送HTTP Get到一個我們知道的URL,該URL返回一個json格式的響應(而不是文本數據)。
然后,代碼調用print(“Fetching text from %s”%TEXT_URL)
response = requests.get(TEXT_URL)
print(‘-’*40)
print(“Text Response: ”, response.text)
print(‘-’*40)
response.close()將響應轉換為一個CPython dict.。
下載:文件
復制代碼
response.json() dict
帶有請求的HTTP POST
請求還可以通過調用print(“Fetching JSON data from %s”%JSON_GET_URL)
response = requests.get(JSON_GET_URL)
print(‘-’*40)
print(“JSON Response: ”, response.json())
print(‘-’*40)
response.close()方法(向其傳遞一個print(“Fetching JSON data from %s”%JSON_GET_URL)
response = requests.get(JSON_GET_URL)
print(‘-’*40)
print(“JSON Response: ”, response.json())
print(‘-’*40)
response.close()值)將數據發布到服務器。
下載:文件
復制代碼
requests.post data
您還可以通過將data = ‘31F’
print(“POSTing data to {0}: {1}”.format(JSON_POST_URL, data))
response = requests.post(JSON_POST_URL, data=data)
print(‘-’*40)
json_resp = response.json()
# Parse out the ‘data’ key from json_resp dict.
print(“Data received from server:”, json_resp[‘data’])
print(‘-’*40)
response.close()傳遞到data = ‘31F’
print(“POSTing data to {0}: {1}”.format(JSON_POST_URL, data))
response = requests.post(JSON_POST_URL, data=data)
print(‘-’*40)
json_resp = response.json()
# Parse out the ‘data’ key from json_resp dict.
print(“Data received from server:”, json_resp[‘data’])
print(‘-’*40)
response.close()方法中,將json格式的數據發布到服務器。
下載:文件
復制代碼
json_data requests.post
高級請求使用情況
要發送自定義HTTP, aders,將響應解析為原始字節,還是在CircuitPython代碼中處理響應的http狀態代碼?
我們已經編寫了一個示例來顯示下面的請求模塊的高級用法。
下載:項目Zip 或 requests_advanced.py | 在Github上查看
復制代碼
json_data = {“Date” : “July 25, 2019”}
print(“POSTing data to {0}: {1}”.format(JSON_POST_URL, json_data))
response = requests.post(JSON_POST_URL, json=json_data)
print(‘-’*40)
json_resp = response.json()
# Parse out the ‘json’ key from json_resp dict.
print(“JSON Data received from server:”, json_resp[‘json’])
print(‘-’*40)
response.close()
json_data = {“Date” : “July 25, 2019”}
print(“POSTing data to {0}: {1}”.format(JSON_POST_URL, json_data))
response = requests.post(JSON_POST_URL, json=json_data)
print(‘-’*40)
json_resp = response.json()
# Parse out the ‘json’ key from json_resp dict.
print(“JSON Data received from server:”, json_resp[‘json’])
print(‘-’*40)
response.close()
WiFi Manager
這個最簡單的示例可行,但有點挑剔-您需要不斷檢查WiFi狀態,并具有許多循環來管理連接和斷開連接。對于更高級的用途,我們建議使用WiFiManager對象。它將為您包裝連接/狀態/請求循環-如果WiFi掉線,則重新連接,如果ESP32進入不良狀態,則重置ESP32,等等。
這是一個更高級的示例,其中顯示了WiFi管理器以及如何使用一些額外的標題發布數據:
下載:Project Zip 或 esp32spi_aio_post.py | 在Github上查看
復制代碼
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
print(“Connecting to AP.。.”)
while not esp.is_connected:
try:
esp.connect_AP(b‘MY_SSID_NAME’, b‘MY_SSID_PASSWORD’)
except RuntimeError as e:
print(“could not connect to AP, retrying: ”,e)
continue
print(“Connected to”, str(esp.ssid, ‘utf-8’), “ RSSI:”, esp.rssi)
# Initialize a requests object with a socket and esp32spi interface
requests.set_socket(socket, esp)
JSON_GET_URL = “http://httpbin.org/get”
# Define a custom header as a dict.
headers = {“user-agent” : “blinka/1.0.0”}
print(“Fetching JSON data from %s.。.”%JSON_GET_URL)
response = requests.get(JSON_GET_URL, headers=headers)
print(‘-’*60)
json_data = response.json()
headers = json_data[‘headers’]
print(“Response‘s Custom User-Agent Header: {0}”.format(headers[’User-Agent‘]))
print(’-‘*60)
# Read Response’s HTTP status code
print(“Response HTTP Status Code: ”, response.status_code)
print(‘-’*60)
# Read Response, as raw bytes instead of pretty text
print(“Raw Response: ”, response.content)
# Close, delete and collect the response data
response.close()
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
print(“Connecting to AP.。.”)
while not esp.is_connected:
try:
esp.connect_AP(b‘MY_SSID_NAME’, b‘MY_SSID_PASSWORD’)
except RuntimeError as e:
print(“could not connect to AP, retrying: ”,e)
continue
print(“Connected to”, str(esp.ssid, ‘utf-8’), “ RSSI:”, esp.rssi)
# Initialize a requests object with a socket and esp32spi interface
requests.set_socket(socket, esp)
JSON_GET_URL = “http://httpbin.org/get”
# Define a custom header as a dict.
headers = {“user-agent” : “blinka/1.0.0”}
print(“Fetching JSON data from %s.。.”%JSON_GET_URL)
response = requests.get(JSON_GET_URL, headers=headers)
print(‘-’*60)
json_data = response.json()
headers = json_data[‘headers’]
print(“Response‘s Custom User-Agent Header: {0}”.format(headers[’User-Agent‘]))
print(’-‘*60)
# Read Response’s HTTP status code
print(“Response HTTP Status Code: ”, response.status_code)
print(‘-’*60)
# Read Response, as raw bytes instead of pretty text
print(“Raw Response: ”, response.content)
# Close, delete and collect the response data
response.close()
您會在這里注意到,我們使用secrets.py文件來管理我們的SSID信息。 wifimanager被賦予了ESP32對象,密碼和一個用于指示狀態的新像素。
請注意,您需要在密碼文件中添加一些其他信息,以便代碼可以查詢Adafruit IO API:
import time
import board
import busio
from digitalio import DigitalInOut
import neopixel
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
print(“ESP32 SPI webclient test”)
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print(“WiFi secrets are kept in secrets.py, please add them there!”)
raise
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
“”“Use below for Most Boards”“”
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
“”“Uncomment below for ItsyBitsy M4”“”
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
# Uncomment below for an externally defined RGB LED
# import adafruit_rgbled
# from adafruit_esp32spi import PWMOut
# RED_LED = PWMOut.PWMOut(esp, 26)
# GREEN_LED = PWMOut.PWMOut(esp, 27)
# BLUE_LED = PWMOut.PWMOut(esp, 25)
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
counter = 0
while True:
try:
print(“Posting data.。.”, end=‘’)
data = counter
feed = ‘test’
payload = {‘value’:data}
response = wifi.post(
“https://io.adafruit.com/api/v2/”+secrets[‘aio_username’]+“/feeds/”+feed+“/data”,
json=payload,
headers={“X-AIO-KEY”:secrets[‘aio_key’]})
print(response.json())
response.close()
counter = counter + 1
print(“OK”)
except (ValueError, RuntimeError) as e:
print(“Failed to get data, retrying ”, e)
wifi.reset()
continue
response = None
time.sleep(15)
import time
import board
import busio
from digitalio import DigitalInOut
import neopixel
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
print(“ESP32 SPI webclient test”)
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print(“WiFi secrets are kept in secrets.py, please add them there!”)
raise
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
“”“Use below for Most Boards”“”
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
“”“Uncomment below for ItsyBitsy M4”“”
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
# Uncomment below for an externally defined RGB LED
# import adafruit_rgbled
# from adafruit_esp32spi import PWMOut
# RED_LED = PWMOut.PWMOut(esp, 26)
# GREEN_LED = PWMOut.PWMOut(esp, 27)
# BLUE_LED = PWMOut.PWMOut(esp, 25)
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
counter = 0
while True:
try:
print(“Posting data.。.”, end=‘’)
data = counter
feed = ‘test’
payload = {‘value’:data}
response = wifi.post(
“https://io.adafruit.com/api/v2/”+secrets[‘aio_username’]+“/feeds/”+feed+“/data”,
json=payload,
headers={“X-AIO-KEY”:secrets[‘aio_key’]})
print(response.json())
response.close()
counter = counter + 1
print(“OK”)
except (ValueError, RuntimeError) as e:
print(“Failed to get data, retrying ”, e)
wifi.reset()
continue
response = None
time.sleep(15)
您可以轉到adafruit.io查看AIO密鑰鏈接獲取這兩個值并將它們添加到secrets文件中,該文件現在看起來像這樣:
下載:文件
復制代碼
aio_username aio_key
下一步,設置一個名為Adafruit IO提要# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it
secrets = {
‘ssid’ : ‘_your_ssid_’,
‘password’ : ‘_your_wifi_password_’,
‘timezone’ : “America/Los_Angeles”, # http://worldtimeapi.org/timezones
‘aio_username’ : ‘_your_aio_username_’,
‘aio_key’ : ‘_your_aio_key_’,
}
如果您不知道如何設置供稿,請遵循此頁面并在設置名為。
然后,我們可以有一個簡單的循環,用于將數據發布到Adafruit IO,而無需處理連接或初始化硬件!
在Adafruit.io上查看您的測試數據,每次Cir時,您都會看到該值增加cuitPython板向其發布數據!
使用CircuitPython編寫PyPortal代碼
開放天氣Maps API密鑰
我們將使用OpenWeatherMaps.org通過其API檢索天氣信息。為此,您需要注冊一個帳戶并獲取您的API密鑰。
轉到此鏈接并注冊一個免費帳戶。注冊后,您會收到一封包含API密鑰的電子郵件,也稱為“ openweather令牌”。
將此密鑰復制并粘貼到您的 secrets.py 文件中在CIRCUITPY驅動器的根目錄上,因此如下所示:
下載:文件
復制代碼
secrets = {
‘ssid’ : ‘your_wifi_ssid’,
‘password’ : ‘your_wifi_password’,
‘openweather_token’ : ‘xxxxxxxxxxxxxxxxxxxxxxxx’
}
secrets = {
‘ssid’ : ‘your_wifi_ssid’,
‘password’ : ‘your_wifi_password’,
‘openweather_token’ : ‘xxxxxxxxxxxxxxxxxxxxxxxx’
}
Adafruit IO時間服務器
為了獲取準確的時間,我們的項目將查詢Adafruit IO物聯網服務的時間。 Adafruit IO是絕對免費的,但是您需要使用Adafruit帳戶登錄才能使用它。如果您還沒有Adafruit登錄名,請在此處創建一個登錄名。
如果您以前從未使用過Adafruit IO,請查看本指南以獲取更多信息。
登錄到您的帳戶后,您需要在secrets.py文件中放置以下兩項信息: Adafruit IO用戶名和 Adafruit IO鍵。前往io.adafruit.com,只需點擊Adafruit IO頁面左側的查看AIO密鑰鏈接即可獲取此信息。
然后將它們添加到secrets.py文件如下:
下載:文件
復制代碼
secrets = {
‘ssid’ : ‘your_wifi_ssid’,
‘password’ : ‘your_wifi_password’,
‘openweather_token’ : ‘xxxxxxxxxxxxxxxxxxxxxxxx’,
‘aio_username’ : ‘_your_aio_username_’,
‘aio_key’ : ‘_your_big_huge_super_long_aio_key_’
}
secrets = {
‘ssid’ : ‘your_wifi_ssid’,
‘password’ : ‘your_wifi_password’,
‘openweather_token’ : ‘xxxxxxxxxxxxxxxxxxxxxxxx’,
‘aio_username’ : ‘_your_aio_username_’,
‘aio_key’ : ‘_your_big_huge_super_long_aio_key_’
}
CircuitPython代碼
在下面的嵌入式代碼元素中,單擊下載:Project Zip 鏈接,然后保存。 zip壓縮文件到您的計算機。
然后解壓縮.zip文件,它將解壓縮到名為 PyPortal_OpenWeather 的文件夾中。
復制 PyPortal_OpenWeather 目錄到PyPortal的 CIRCUITPY 驅動器,然后確保將 openweather.py 文件重命名為 code.py 》,因此它將在PyPortal重新啟動時自動運行。
這是 CIRCUITPY 驅動器的最終內容,如下所示:
下載:項目Zip 或 openweather.py | 在Github上查看
復制代碼
“”“
This example queries the Open Weather Maps site API to find out the current
weather for your location.。. and display it on a screen!
if you can find something that spits out JSON data, we can display it
”“”
import sys
import time
import board
from adafruit_pyportal import PyPortal
cwd = (“/”+__file__).rsplit(‘/’, 1)[0] # the current working directory (where this file is)
sys.path.append(cwd)
import openweather_graphics # pylint: disable=wrong-import-position
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print(“WiFi secrets are kept in secrets.py, please add them there!”)
raise
# Use cityname, country code where countrycode is ISO3166 format.
# E.g. “New York, US” or “London, GB”
LOCATION = “Manhattan, US”
# Set up where we‘ll be fetching data from
DATA_SOURCE = “http://api.openweathermap.org/data/2.5/weather?q=”+LOCATION
DATA_SOURCE += “&appid=”+secrets[’openweather_token‘]
# You’ll need to get a token from openweather.org, looks like ‘b6907d289e10d714a6e88b30761fae22’
DATA_LOCATION = []
# Initialize the pyportal object and let us know what data to fetch and where
# to display it
pyportal = PyPortal(url=DATA_SOURCE,
json_path=DATA_LOCATION,
status_neopixel=board.NEOPIXEL,
default_bg=0x000000)
gfx = openweather_graphics.OpenWeather_Graphics(pyportal.splash, am_pm=True, celsius=False)
localtile_refresh = None
weather_refresh = None
while True:
# only query the online time once per hour (and on first run)
if (not localtile_refresh) or (time.monotonic() - localtile_refresh) 》 3600:
try:
print(“Getting time from internet!”)
pyportal.get_local_time()
localtile_refresh = time.monotonic()
except RuntimeError as e:
print(“Some error occured, retrying! -”, e)
continue
# only query the weather every 10 minutes (and on first run)
if (not weather_refresh) or (time.monotonic() - weather_refresh) 》 600:
try:
value = pyportal.fetch()
print(“Response is”, value)
gfx.display_weather(value)
weather_refresh = time.monotonic()
except RuntimeError as e:
print(“Some error occured, retrying! -”, e)
continue
gfx.update_time()
time.sleep(30) # wait 30 seconds before updating anything again
“”“
This example queries the Open Weather Maps site API to find out the current
weather for your location.。. and display it on a screen!
if you can find something that spits out JSON data, we can display it
”“”
import sys
import time
import board
from adafruit_pyportal import PyPortal
cwd = (“/”+__file__).rsplit(‘/’, 1)[0] # the current working directory (where this file is)
sys.path.append(cwd)
import openweather_graphics # pylint: disable=wrong-import-position
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print(“WiFi secrets are kept in secrets.py, please add them there!”)
raise
# Use cityname, country code where countrycode is ISO3166 format.
# E.g. “New York, US” or “London, GB”
LOCATION = “Manhattan, US”
# Set up where we‘ll be fetching data from
DATA_SOURCE = “http://api.openweathermap.org/data/2.5/weather?q=”+LOCATION
DATA_SOURCE += “&appid=”+secrets[’openweather_token‘]
# You’ll need to get a token from openweather.org, looks like ‘b6907d289e10d714a6e88b30761fae22’
DATA_LOCATION = []
# Initialize the pyportal object and let us know what data to fetch and where
# to display it
pyportal = PyPortal(url=DATA_SOURCE,
json_path=DATA_LOCATION,
status_neopixel=board.NEOPIXEL,
default_bg=0x000000)
gfx = openweather_graphics.OpenWeather_Graphics(pyportal.splash, am_pm=True, celsius=False)
localtile_refresh = None
weather_refresh = None
while True:
# only query the online time once per hour (and on first run)
if (not localtile_refresh) or (time.monotonic() - localtile_refresh) 》 3600:
try:
print(“Getting time from internet!”)
pyportal.get_local_time()
localtile_refresh = time.monotonic()
except RuntimeError as e:
print(“Some error occured, retrying! -”, e)
continue
# only query the weather every 10 minutes (and on first run)
if (not weather_refresh) or (time.monotonic() - weather_refresh) 》 600:
try:
value = pyportal.fetch()
print(“Response is”, value)
gfx.display_weather(value)
weather_refresh = time.monotonic()
except RuntimeError as e:
print(“Some error occured, retrying! -”, e)
continue
gfx.update_time()
time.sleep(30) # wait 30 seconds before updating anything again
如果遇到任何錯誤,例如“ ImportError:沒有名為`adafruit_display_text.label`的模塊”,請確保將您的庫更新到最新版本!
PyPortal氣象站有幾個步驟可為您提供所需的信息!它具有啟動屏幕,天氣圖標和多種用于顯示信息的字體。
背景
首先,它將位圖圖形顯示為屏幕的啟動背景,直到連接到打開天氣地圖服務器以獲取天氣信息。這是 .bmp 格式的320 x 240像素RGB 16位光柵圖形。
時間
接下來,該程序通過WiFi連接,通過adafruit.io服務器獲取本地時間,該時間將顯示在顯示屏的右上角。
位置
在 code.py 文件(將從 openweather.py 重命名),您可以在此行中更改要顯示天氣的位置:
下載:文件
復制代碼
# Use cityname, country code where countrycode is ISO3166 format.
# E.g. “New York, US” or “London, GB”
LOCATION = “Manhattan, US” # Use cityname, country code where countrycode is ISO3166 format.
# E.g. “New York, US” or “London, GB”
LOCATION = “Manhattan, US”
API查詢和JSON
使用這些信息之后,代碼即可將查詢發送給看起來像是t的Open Weather Maps API他的:
http://api.openweathermap.org/data/2.5/weather?q=Los Angeles, US&appid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
(所有這些“ x”都是您的令牌)。
此查詢完成后,它將返回JSON看起來像這樣的文件:
下載:文件
復制代碼
{
“coord”: {
“lon”: -118.24,
“lat”: 34.05
},
“weather”: [
{
“id”: 501,
“main”: “Rain”,
“description”: “moderate rain”,
“icon”: “10d”
}
],
“base”: “stations”,
“main”: {
“temp”: 287.42,
“pressure”: 1016,
“humidity”: 50,
“temp_min”: 285.15,
“temp_max”: 289.15
},
“visibility”: 16093,
“wind”: {
“speed”: 3.6,
“deg”: 300
},
“rain”: {
“1h”: 1.52
},
“clouds”: {
“all”: 75
},
“dt”: 1552073935,
“sys”: {
“type”: 1,
“id”: 3514,
“message”: 0.0087,
“country”: “US”,
“sunrise”: 1552054308,
“sunset”: 1552096542
},
“id”: 5368361,
“name”: “Los Angeles”,
“cod”: 200
}
{
“coord”: {
“lon”: -118.24,
“lat”: 34.05
},
“weather”: [
{
“id”: 501,
“main”: “Rain”,
“description”: “moderate rain”,
“icon”: “10d”
}
],
“base”: “stations”,
“main”: {
“temp”: 287.42,
“pressure”: 1016,
“humidity”: 50,
“temp_min”: 285.15,
“temp_max”: 289.15
},
“visibility”: 16093,
“wind”: {
“speed”: 3.6,
“deg”: 300
},
“rain”: {
“1h”: 1.52
},
“clouds”: {
“all”: 75
},
“dt”: 1552073935,
“sys”: {
“type”: 1,
“id”: 3514,
“message”: 0.0087,
“country”: “US”,
“sunrise”: 1552054308,
“sunset”: 1552096542
},
“id”: 5368361,
“name”: “Los Angeles”,
“cod”: 200
}
以下是使用Firefox瀏覽器的內置工具美化的同一文件(您也可以使用在線代碼“美化器”,例如https://codebeautify.org/jsonviewer或http://jsonviewer.stack.hu):
JSON遍歷
JSON文件的格式設置使其可以輕松遍歷層次結構和解析數據。在其中,您將看到諸如main,description,icon和temp之類的鍵及其各自的值。因此,以下是氣象站需要關注的一些 key:value 對:
“main” : “Rain”
“description” : “moderate rain”
“icon” : “10d”
“temp” : “287.42”
為了從文件中獲取此數據,我們需要能夠分層描述它們在文件中的位置。例如,這有助于區分‘main’天氣狀況和包含溫度和其他數據的‘main’部分。為了避免名稱沖突,我們依靠JSON遍歷。
在 openweather_graphics.py 文件中,您將看到如何完成此操作。例如,可以在以下JSON文件層次結構中找到主鍵:[‘weather’], [0], [‘main’]
這意味著在JSON文件的頂層有一個名為‘weather’的鍵,該鍵有一個索引為[0]的子樹,然后在其下面為‘main’鍵。
此過程用于投射溫度,天氣,描述以及從位圖圖標目錄中顯示的圖標。
這些是表示的圖標:
字體
數據以使用位圖字體創建的文本顯示為疊加在背景上。這里使用的字體是由Arial字體制成的位圖字體。您可以在本指南中了解有關轉換類型的更多信息。
PyPortal構造函數
設置pyportal構造函數時,我們將為它們提供以下內容:
url進行查詢
json_path遍歷并找到我們需要的鍵:值對
default_bg默認背景色
獲取
設置PyPortal之后,我們可以使用pyportal.fetch()進行天氣數據的查詢和解析,然后將其顯示在屏幕上。
解析數據并將其顯示為文本或位圖的所有繁重工作都在 openweather_graphics.py 代碼。
圖形
讓我們看看 openweather_graphics.py 代碼如何將元素放置在屏幕上。在下面,我們可以看到顯示的圖標和文本。引號中的項目是JSON文件中的鍵名,它們的值是我們使用CircuitPython label庫看到的內容。
下載:Zip項目或 openweather_graphics.py | 在Github上查看
復制代碼
import time
import json
import displayio
from adafruit_display_text.label import Label
from adafruit_bitmap_font import bitmap_font
cwd = (“/”+__file__).rsplit(‘/’, 1)[0] # the current working directory (where this file is)
small_font = cwd+“/fonts/Arial-12.bdf”
medium_font = cwd+“/fonts/Arial-16.bdf”
large_font = cwd+“/fonts/Arial-Bold-24.bdf”
class OpenWeather_Graphics(displayio.Group):
def __init__(self, root_group, *, am_pm=True, celsius=True):
super().__init__(max_size=2)
self.am_pm = am_pm
self.celsius = celsius
root_group.append(self)
self._icon_group = displayio.Group(max_size=1)
self.append(self._icon_group)
self._text_group = displayio.Group(max_size=5)
self.append(self._text_group)
self._icon_sprite = None
self._icon_file = None
self.set_icon(cwd+“/weather_background.bmp”)
self.small_font = bitmap_font.load_font(small_font)
self.medium_font = bitmap_font.load_font(medium_font)
self.large_font = bitmap_font.load_font(large_font)
glyphs = b‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,。: ’
self.small_font.load_glyphs(glyphs)
self.medium_font.load_glyphs(glyphs)
self.large_font.load_glyphs(glyphs)
self.large_font.load_glyphs((‘°’,)) # a non-ascii character we need for sure
self.city_text = None
self.time_text = Label(self.medium_font, max_glyphs=8)
self.time_text.x = 200
self.time_text.y = 12
self.time_text.color = 0xFFFFFF
self._text_group.append(self.time_text)
self.temp_text = Label(self.large_font, max_glyphs=6)
self.temp_text.x = 200
self.temp_text.y = 195
self.temp_text.color = 0xFFFFFF
self._text_group.append(self.temp_text)
self.main_text = Label(self.large_font, max_glyphs=20)
self.main_text.x = 10
self.main_text.y = 195
self.main_text.color = 0xFFFFFF
self._text_group.append(self.main_text)
self.description_text = Label(self.small_font, max_glyphs=60)
self.description_text.x = 10
self.description_text.y = 225
self.description_text.color = 0xFFFFFF
self._text_group.append(self.description_text)
def display_weather(self, weather):
weather = json.loads(weather)
# set the icon/background
weather_icon = weather[‘weather’][0][‘icon’]
self.set_icon(cwd+“/icons/”+weather_icon+“.bmp”)
city_name = weather[‘name’] + “, ” + weather[‘sys’][‘country’]
print(city_name)
if not self.city_text:
self.city_text = Label(self.medium_font, text=city_name)
self.city_text.x = 10
self.city_text.y = 12
self.city_text.color = 0xFFFFFF
self._text_group.append(self.city_text)
self.update_time()
main_text = weather[‘weather’][0][‘main’]
print(main_text)
self.main_text.text = main_text
temperature = weather[‘main’][‘temp’] - 273.15 # its.。.in kelvin
print(temperature)
if self.celsius:
self.temp_text.text = “%d °C” % temperature
else:
self.temp_text.text = “%d °F” % ((temperature * 9 / 5) + 32)
description = weather[‘weather’][0][‘description’]
description = description[0].upper() + description[1:]
print(description)
self.description_text.text = description
# “thunderstorm with heavy drizzle”
def update_time(self):
“”“Fetch the time.localtime(), parse it out and update the display text”“”
now = time.localtime()
hour = now[3]
minute = now[4]
format_str = “%d:%02d”
if self.am_pm:
if hour 》= 12:
hour -= 12
format_str = format_str+“ PM”
else:
format_str = format_str+“ AM”
if hour == 0:
hour = 12
time_str = format_str % (hour, minute)
print(time_str)
self.time_text.text = time_str
def set_icon(self, filename):
“”“The background image to a bitmap file.
:param filename: The filename of the chosen icon
”“”
print(“Set icon to ”, filename)
if self._icon_group:
self._icon_group.pop()
if not filename:
return # we‘re done, no icon desired
if self._icon_file:
self._icon_file.close()
self._icon_file = open(filename, “rb”)
icon = displayio.OnDiskBitmap(self._icon_file)
try:
self._icon_sprite = displayio.TileGrid(icon,
pixel_shader=displayio.ColorConverter())
except TypeError:
self._icon_sprite = displayio.TileGrid(icon,
pixel_shader=displayio.ColorConverter(),
position=(0,0))
self._icon_group.append(self._icon_sprite)
import time
import json
import displayio
from adafruit_display_text.label import Label
from adafruit_bitmap_font import bitmap_font
cwd = (“/”+__file__).rsplit(’/‘, 1)[0] # the current working directory (where this file is)
small_font = cwd+“/fonts/Arial-12.bdf”
medium_font = cwd+“/fonts/Arial-16.bdf”
large_font = cwd+“/fonts/Arial-Bold-24.bdf”
class OpenWeather_Graphics(displayio.Group):
def __init__(self, root_group, *, am_pm=True, celsius=True):
super().__init__(max_size=2)
self.am_pm = am_pm
self.celsius = celsius
root_group.append(self)
self._icon_group = displayio.Group(max_size=1)
self.append(self._icon_group)
self._text_group = displayio.Group(max_size=5)
self.append(self._text_group)
self._icon_sprite = None
self._icon_file = None
self.set_icon(cwd+“/weather_background.bmp”)
self.small_font = bitmap_font.load_font(small_font)
self.medium_font = bitmap_font.load_font(medium_font)
self.large_font = bitmap_font.load_font(large_font)
glyphs = b’0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,。: ‘
self.small_font.load_glyphs(glyphs)
self.medium_font.load_glyphs(glyphs)
self.large_font.load_glyphs(glyphs)
self.large_font.load_glyphs((’°‘,)) # a non-ascii character we need for sure
self.city_text = None
self.time_text = Label(self.medium_font, max_glyphs=8)
self.time_text.x = 200
self.time_text.y = 12
self.time_text.color = 0xFFFFFF
self._text_group.append(self.time_text)
self.temp_text = Label(self.large_font, max_glyphs=6)
self.temp_text.x = 200
self.temp_text.y = 195
self.temp_text.color = 0xFFFFFF
self._text_group.append(self.temp_text)
self.main_text = Label(self.large_font, max_glyphs=20)
self.main_text.x = 10
self.main_text.y = 195
self.main_text.color = 0xFFFFFF
self._text_group.append(self.main_text)
self.description_text = Label(self.small_font, max_glyphs=60)
self.description_text.x = 10
self.description_text.y = 225
self.description_text.color = 0xFFFFFF
self._text_group.append(self.description_text)
def display_weather(self, weather):
weather = json.loads(weather)
# set the icon/background
weather_icon = weather[’weather‘][0][’icon‘]
self.set_icon(cwd+“/icons/”+weather_icon+“.bmp”)
city_name = weather[’name‘] + “, ” + weather[’sys‘][’country‘]
print(city_name)
if not self.city_text:
self.city_text = Label(self.medium_font, text=city_name)
self.city_text.x = 10
self.city_text.y = 12
self.city_text.color = 0xFFFFFF
self._text_group.append(self.city_text)
self.update_time()
main_text = weather[’weather‘][0][’main‘]
print(main_text)
self.main_text.text = main_text
temperature = weather[’main‘][’temp‘] - 273.15 # its.。.in kelvin
print(temperature)
if self.celsius:
self.temp_text.text = “%d °C” % temperature
else:
self.temp_text.text = “%d °F” % ((temperature * 9 / 5) + 32)
description = weather[’weather‘][0][’description‘]
description = description[0].upper() + description[1:]
print(description)
self.description_text.text = description
# “thunderstorm with heavy drizzle”
def update_time(self):
“”“Fetch the time.localtime(), parse it out and update the display text”“”
now = time.localtime()
hour = now[3]
minute = now[4]
format_str = “%d:%02d”
if self.am_pm:
if hour 》= 12:
hour -= 12
format_str = format_str+“ PM”
else:
format_str = format_str+“ AM”
if hour == 0:
hour = 12
time_str = format_str % (hour, minute)
print(time_str)
self.time_text.text = time_str
def set_icon(self, filename):
“”“The background image to a bitmap file.
:param filename: The filename of the chosen icon
”“”
print(“Set icon to ”, filename)
if self._icon_group:
self._icon_group.pop()
if not filename:
return # we’re done, no icon desired
if self._icon_file:
self._icon_file.close()
self._icon_file = open(filename, “rb”)
icon = displayio.OnDiskBitmap(self._icon_file)
try:
self._icon_sprite = displayio.TileGrid(icon,
pixel_shader=displayio.ColorConverter())
except TypeError:
self._icon_sprite = displayio.TileGrid(icon,
pixel_shader=displayio.ColorConverter(),
position=(0,0))
self._icon_group.append(self._icon_sprite)
文本位置
取決于背景位圖的設計和所顯示文本的長度,您可能需要重新定位文本和標題。
PyPortal的顯示為寬320像素,高240像素。為了引用屏幕上的這些位置,我們使用x/y坐標系,其中x為水平,y為垂直。
此坐標系的原點是左上角。這意味著放在左上角的像素為(0,0),而位于右下角的像素為(320,240)。
因此,如果您想將訂戶計數文本向右移動并更靠近在頂部,您的代碼對于pyportal構造函數的該部分可能是這樣的:text_position=(250, 10)
文本顏色
自定義顯示的另一種方法是調整文本的顏色。構造函數中的text_color=0xFFFFFF行顯示了操作方法。您需要為要顯示的任何顏色使用十六進制值。
在此示例中,您可以使用https://htmlcolorcodes.com/之類的顏色來選擇顏色,然后復制十六進制值。它應該是0x0ED9EE
現在,我們來看將PyPortal安裝到要顯示的盒子中!
PyPortal氣象站案例
我決定使用Ruiz Bros.設計的便攜式PyPortal箱作為外殼。您可以轉到本指南來構建它-通過自己進行3D打印或發送到3DHubs等服務。
我不需要此便攜式設備,因此省略了PowerBoost,電池,揚聲器和開關。
我使用了所有相同的3D零件,并將安裝座擰在一起,然后將頂部和底部卡入到位。
磁鐵
我想將PyPortal氣象站安裝在冰箱上,所以我要固定磁鐵。
磁鐵
使用一塊雙面膠帶將膠帶固定到外殼上,如圖所示。
我在我的工作臺抽屜,效果很好!是時候將其放在冰箱上并插入電源了!
現在,每30秒,您的氣象站將更新為最新信息!
責任編輯:wv
-
氣象站
+關注
關注
1文章
749瀏覽量
15660
發布評論請先 登錄
相關推薦
評論