python 开发板 i2s_Teensy 4.0 开发板

这篇博客演示了如何在Teensy 4.0开发板上使用CircuitPython进行网络连接,通过ESP32模块访问JSON API并显示数据在I2C连接的SSD1306 OLED屏幕上。首先,移植CircuitPython到Teensy 4.0,然后设置ESP32连接AP,获取IP地址,并展示网络信息。接着,从指定URL获取JSON数据,将其内容适配到OLED屏幕的显示限制。最后,不断更新屏幕显示,并在一段时间后刷新。
摘要由CSDN通过智能技术生成

CircuitPython 已经移植到 Teensy 4.0

import time

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

import adafruit_ssd1306

print("ESP32 SPI webclient test")

JSON_URL = "https://www.adafruit.com/api/quotes.php"

i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)

display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c)

# If you have an externally connected ESP32:

esp32_cs = DigitalInOut(board.D5)

esp32_reset = DigitalInOut(board.D6)

esp32_ready = DigitalInOut(board.D9)

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

display.fill(0)

display.text("Found ESP32", 0, 0, 1)

display.show()

for ap in esp.scan_networks():

print("\t%s\t\tRSSI: %d" % (str(ap['ssid'], 'utf-8'), ap['rssi']))

print("Connecting to AP...")

display.text("Connecting to AP", 0, 8, 1)

display.show()

while not esp.is_connected:

try:

esp.connect_AP(b'adafruit', b'ffffffff')

except RuntimeError as e:

print("could not connect to AP, retrying: ",e)

continue

print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", 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"))

display.text("Connected: "+str(esp.ssid, 'utf-8'), 0, 16, 1)

display.text("IP: "+esp.pretty_ip(esp.ip_address), 0, 24, 1)

display.show()

def wrap_nicely(string, max_chars):

"""A helper that will return a list of lines with word-break wrapping.

:param str string: The text to be wrapped.

:param int max_chars: The maximum number of characters on a line before wrapping.

"""

string = string.replace('\n', '').replace('\r', '') # strip confusing newlines

words = string.split(' ')

the_lines = []

the_line = ""

for w in words:

if len(the_line+' '+w) <= max_chars:

the_line += ' '+w

else:

the_lines.append(the_line)

the_line = ''+w

if the_line: # last line remaining

the_lines.append(the_line)

# remove first space from first line:

the_lines[0] = the_lines[0][1:]

return the_lines

while True:

print()

print("Fetching json from", JSON_URL)

r = requests.get(JSON_URL)

j = r.json()

print('-'*40)

print(j)

print('-'*40)

quote = j[0]['text']

author = j[0]['author']

r.close()

display.fill(0)

lines = wrap_nicely(quote, 21)

lines.append(" ")

lines.append(author)

print(lines)

for i, line in enumerate(lines[:-2]):

display.fill(0)

display.text(line, 0, 0, 1)

if len(lines) > i+1:

display.text(lines[i+1], 0, 8, 1)

if len(lines) > i+2:

display.text(lines[i+2], 0, 16, 1)

if len(lines) > i+3:

display.text(lines[i+3], 0, 24, 1)

display.show()

time.sleep(0.5)

time.sleep(3)

print("Done!")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值