色哟哟视频在线观看-色哟哟视频在线-色哟哟欧美15最新在线-色哟哟免费在线观看-国产l精品国产亚洲区在线观看-国产l精品国产亚洲区久久

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

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

怎樣將PyPortal設(shè)置為圖像查看器

454398 ? 來(lái)源:網(wǎng)絡(luò)整理 ? 作者:網(wǎng)絡(luò)整理 ? 2019-11-27 17:04 ? 次閱讀

概述

怎樣將PyPortal設(shè)置為圖像查看器

SPAAAAAACE!誰(shuí)不想看到無(wú)窮無(wú)盡的太空照片?

NASA的每日天文圖片(APOD)網(wǎng)站提供了標(biāo)題中所說(shuō)的內(nèi)容,每天都有一張令人難以置信的嶄新太空照片?,F(xiàn)在,您可以將PyPortal設(shè)置為這些光榮圖像的專用查看器!

您將使用CircuitPython對(duì)其進(jìn)行編碼,并且PyPortal庫(kù)將使查詢NASA Open API的過(guò)程變得簡(jiǎn)單。圖像。

零件

Adafruit PyPortal-CircuitPython支持的Internet顯示

產(chǎn)品ID:4116

PyPortal,我們易于使用的物聯(lián)網(wǎng)設(shè)備,可讓您在幾分鐘內(nèi)為“物聯(lián)網(wǎng)”創(chuàng)建所有東西。制作自定義觸摸屏界面。..

$ 54.95

進(jìn)貨

添加到購(gòu)物車

Adafruit PyPortal桌面支架附件包

產(chǎn)品ID:4146

PyPortal是我們易于使用的IoT設(shè)備,可讓您在數(shù)分鐘內(nèi)為“物聯(lián)網(wǎng)”創(chuàng)建所有東西。創(chuàng)建小口袋。..

$ 9.95

進(jìn)貨

添加到購(gòu)物車

完全可反轉(zhuǎn)的粉紅色/紫色USB A至micro B電纜- 1m長(zhǎng)

產(chǎn)品ID:4111

該電纜不僅非常時(shí)尚,而且具有粉紅色和紫色Blinka編織圖案,而且完全可逆!沒(méi)錯(cuò),您每天可以節(jié)省。..秒。..

$ 3.95

入庫(kù)存

添加到購(gòu)物車

安裝CircuitPython

CircuitPython是MicroPython的派生版,旨在簡(jiǎn)化低成本微控制器上的實(shí)驗(yàn)和教學(xué)。它不需要任何前期桌面軟件下載,因此比以往任何時(shí)候都更容易獲得原型。只需復(fù)制并編輯 CIRCUITPY “閃存”驅(qū)動(dòng)器上的文件即可進(jìn)行迭代。

以下說(shuō)明將向您展示如何安裝CircuitPython。如果您已經(jīng)安裝了CircuitPython,但希望對(duì)其進(jìn)行更新或重新安裝,則同樣的步驟也適用!

設(shè)置CircuitPython快速入門!

請(qǐng)遵循以下快速逐步操作以獲取超快速的Python功能:)

通過(guò)CircuitPython.org為該板下載最新版本的CircuitPython

點(diǎn)擊上面的鏈接,為PyPortal下載最新版本的CircuitPython。

下載并將其保存到您的桌面(或方便的任何地方)。

使用已知良好的USB電纜將PyPortal插入計(jì)算機(jī)。

A很多人最終使用僅可充電的USB電纜,這非常令人沮喪!因此,請(qǐng)確保您擁有知道對(duì)數(shù)據(jù)同步有用的USB電纜。

雙擊中間頂部頂部的重置按鈕(洋紅色箭頭)在板上,您將看到NeoPixel RGB LED(綠色箭頭)變?yōu)榫G色。如果它變成紅色,請(qǐng)檢查USB電纜,嘗試使用另一個(gè)USB端口,等等。注意:USB接口旁邊的紅色小LED指示燈將呈紅色閃爍。沒(méi)關(guān)系!

如果第一次雙擊不起作用,請(qǐng)重試。有時(shí)可能需要一些嘗試才能使節(jié)奏正確!

您將看到一個(gè)名為 PORTALBOOT 。

將 adafruit-circuitpython-pyportal- .uf2 文件拖到 PORTALBOOT。

LED將閃爍。然后, PORTALBOOT 驅(qū)動(dòng)器將消失,并且將出現(xiàn)一個(gè)名為 CIRCUITPY 的新磁盤驅(qū)動(dòng)器。

如果未在板上添加任何代碼,唯一存在的文件是 boot_out.txt 。這是絕對(duì)正常的!是時(shí)候添加您的 code.py 并開始使用它了!

就這樣,您完成了! :)

PyPortal默認(rèn)文件

單擊下面的按鈕,下載PyPortal附帶的文件的zip。

PyPortal默認(rèn)文件

PyPortal CircuitPython設(shè)置

要在CircuitPython中使用PyPortal的所有出色功能,必須首先安裝許多庫(kù)。本頁(yè)涵蓋了該過(guò)程。

Adafruit CircuitPython捆綁包

下載Adafruit CircuitPython庫(kù)捆綁包。您可以在此處找到最新版本:

最新的Adafruit CircuitPython庫(kù)包

下載 adafruit-circuitpython-bundle-4。 x-mpy-*。zip 捆綁zip文件,然后解壓縮同名文件夾。在內(nèi)部,您會(huì)找到一個(gè) lib 文件夾。您有兩個(gè)選擇:

您可以將 lib 文件夾添加到您的 CIRCUITPY 驅(qū)動(dòng)器中。這將確保您擁有所有驅(qū)動(dòng)程序。但是這將占用8 MB磁盤上的一堆空間

根據(jù)需要添加每個(gè)庫(kù),這將減少空間使用量,但您需要

至少,我們推薦以下庫(kù),實(shí)際上我們比推薦的要多?;旧鲜潜匦璧?。因此,立即抓住它們并將它們安裝到 CIRCUITPY/lib !

adafruit_esp32spi -這是一個(gè)庫(kù),可通過(guò)ESP32進(jìn)行互聯(lián)網(wǎng)訪問(wèn)使用(您猜對(duì)了?。㏒PI傳輸。 Internet所需的任何東西都需要使用它

adafruit_requests -該庫(kù)使我們能夠執(zhí)行HTTP請(qǐng)求并從服務(wù)器獲取響應(yīng)。 GET/POST/PUT/PATCH-它們都在這里!

adafruit_pyportal -這是我們友好的包裝器庫(kù),可處理很多項(xiàng)目,顯示圖形和文本,從互聯(lián)網(wǎng)上獲取數(shù)據(jù)。

adafruit_touchscreen -一個(gè)用于從電阻式觸摸屏讀取觸摸的庫(kù)。

adafruit_cursorcontrol -用于讀取PyGamer和PyBadge游戲桿和按鈕的庫(kù)。為您處理所有圖形,按鈕閱讀和操縱桿移動(dòng)。

adafruit_io -該庫(kù)有助于將PyPortal連接到我們的免費(fèi)數(shù)據(jù)記錄和查看服務(wù)

adafruit_imageload -任何圖形所需的圖像顯示助手!

adafruit_display_text -毫不奇怪,它在屏幕上顯示文本

adafruit_bitmap_font -我們擁有精美的字體支持,并且易于制作新字體。該庫(kù)讀取并解析字體文件。

adafruit_slideshow -用于制作圖像幻燈片-方便快速顯示圖形和聲音

neopixel -用于控制板載新像素

adafruit_adt7410 -用于從板載Analog Devices ADT7410精密溫度傳感器讀取溫度的庫(kù)

adafruit_sdcard -支持從板載SD卡插槽中讀取/寫入數(shù)據(jù)。

adafruit_bus_device -對(duì)I2C/SPI的低級(jí)支持

Internet連接!

一旦您安裝了CircuitPython安裝程序和庫(kù),我們就可以將您的電路板連接到Internet。請(qǐng)注意,目前不支持訪問(wèn)企業(yè)級(jí)安全的WiFi網(wǎng)絡(luò),僅支持需要SSID和密碼的WiFi網(wǎng)絡(luò)。

要建立連接,您需要先創(chuàng)建一個(gè)秘密文件。

什么是機(jī)密文件?

我們希望人們?cè)跇?gòu)建CircuitPython WiFi小部件時(shí)共享大量項(xiàng)目。我們要避免的是人們不小心共享了他們的密碼或秘密令牌和API密鑰。因此,我們?cè)O(shè)計(jì)了所有示例,以使用 CIRCUITPY 驅(qū)動(dòng)器中的secrets.py文件來(lái)保存秘密/私有/自定義數(shù)據(jù)。這樣,您就可以共享您的主項(xiàng)目,而不必?fù)?dān)心意外共享私有內(nèi)容。

您的 secrets.py 文件應(yīng)如下所示:

下載:文件

復(fù)制代碼

# 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是一個(gè)名為secrets的python字典,每個(gè)條目都有一行。每個(gè)條目都有一個(gè)條目名稱(例如‘ssid’),然后是一個(gè)冒號(hào)以將其與條目鍵‘home ssid’分開,最后是一個(gè)逗號(hào),

至少您需要ssid和password用于您的本地WiFi設(shè)置。在創(chuàng)建項(xiàng)目時(shí),您可能需要更多的令牌和密鑰,只需一次添加一行即可。例如,請(qǐng)參見其他令牌,例如用于訪問(wèn)github或hackaday API的令牌。其他非機(jī)密數(shù)據(jù)(例如您的時(shí)區(qū))也可以在此處,僅因?yàn)槠浞Q為機(jī)密的信息并不意味著您不能在其中包含常規(guī)的自定義數(shù)據(jù)!

有關(guān)正確的時(shí)區(qū)字符串,請(qǐng)查看http://worldtimeapi.org/timezones,請(qǐng)記住,如果未列出您的城市,請(qǐng)?jiān)谕粫r(shí)區(qū)中查找城市,例如,波士頓,紐約,費(fèi)城,華盛頓特區(qū)和邁阿密都與紐約位于同一時(shí)間。

當(dāng)然,不要共享您的 secrets.py -將其保存在GitHub,Discord或其他項(xiàng)目共享站點(diǎn)之外。

連接到WiFi

好,現(xiàn)在您已設(shè)置好機(jī)密-您可以連接到Internet。讓我們使用ESP32SPI和Requests庫(kù)-您需要訪問(wèn)CircuitPython捆綁包并安裝:

adafruit_bus_device

adafruit_esp32spi

adafruit_requests

neopixel

進(jìn)入您的lib文件夾。完成后,使用Mu或您喜歡的編輯器加載以下示例:

下載:項(xiàng)目郵編或 esp32spi_simpletest.py | 在Github上查看

復(fù)制代碼

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。

第一個(gè)連接示例不使用機(jī)密文件-您將首先輸入SSID/密碼來(lái)驗(yàn)證連接性!

然后轉(zhuǎn)到此行

esp.connect_AP(b‘MY_SSID_NAME’, b‘MY_SSID_PASSWORD’)

并更改MY_SSID_NAME和 MY_SSID_PASSWORD 設(shè)置為您的訪問(wèn)點(diǎn)名稱和密碼,并將其保留在‘’引號(hào)內(nèi)。 (此示例不使用機(jī)密文件,但它也非常獨(dú)立,因此,如果其他事情似乎不起作用,則可以始終重新加載該文件。您應(yīng)該獲得如下內(nèi)容:

示例代碼。..

使用SPI端口和3個(gè)控制引腳通過(guò)SPI初始化ESP32:

下載:文件

復(fù)制代碼

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庫(kù)我們正在使用的套接字的類型(套接字類型隨連接類型而變化-在此示例中,我們將使用adafruit_esp32spi_socket)。還將接口設(shè)置為

下載:文件

復(fù)制代碼

esp requests

驗(yàn)證找到ESP32,檢查固件和MAC地址

下載:文件

復(fù)制代碼

requests.set_socket(socket, esp) requests.set_socket(socket, esp)

掃描它可以看到的所有訪問(wèn)點(diǎn),并打印出名稱和信號(hào)強(qiáng)度:

下載:文件

復(fù)制代碼

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])

連接到我們?cè)诖颂幎x的AP,然后打印出來(lái)本地IP地址,嘗試進(jìn)行域名查找并ping google.com以檢查網(wǎng)絡(luò)連接(請(qǐng)注意,有時(shí)ping失敗或需要一段時(shí)間,這沒(méi)什么大不了的)

下載:文件

復(fù)制代碼

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’]))

好的,現(xiàn)在我們進(jìn)入真正有趣的部分。使用SAMD51或其他大容量RAM(超過(guò)32 KB)設(shè)備,我們可以做很多巧妙的事情。例如,我們可以像請(qǐng)求一樣實(shí)現(xiàn)一個(gè)接口,這使得獲取數(shù)據(jù)非常簡(jiǎn)單

從Web URL調(diào)用 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

下載:文件

復(fù)制代碼

requests.get https

或者,如果數(shù)據(jù)使用結(jié)構(gòu)化JSON,則可以獲取json pre -解析為可以輕松查詢或遍歷的Python字典。 (同樣,僅適用于nRF52840,M4和其他高RAM板)

下載:文件

復(fù)制代碼

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

我們已經(jīng)為Web接口編寫了一個(gè)類似請(qǐng)求的庫(kù),名為Adafruit_CircuitPython_Requests。該庫(kù)允許您發(fā)送HTTP/1.1請(qǐng)求,而無(wú)需“設(shè)計(jì)”它們,并提供了有用的方法來(lái)解析服務(wù)器的響應(yīng)。

下載:Project Zip 或 requests_simpletest.py | 在Github上查看

復(fù)制代碼

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()

代碼首先設(shè)置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()

對(duì)象初始化request對(duì)象。

下載:文件

復(fù)制代碼

socket esp

帶有請(qǐng)求的HTTP GET

代碼向Adafruit的WiFi測(cè)試網(wǎng)站-http:/發(fā)出HTTP GET請(qǐng)求/wifitest.adafruit.com/testwifi/index.html。

為此,我們會(huì)將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)。我們還將將來(lái)自服務(wù)器的響應(yīng)保存到名為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)的變量。

盡管我們從服務(wù)器請(qǐng)求數(shù)據(jù),但我們還是希望服務(wù)器回應(yīng)。由于我們已經(jīng)保存了服務(wù)器的requests.get(),因此可以將其讀回。對(duì)我們來(lái)說(shuō)幸運(yùn)的是,請(qǐng)求將服務(wù)器的響應(yīng)自動(dòng)解碼為人類可讀的文本,您可以通過(guò)調(diào)用response將其讀回。

最后,我們將執(zhí)行一些操作通過(guò)調(diào)用response進(jìn)行清理。這樣會(huì)關(guān)閉,刪除和收集響應(yīng)的數(shù)據(jù)。

下載:文件

復(fù)制代碼

response.text response.close()

一些服務(wù)器使用文本響應(yīng),而某些服務(wù)器則使用由屬性-值對(duì)組成的json格式的數(shù)據(jù)進(jìn)行響應(yīng)。/p》

CircuitPython_Requests可以將來(lái)自服務(wù)器的JSON格式的響應(yīng)轉(zhuǎn)換為CPython print(“Fetching text from %s”%TEXT_URL)

response = requests.get(TEXT_URL)

print(‘-’*40)

print(“Text Response: ”, response.text)

print(‘-’*40)

response.close()對(duì)象。

我們還可以獲取和解析 json 數(shù)據(jù)。我們將發(fā)送HTTP Get到一個(gè)我們知道的URL,該URL返回一個(gè)json格式的響應(yīng)(而不是文本數(shù)據(jù))。

然后,代碼調(diào)用print(“Fetching text from %s”%TEXT_URL)

response = requests.get(TEXT_URL)

print(‘-’*40)

print(“Text Response: ”, response.text)

print(‘-’*40)

response.close()將響應(yīng)轉(zhuǎn)換為一個(gè)CPython dict.。

下載:文件

復(fù)制代碼

response.json() dict

帶有請(qǐng)求的HTTP POST

請(qǐng)求還可以通過(guò)調(diào)用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()方法(向其傳遞一個(gè)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()值)將數(shù)據(jù)發(fā)布到服務(wù)器。

下載:文件

復(fù)制代碼

requests.post data

您還可以通過(guò)將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格式的數(shù)據(jù)發(fā)布到服務(wù)器。

下載:文件

復(fù)制代碼

json_data requests.post

高級(jí)請(qǐng)求使用情況

要發(fā)送自定義HTTP, aders,將響應(yīng)解析為原始字節(jié),還是在CircuitPython代碼中處理響應(yīng)的http狀態(tài)代碼?

我們已經(jīng)編寫了一個(gè)示例來(lái)顯示下面的請(qǐng)求模塊的高級(jí)用法。

下載:項(xiàng)目Zip 或 requests_advanced.py | 在Github上查看

復(fù)制代碼

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

這個(gè)最簡(jiǎn)單的示例可行,但有點(diǎn)挑剔-您需要不斷檢查WiFi狀態(tài),并具有許多循環(huán)來(lái)管理連接和斷開連接。對(duì)于更高級(jí)的用途,我們建議使用WiFiManager對(duì)象。它將為您包裝連接/狀態(tài)/請(qǐng)求循環(huán)-如果WiFi掉線,則重新連接,如果ESP32進(jìn)入不良狀態(tài),則重置ESP32,等等。

這是一個(gè)更高級(jí)的示例,其中顯示了WiFi管理器以及如何使用一些額外的標(biāo)題發(fā)布數(shù)據(jù):

下載:Project Zip 或 esp32spi_aio_post.py | 在Github上查看

復(fù)制代碼

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()

您會(huì)在這里注意到,我們使用secrets.py文件來(lái)管理我們的SSID信息。 wifimanager被賦予了ESP32對(duì)象,密碼和一個(gè)用于指示狀態(tài)的新像素。

請(qǐng)注意,您需要在密碼文件中添加一些其他信息,以便代碼可以查詢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[‘a(chǎn)io_username’]+“/feeds/”+feed+“/data”,

json=payload,

headers={“X-AIO-KEY”:secrets[‘a(chǎn)io_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[‘a(chǎn)io_username’]+“/feeds/”+feed+“/data”,

json=payload,

headers={“X-AIO-KEY”:secrets[‘a(chǎn)io_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)

您可以轉(zhuǎn)到adafruit.io查看AIO密鑰鏈接獲取這兩個(gè)值并將它們添加到secrets文件中,該文件現(xiàn)在看起來(lái)像這樣:

下載:文件

復(fù)制代碼

aio_username aio_key

下一步,設(shè)置一個(gè)名為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

‘a(chǎn)io_username’ : ‘_your_aio_username_’,

‘a(chǎn)io_key’ : ‘_your_aio_key_’,

}

如果您不知道如何設(shè)置供稿,請(qǐng)遵循此頁(yè)面并在設(shè)置名為。

然后,我們可以有一個(gè)簡(jiǎn)單的循環(huán),用于將數(shù)據(jù)發(fā)布到Adafruit IO,而無(wú)需處理連接或初始化硬件!

在Adafruit.io上查看您的測(cè)試數(shù)據(jù),每次Cir時(shí),您都會(huì)看到該值增加cuitPython板向其發(fā)布數(shù)據(jù)!

代碼PyPortal NASA圖像查看器

NASA Open API密鑰

我們將使用NASA API進(jìn)行查詢并檢索當(dāng)天的圖片。為此,您需要在 api.nasa.org 注冊(cè)一個(gè)免費(fèi)帳戶并獲取您的API密鑰。

這很容易做到,只需轉(zhuǎn)到此鏈接,然后輸入您的姓名和電子郵件地址。這里的所有都是它的!您的API密鑰將生成并通過(guò)電子郵件發(fā)送給您,并立即顯示在瀏覽器中。保持該鍵(或包含它的電子郵件)方便,我們需要稍后將其復(fù)制并粘貼到我們的代碼中。

Adafruit IO時(shí)間服務(wù)器

為了使用Adafruit圖像轉(zhuǎn)換器,該項(xiàng)目將要求您擁有Adafruit IO用戶名和密鑰。 Adafruit IO是絕對(duì)免費(fèi)的,但是您需要使用Adafruit帳戶登錄才能使用它。如果您還沒(méi)有Adafruit登錄名,請(qǐng)?jiān)诖颂巹?chuàng)建一個(gè)登錄名。

如果您以前從未使用過(guò)Adafruit IO,請(qǐng)查看本指南以獲取更多信息。

登錄到您的帳戶后,您需要在 secrets.py 文件中放置以下兩項(xiàng)信息: Adafruit IO用戶名和 Adafruit IO鍵。前往io.adafruit.com,只需點(diǎn)擊Adafruit IO頁(yè)面左側(cè)的查看AIO密鑰鏈接即可獲取此信息。

然后將它們添加到 secrets.py 文件,例如:

下載:文件

復(fù)制代碼

secrets = {

‘ssid’ : ‘your_wifi_ssid’,

‘password : ’your_wifi_password‘,

’aio_username‘ : ’your_aio_username‘,

’aio_key‘ : ’your_big_huge_super_long_aio_key‘

} secrets = {

’ssid‘ : ’your_wifi_ssid‘,

’password : ‘your_wifi_password’,

‘a(chǎn)io_username’ : ‘your_aio_username’,

‘a(chǎn)io_key’ : ‘your_big_huge_super_long_aio_key’

}

添加CircuitPython代碼和資產(chǎn)

在下面的嵌入式代碼元素中,單擊下載:項(xiàng)目Zip 鏈接,然后保存.zip存檔文件。

然后解壓縮.zip文件,它將解壓縮到名為 PyPortal_NASA 的文件夾中。

復(fù)制 PyPortal_NASA 目錄到PyPortal的 CIRCUITPY 驅(qū)動(dòng)器,然后確保將 nasa.py 文件重命名為 code.py ,當(dāng)PyPortal重新啟動(dòng)時(shí),它將自動(dòng)運(yùn)行。

編輯代碼

您可以使用任何喜歡的文本編輯器來(lái)編輯 code.py 文件。 Adafruit建議安裝免費(fèi)的Mu Python編輯器,因?yàn)樗?jí)方便,可以識(shí)別Adafruit板,并具有內(nèi)置的串行監(jiān)視器/REPL以與板交互。在此處了解有關(guān)Mu的更多信息。

添加NASA API密鑰

在Mu中打開 code.py ,然后從我們之前獲得的NASA復(fù)制并粘貼您的API密鑰到DATA_SOURCE網(wǎng)址中,

該行應(yīng)如下所示:

DEMO_KEY

完成后,再次將 code.py 文件保存到PyPortal的 CIRCUITPY 驅(qū)動(dòng)器中。

boot.py

我們正在使用一個(gè)特殊文件來(lái)確保.bmp緩存寫入正確閃爍。這是您復(fù)制到驅(qū)動(dòng)器的 unsafe_boot.py 文件?,F(xiàn)在將其重命名為 boot.py 。

請(qǐng)注意,重啟后會(huì)出現(xiàn)這種看上去很恐怖的文字,請(qǐng)放心,這應(yīng)該是就是這么說(shuō)!

****************警告******************

將文件系統(tǒng)用作可寫緩存!

這是冒險(xiǎn)行為,請(qǐng)備份文件!

** **************警告******************

這是 CIRCUITPY 驅(qū)動(dòng)器的最終內(nèi)容,如下所示:

下載:Project Zip 或 nasa.py | 在Github上查看

復(fù)制代碼

DATA_SOURCE = “https://api.nasa.gov/planetary/apod?api_key=your_actual_really_long_key_here” import time

import board

from adafruit_pyportal import PyPortal

# Set up where we‘ll be fetching data from

DATA_SOURCE = “https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY”

# There’s a few different places we look for data in the photo of the day

IMAGE_LOCATION = [“url”]

TITLE_LOCATION = [“title”]

DATE_LOCATION = [“date”]

# the current working directory (where this file is)

cwd = (“/”+__file__).rsplit(‘/’, 1)[0]

pyportal = PyPortal(url=DATA_SOURCE,

json_path=(TITLE_LOCATION, DATE_LOCATION),

status_neopixel=board.NEOPIXEL,

default_bg=cwd+“/nasa_background.bmp”,

text_font=cwd+“/fonts/Arial-12.bdf”,

text_position=((5, 220), (5, 200)),

text_color=(0xFFFFFF, 0xFFFFFF),

text_maxlen=(50, 50), # cut off characters

image_json_path=IMAGE_LOCATION,

image_resize=(320, 240),

image_position=(0, 0))

while True:

response = None

try:

response = pyportal.fetch()

print(“Response is”, response)

except RuntimeError as e:

print(“Some error occured, retrying! -”, e)

time.sleep(30*60) # 30 minutes till next check

如果遇到任何錯(cuò)誤,例如“ ImportError:沒(méi)有名為` adafruit_display_text.label`”,請(qǐng)務(wù)必將您的庫(kù)更新到最新ST發(fā)布包!

工作原理

NASA Image Viewer正在使用CircuitPython和PyPortal做一些很酷的事情:

背景閃屏

首先,我們將顯示帶有NASA徽標(biāo)的初始屏幕。這是 .bmp 格式的320x240像素RGB 16位光柵圖形。

字體

我們將圖像標(biāo)題和今天的日期顯示為使用位圖字體創(chuàng)建的文本加載后疊加在背景圖片上。這里使用的字體是由斜Arial字體制成的位圖字體。您可以在本指南中了解有關(guān)轉(zhuǎn)換類型的更多信息。

JSON

為了檢索當(dāng)天的NASA天文學(xué)圖片(APOD),我們

向NASA API進(jìn)行查詢。

在添加了API密鑰的APOD服務(wù)器請(qǐng)求中,您將獲得一個(gè)JSON文件作為響應(yīng)。

實(shí)際上,您可以運(yùn)行與PyPortal相同的查詢來(lái)查看結(jié)果。將此鏈接import time

import board

from adafruit_pyportal import PyPortal

# Set up where we‘ll be fetching data from

DATA_SOURCE = “https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY”

# There’s a few different places we look for data in the photo of the day

IMAGE_LOCATION = [“url”]

TITLE_LOCATION = [“title”]

DATE_LOCATION = [“date”]

# the current working directory (where this file is)

cwd = (“/”+__file__).rsplit(‘/’, 1)[0]

pyportal = PyPortal(url=DATA_SOURCE,

json_path=(TITLE_LOCATION, DATE_LOCATION),

status_neopixel=board.NEOPIXEL,

default_bg=cwd+“/nasa_background.bmp”,

text_font=cwd+“/fonts/Arial-12.bdf”,

text_position=((5, 220), (5, 200)),

text_color=(0xFFFFFF, 0xFFFFFF),

text_maxlen=(50, 50), # cut off characters

image_json_path=IMAGE_LOCATION,

image_resize=(320, 240),

image_position=(0, 0))

while True:

response = None

try:

response = pyportal.fetch()

print(“Response is”, response)

except RuntimeError as e:

print(“Some error occured, retrying! -”, e)

time.sleep(30*60) # 30 minutes till next check

復(fù)制并粘貼到您的瀏覽器中,除了將DEMO_KEY替換為NASA在注冊(cè)電子郵件中發(fā)送的自己的API密鑰。

當(dāng)您在在網(wǎng)絡(luò)瀏覽器中,您將看到如下返回結(jié)果:

下載:文件

復(fù)制代碼

https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY {

“copyright”: “Mario Zauner”,

“date”: “2019-04-03”,

“explanation”: “The famous Horsehead Nebula in Orion is not alone. A deep exposure shows that the dark familiar shaped indentation, visible just below center, is part of a vast complex of absorbing dust and glowing gas. To bring out details of the Horsehead‘s pasture, an amateur astronomer used a backyard telescope in Austria to accumulate and artistically combine 7.5 hours of images in the light of Hydrogen (red), Oxygen (green), and Sulfur (blue)。 The resulting spectacular picture details an intricate tapestry of gaseous wisps and dust-laden filaments that were created and sculpted over eons by stellar winds and ancient supernovas. The Flame Nebula is visible just to the left of the Horsehead, while the bright star on the upper left is Alnilam, the central star in Orion’s Belt. The Horsehead Nebula lies 1,500 light years distant towards the constellation of Orion. Bounce around the Universe: Random APOD Generator”,

“hdurl”: “https://apod.nasa.gov/apod/image/1904/HorseheadFlame_Zauner_4404.jpg”,

“media_type”: “image”,

“service_version”: “v1”,

“title”: “Wisps Surrounding the Horsehead Nebula”,

“url”: “https://apod.nasa.gov/apod/image/1904/HorseheadFlame_Zauner_960.jpg”

}

該結(jié)果是一個(gè)JSON(JavaScript對(duì)象表示法)數(shù)組。它由具有七個(gè) key:value 對(duì)的單個(gè)元素組成。例如,有一個(gè)名為{

“copyright”: “Mario Zauner”,

“date”: “2019-04-03”,

“explanation”: “The famous Horsehead Nebula in Orion is not alone. A deep exposure shows that the dark familiar shaped indentation, visible just below center, is part of a vast complex of absorbing dust and glowing gas. To bring out details of the Horsehead‘s pasture, an amateur astronomer used a backyard telescope in Austria to accumulate and artistically combine 7.5 hours of images in the light of Hydrogen (red), Oxygen (green), and Sulfur (blue)。 The resulting spectacular picture details an intricate tapestry of gaseous wisps and dust-laden filaments that were created and sculpted over eons by stellar winds and ancient supernovas. The Flame Nebula is visible just to the left of the Horsehead, while the bright star on the upper left is Alnilam, the central star in Orion’s Belt. The Horsehead Nebula lies 1,500 light years distant towards the constellation of Orion. Bounce around the Universe: Random APOD Generator”,

“hdurl”: “https://apod.nasa.gov/apod/image/1904/HorseheadFlame_Zauner_4404.jpg”,

“media_type”: “image”,

“service_version”: “v1”,

“title”: “Wisps Surrounding the Horsehead Nebula”,

“url”: “https://apod.nasa.gov/apod/image/1904/HorseheadFlame_Zauner_960.jpg”

}的鍵,它的值為date,表示方式如下:

2019-04-03

我們關(guān)心的另一個(gè)是“date”: “2019-04-03” 鍵,其值為title

表示為:

Wisps Surrounding the Horsehead Nebula

我們真正關(guān)心的另一個(gè)鍵是“title”: “Wisps Surrounding the Horsehead Nebula”具有圖像位置的值:

url

由于此JSON對(duì)象數(shù)組具有一致的返回結(jié)果的方式對(duì)我們來(lái)說(shuō),我們?cè)赑yPortal上運(yùn)行的代碼可以輕松解析數(shù)據(jù)并將其顯示!

這是Firefox的“代碼美化器”中JSON文件的外觀。

您可以在 code.py 的此部分中了解其操作方式:

下載:文件

復(fù)制代碼

“url”: “https://apod.nasa.gov/apod/image/1904/HorseheadFlame_Zauner_960.jpg” DATA_SOURCE = “https://api.nasa.gov/planetary/apod?api_key=xxxxxxx” #put your api key here

IMAGE_LOCATION = [“url”]

TITLE_LOCATION = [“title”]

DATE_LOCATION = [“date”]

然后,在DATA_SOURCE = “https://api.nasa.gov/planetary/apod?api_key=xxxxxxx” #put your api key here

IMAGE_LOCATION = [“url”]

TITLE_LOCATION = [“title”]

DATE_LOCATION = [“date”]查詢,我們要求從該URL中獲取日期和 title 名稱,然后使用pyportal參數(shù)設(shè)置text_ ,font,position,color和wrap文本顯示時(shí)。

我們使用 url 名稱以獲取.jpeg圖像文件的路徑。

下載:文件

復(fù)制代碼

maxlen pyportal = PyPortal(url=DATA_SOURCE,

json_path=(TITLE_LOCATION, DATE_LOCATION),

status_neopixel=board.NEOPIXEL,

default_bg=cwd+“/nasa_background.bmp”,

text_font=cwd+“/fonts/Arial-12.bdf”,

text_position=((5, 220), (5, 200)),

text_color=(0xFFFFFF, 0xFFFFFF),

text_maxlen=(50, 50), # cut off characters

image_json_path=IMAGE_LOCATION,

image_resize=(320, 240),

image_position=(0, 0))

所有這些都準(zhǔn)備好了,在pyportal = PyPortal(url=DATA_SOURCE,

json_path=(TITLE_LOCATION, DATE_LOCATION),

status_neopixel=board.NEOPIXEL,

default_bg=cwd+“/nasa_background.bmp”,

text_font=cwd+“/fonts/Arial-12.bdf”,

text_position=((5, 220), (5, 200)),

text_color=(0xFFFFFF, 0xFFFFFF),

text_maxlen=(50, 50), # cut off characters

image_json_path=IMAGE_LOCATION,

image_resize=(320, 240),

image_position=(0, 0))的主循環(huán)中,代碼將查詢

當(dāng)獲取.jpeg文件的路徑時(shí),pyportal庫(kù)將其傳遞到Adafruit IO圖像轉(zhuǎn)換器服務(wù)器,在該服務(wù)器中文件被轉(zhuǎn)換為PyPortal可以使用的格式顯示屏,一個(gè)320x240像素RGB 16位.bmp。

原始jpeg

轉(zhuǎn)換并調(diào)整大小的bmp

此圖像然后將e緩存到PyPortal的存儲(chǔ)中并顯示在PyPortal TFT屏幕上。

最后,文本將顯示在圖像上。

此更新每三十分鐘。請(qǐng)注意,您每天對(duì)NASA API的查詢不能超過(guò)50個(gè)!

責(zé)任編輯:wv

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 編碼
    +關(guān)注

    關(guān)注

    6

    文章

    945

    瀏覽量

    54850
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    基于OpenCV的拆分和合并圖像通道實(shí)驗(yàn)案例分享_基于RK3568教學(xué)實(shí)驗(yàn)箱

    不要上電。 軟件操作 (1)先在設(shè)備管理查看串口的端口號(hào); (2)再設(shè)置串口調(diào)試工具,波特率設(shè)置1500000,點(diǎn)擊連接,在Xshel
    發(fā)表于 12-03 14:27

    KiCon 演講回顧(九):基于 Web 的 EDA 查看器及 AI 助手

    “ ?由 KiCad Lead Developer Ethan 帶來(lái)的精彩演講。介紹了在線設(shè)計(jì)查看器及 CoPilot 的應(yīng)用,并詳細(xì)展示了實(shí)現(xiàn)的原理。 ? ” ? 完整的演講視頻在這里
    的頭像 發(fā)表于 11-28 15:25 ?1423次閱讀
    KiCon 演講回顧(九):基于 Web 的 EDA <b class='flag-5'>查看器</b>及 AI 助手

    KiCad發(fā)現(xiàn)之旅(三)PCB 3D查看器

    “ ?本文轉(zhuǎn)載自KiCad官網(wǎng),通過(guò)幾個(gè)簡(jiǎn)短的視頻,幫助大家快速了解KiCad的基礎(chǔ)功能。如需了解更多關(guān)于KiCad的咨詢,請(qǐng)參考: KiCad常用資源? ” 利用KiCad的集成3D查看器以3D
    的頭像 發(fā)表于 11-12 12:23 ?621次閱讀
    KiCad發(fā)現(xiàn)之旅(三)PCB 3D<b class='flag-5'>查看器</b>

    KiCad發(fā)現(xiàn)之旅(四)Gerber查看器

    “ ?本文轉(zhuǎn)載自KiCad官網(wǎng),通過(guò)幾個(gè)簡(jiǎn)短的視頻,幫助大家快速了解KiCad的基礎(chǔ)功能。如需了解更多關(guān)于KiCad的資訊,請(qǐng)參考: KiCad常用資源? ” Gerber查看器可以幫助您在發(fā)送到板
    的頭像 發(fā)表于 11-12 12:23 ?500次閱讀
    KiCad發(fā)現(xiàn)之旅(四)Gerber<b class='flag-5'>查看器</b>

    自己html網(wǎng)站如何嵌入DigiPCBA 查看器

    官網(wǎng)給的DigiPCBA 查看器嵌入自己的網(wǎng)站代碼有欠缺,請(qǐng)問(wèn)大佬們,如何把DigiPCBA 查看器嵌入到自己的網(wǎng)站中,iframe會(huì)有跨域問(wèn)題
    發(fā)表于 09-02 16:28

    開源 KiCad/AD 查看器 + AI Copilot?

    先簡(jiǎn)單總結(jié)下這個(gè)項(xiàng)目的主要功能: 這是一個(gè)基于 Web 的 EDA 查看器,支持查看 KiCad 和 Altium Designer 的二進(jìn)制設(shè)計(jì)圖紙(和 Altium 365 查看器 非常類似
    發(fā)表于 08-18 22:41

    安寶特產(chǎn)品 安寶特3D Analyzer:高級(jí) 3D CAD 查看器與分析軟件工具

    3D_Analyzer 查看器是完美的工具,它可以獨(dú)立地審查和分析所有主流 CAD 原生和標(biāo)準(zhǔn)格式的模型,而不需要原生CAD原生系統(tǒng)軟件。我們的查看器具有獨(dú)特的功能和直觀的用戶界面,是當(dāng)今市場(chǎng)上一流的產(chǎn)品。
    的頭像 發(fā)表于 08-06 15:31 ?379次閱讀
    安寶特產(chǎn)品  安寶特3D Analyzer:高級(jí) 3D CAD <b class='flag-5'>查看器</b>與分析軟件工具

    DSP教學(xué)實(shí)驗(yàn)箱_數(shù)字圖像處理操作_案例分享:5-13 灰度圖像二值化

    一、實(shí)驗(yàn)?zāi)康?學(xué)習(xí)灰度圖像二值化的原理,掌握圖像的讀取方法,并實(shí)現(xiàn)在LCD上顯示二值化前后的圖像。 二、實(shí)驗(yàn)原理 圖像二值化 圖像的二值化處
    發(fā)表于 07-25 15:03

    基于DSP C6000教學(xué)實(shí)驗(yàn)箱_數(shù)字圖像處理教程:5-9 圖像反色實(shí)驗(yàn)案例分享

    目錄下的BMP圖像,接著進(jìn)行反色處理,最后保存圖像。 三、操作現(xiàn)象 本實(shí)驗(yàn)所需硬件實(shí)驗(yàn)板、仿真和電源。 硬件連接 (1)連接仿真
    發(fā)表于 07-19 09:57

    DSP教學(xué)實(shí)驗(yàn)箱_數(shù)字圖像處理_操作教程:5-1 圖像旋轉(zhuǎn)

    ,接著進(jìn)行旋轉(zhuǎn)處理,最后保存旋轉(zhuǎn)后的圖像。 三、操作現(xiàn)象 本實(shí)驗(yàn)所需硬件實(shí)驗(yàn)板、仿真和電源。 硬件連接 (1)連接仿真和電腦的USB接口, (2)
    發(fā)表于 06-14 14:03

    TC397在系統(tǒng)需要時(shí)MCU設(shè)置睡眠模式遇到的疑問(wèn)求解

    回 1? 2、請(qǐng)查看下圖,喚醒源代碼登錄后,除主處理外,其他 CPU 保持空閑狀態(tài)。 我是否需要通過(guò)開關(guān) CPU 與主控分開
    發(fā)表于 05-27 08:28

    怎么在網(wǎng)頁(yè)上嵌入DigiPCBA查看器

    請(qǐng)問(wèn)怎么在網(wǎng)頁(yè)上嵌入DigiPCBA查看器,要在網(wǎng)頁(yè)的<head>部分放入什么代碼?DigiPCBA官網(wǎng)的幫助文檔里有提到,但是沒(méi)有相關(guān)代碼,有人能提供下嗎
    發(fā)表于 04-20 15:20

    Sensor如何mipi端的設(shè)置YUv格式?

    我的Sensor輸出是raw8圖像,如何 mipi 端的設(shè)置YUv格式,而不是UYVY格式。
    發(fā)表于 02-29 08:03

    怎樣查看input/output delay是否生效

    通過(guò)get_port命令查看接口。 get_ports * 以LVDS的輸入輸出怎樣查看outputdelay ? set_output_delay -clock
    的頭像 發(fā)表于 02-27 08:38 ?739次閱讀
    <b class='flag-5'>怎樣</b><b class='flag-5'>查看</b>input/output delay是否生效

    dns的服務(wù)地址設(shè)置多少

    DNS(域名系統(tǒng))是互聯(lián)網(wǎng)的基礎(chǔ)設(shè)施之一,用于域名轉(zhuǎn)換為IP地址,使用戶可以訪問(wèn)網(wǎng)絡(luò)資源。要設(shè)置DNS服務(wù)地址,首先需要理解DNS的工作原理和不同類型的DNS服務(wù)。本文
    的頭像 發(fā)表于 01-15 11:11 ?1778次閱讀
    主站蜘蛛池模板: 久久精品国产免费中文| 91国在线产| 超碰免费视频caopoom9| 两个洞一起插哦!好刺激| 日韩亚射吧| BLACKED太粗太长| 麻豆啊传媒app黄版破解免费| 亚洲无吗在线视频| 国语自产拍在线视频普通话| 午夜十八岁禁| 国产乱人伦AV麻豆网| 人妻精品久久无码专区| 99视频网址| 暖暖日本在线手机免费完整版| 26uuu老色哥| 免费观看男生桶美女私人部位| 91精品国产91| 免费韩伦影院在线观看| 91国内精品久久久久免费影院| 秘密教学26我们在做一次吧免费| 6080伦理久久亚洲精品| 男人有噶坏| xhameter中国| 日韩一卡二卡三卡四卡免费观在线| 俄罗斯美女破处| 添加一点爱与你电视剧免费观看| 国产精品99久久久久久WWW| 午夜亚洲精品不卡在线| 国产亚洲免费观看| 亚洲视频网站欧美视频网站| 久久精品18| 91热久久免费频精品动漫99| 欧美高清69vivo| 88蜜桃人妻无码精品系列| 免费精品国产日韩热久久| 99久久精品国产一区二区三区 | 久久久久琪琪精品色| 69精品人妻一区二区三区蜜桃| 欧美zzzoooxxx| 纯肉合集(高H)| 学生无码AV一区二区三区|