「Raspberry pico w」:修訂間差異

出自TSK WIKI
跳至導覽 跳至搜尋
 
(未顯示同一使用者於中間所作的 8 次修訂)
第3行: 第3行:
*[https://www.youtube.com/watch?v=onBkPkaqDnk Raspberry Pi Pico - USB HID Auto Clicker with Circuit Python ]
*[https://www.youtube.com/watch?v=onBkPkaqDnk Raspberry Pi Pico - USB HID Auto Clicker with Circuit Python ]
**[https://circuitpython.org/downloads CitPython downloads]
**[https://circuitpython.org/downloads CitPython downloads]
*[https://www.youtube.com/watch?v=Lwr8LLvLRb8 Feel the noise on a Raspberry Pi Pico - learn to play .wav and .mp3 files in CircuitPython]
**[https://drive.google.com/drive/folders/1VkvuRZguE6g79imw_4XpzSaFeeDjCHxR circuitpython-school-files]
*[https://hackmd.io/@meebox/BymP1kQSu#CircuitPython-%E5%85%A5%E9%96%80%E2%80%93%E4%BD%BF%E7%94%A8-Raspberry-Pi-Pico CircuitPython 入門–使用 Raspberry Pi Pico]
==學習方向==
*wifi連網
**http
*** TTS
*** Barcode toQL
*mp3player 用來撥放米數音檔【done】
** GPIO14 + GND to SOCKET
*Barcord scaner
**bluetooth
**usb
*MQTT publisher
*WS2812B LED燈條驅動
**neopixel
*外接電源
==line notify push==
<python>
import os
import ssl
import adafruit_requests
import socketpool
import wifi
token = "token"
message = "How are you!"
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
response = requests.post(
    "https://notify-api.line.me/api/notify",
    headers={"Authorization": f"Bearer {token}"},
    data={"message": message},
)
</python>
==online mp3 playe==
<python>
import adafruit_requests
import wifi
import socketpool
import ssl
import board
import audiomp3
import os
from audiopwmio import PWMAudioOut as AudioOut
audio = AudioOut(board.GP14)
mp3_url = 'http://192.168.1.100:8080/IOT/mp3/0.19.mp3'
# chunk size for downloading the mp3 file in parts
chunk_size = 1024
# connects to SSID
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# requests the url and writes the mp3 to a file
try:
    doc = requests.get(mp3_url)
    # opens the file in binary write mode
    with open('sound.mp3', 'wb') as f:
    # writes the file in chunks
        for chunk in doc.iter_content(chunk_size=chunk_size):
            f.write(chunk)
except Exception as e:
    print("Connection error:\n", str(e))
# plays the mp3 file from flash memory
try:
    # creates an mp3 object
    mp3 = audiomp3.MP3Decoder(open("sound.mp3", "rb"))
    # optimizes the sample rate for your setup before playing
    mp3.sample_rate=24000
    # mp3.bits_per_sample=50
    audio.play(mp3)
    while audio.playing:
        pass
    print("Done playing!")
except Exception as e:
    print("Error in creating mp3 or playing audio:\n", str(e))
</python>
<python>
#pico檔案系統預設唯讀,需用boot.py去關掉
#1.需短路GPIO0和GND
#2.先VSYS送電再連USB到電腦
#boot.py內容如下
import gc
import board
import digitalio
import storage
gp0 = digitalio.DigitalInOut(board.GP0)
gp0.pull = digitalio.Pull.UP
if gp0.value == False:
    storage.remount("/", readonly = False)
gc.collect(150000)
storage.remount("/", False)
</python>
==mp3 player==
<python>
import board, digitalio
from audiomp3 import MP3Decoder
from audiopwmio import PWMAudioOut as AudioOut
audio = AudioOut(board.GP14)
path = "sounds/"
filename = "1.mp3"
mp3_file = open(path + filename, "rb")
decoder = MP3Decoder(mp3_file)
def play_mp3(filename):
    decoder.file = open(path + filename, "rb")
    audio.play(decoder)
    while audio.playing:
        pass
play_mp3("1.mp3")
</python>


==LED blink==
==LED blink==

於 2024年1月9日 (二) 09:30 的最新修訂

參考資料

學習方向

  • wifi連網
    • http
      • TTS
      • Barcode toQL
  • mp3player 用來撥放米數音檔【done】
    • GPIO14 + GND to SOCKET
  • Barcord scaner
    • bluetooth
    • usb
  • MQTT publisher
  • WS2812B LED燈條驅動
    • neopixel
  • 外接電源

line notify push

<python> import os import ssl import adafruit_requests import socketpool import wifi

token = "token" message = "How are you!"

wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context())


response = requests.post(

   "https://notify-api.line.me/api/notify",
   headers={"Authorization": f"Bearer {token}"},
   data={"message": message},

) </python>

online mp3 playe

<python> import adafruit_requests import wifi import socketpool import ssl import board import audiomp3 import os from audiopwmio import PWMAudioOut as AudioOut

audio = AudioOut(board.GP14)

mp3_url = 'http://192.168.1.100:8080/IOT/mp3/0.19.mp3'

  1. chunk size for downloading the mp3 file in parts

chunk_size = 1024

  1. connects to SSID

wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))

pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context())

  1. requests the url and writes the mp3 to a file

try:

   doc = requests.get(mp3_url)
   # opens the file in binary write mode
   with open('sound.mp3', 'wb') as f:
   # writes the file in chunks
       for chunk in doc.iter_content(chunk_size=chunk_size):
           f.write(chunk)

except Exception as e:

   print("Connection error:\n", str(e))
  1. plays the mp3 file from flash memory

try:

   # creates an mp3 object
   mp3 = audiomp3.MP3Decoder(open("sound.mp3", "rb"))
   # optimizes the sample rate for your setup before playing
   mp3.sample_rate=24000
   # mp3.bits_per_sample=50
   audio.play(mp3)
   while audio.playing:
       pass
   print("Done playing!")

except Exception as e:

   print("Error in creating mp3 or playing audio:\n", str(e))

</python> <python>

  1. pico檔案系統預設唯讀,需用boot.py去關掉
  2. 1.需短路GPIO0和GND
  3. 2.先VSYS送電再連USB到電腦
  4. boot.py內容如下

import gc import board import digitalio import storage

gp0 = digitalio.DigitalInOut(board.GP0) gp0.pull = digitalio.Pull.UP

if gp0.value == False:

   storage.remount("/", readonly = False)

gc.collect(150000) storage.remount("/", False)

</python>

mp3 player

<python> import board, digitalio from audiomp3 import MP3Decoder from audiopwmio import PWMAudioOut as AudioOut

audio = AudioOut(board.GP14) path = "sounds/"

filename = "1.mp3" mp3_file = open(path + filename, "rb") decoder = MP3Decoder(mp3_file)

def play_mp3(filename):

   decoder.file = open(path + filename, "rb")
   audio.play(decoder)
   while audio.playing:
       pass

play_mp3("1.mp3")

</python>

LED blink

<python> from machine import Pin import time

led = Pin("LED", Pin.OUT)

while True:

 led.on()   # LED亮
 time.sleep(1)  # 等待1秒
 led.off()
 time.sleep(1)  # LED滅

</python>

連上wifi

<python> import network import time

  1. 配置您的WiFi连接信息

ssid = 'QCRD' password = 'tskdenko'

  1. 初始化WiFi接口

wlan = network.WLAN(network.STA_IF)

  1. 激活接口

wlan.active(True)

  1. 连接到WiFi网络

if not wlan.isconnected():

   print('正在连接到网络...')
   wlan.connect(ssid, password)
   # 等待连接
   while not wlan.isconnected():
       pass
  1. 显示网络接口的状态

print('网络配置:', wlan.ifconfig()) </python>