วิธีติดตั้ง CircuitPython บน Pyb V1.1
อุปกรณ์ที่ใช้
- Pyboard PYBv1.1 STM32F405RG
2. สาย Jumper ตัวเมีย-เมีย
Firmware ที่ใช้ ไปที่หน้าเวป https://circuitpython.org/board/pyboard_v11/
คลิก Download .ฺBIN NOW จะได้ไฟล์ เช่น adafruit-circuitpython-pyboard_v11-en_US-5.3.0.bin
Software ที่ใช้ STM32CubeProgrammer
ดาวน์โหลดโปรแกรม STM32CubeProgrammer คลิก
หรือ Download
ได้ที่เวป https://www.st.com/en/development-tools/stm32cubeprog.html ซึ่งต้องลงทะเบียนด้วย email แล้วจะมีลิ้งค์ให้ดาวน์โหลดส่งมาทาง email
เริ่มต้น Flash Firmware
Jump สายให้ Boot เข้า DFU โหมดตอนบูท
ถ้าเป็น Board Clone ของจีน ไม่มี Pin แถวล่าง จะต้องเชื่อมขา B0 กับ 3V3 แล้วค่อยเสียบสาย USB
หลังเสียบสาย USB Led จะติดสามสี คือ สีแดง สีเขียว สีน้ำเงิน
หากเป็นรุ่นแบบภาพด้านบน 3V3 กับขา DFU P1
เปิด STM32CubeProgrammer
เลือก USB และ Port USB1 กด Connect
จะเห็นข้อมูลขึ้นบนหน้าจอ
คลิกที่ TAB Open File เลือกไฟล์ .bin ที่เรา Download ได้มา แล้วคลิก Open
หากไฟล์มีรูปแบบถูกต้อง จะเห็นข้อมูลของ File
แล้วให้คลิกที่มุมขวาบน Download
หน้าต่าง Log จะขึ้น Download in Progress:
หากเสร็จสิ้นจะขึ้น Download Complete ถอดสาย Micro USB ดึง Jumper DFU โหมดออก
การใช้งาน
เสียบสาย Micro USB Type หากทำงานถูกต้อง จะเห็น Virtual Drive ชื่อ CIRCUITPY ซึ่งทำงานคล้ายกับ Flash Drive ปรากฎขึ้น ของผู้เขียนจะขึ้่นเป็น Drive H:
เรียกโปรแกรม Thonny เข้าเมนู Tool /Option
หมายเหตุ จากการใช้งาน Thonny พบว่าไม่ค่อยจะ Compatible กับ CircuitPython บน PyB การแก้ไขทดสอบไฟล์จะทำได้บนเครื่อง PC เท่านั้น
เลือก CircuitPython (generic) และ Port ให้ถูกต้อง
จะเห็นว่ามีพื้นที่ให้เก็บโปรแกรมเพียง 28KB เท่านั้น ซึ่งก็น่าจะเพียงพอกับการใช้งานได้ระดับหนึ่ง
หลังจาก Soft Reboot ด้วย Key CTRL-D จะเห็นข้อความเกี่ยวกับ Firmware ดังนี้ Adafruit CircuitPython 5.3.0 on 2020–04–29; PyboardV1_1 with STM32F405RG
soft rebootAuto-reload is on. Simply save files over USB to run them or enter REPL to disable.Press Ctrl-C to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 5.3.0 on 2020-04-29; PyboardV1_1 with STM32F405RG
ทดสอบการใช้งาน
เพื่อให้สามารถใช้งาน Thonny ได้ง่ายขึ้นให้เปิด View หน้าต่าง Thonny เพิ่มขึ้น เช่น View Files, Shell เป็นต้น
>>> import os; print (os.uname())
(sysname='stm32f4', nodename='stm32f4', release='5.3.0', version='5.3.0 on 2020-04-29', machine='PyboardV1_1 with STM32F405RG')
>>>
พื้นที่ใช้งานจะมีประมาณ 31kB
ตัวอย่าง 1.0 scanI2C.py
ส่วน Sensor AHT10 ให้เชื่อม Vin เข้ากับ 3V3, ขา GND เข้ากับ GND, SCL เข้ากับ X9, SDA เข้ากับ X10 ซึ่งจะเป็น I2C1
import board
import busio
i2c = busio.I2C(board.SCL, board.SDA)
while not i2c.try_lock():
pass
print('Scan i2c bus…')
devices = i2c.scan()
if len(devices) == 0:
print("No i2c device !")
else:
print('i2c devices found:', len(devices))
for device in devices:
print("Decimal address: ", device, " | Hexa address: ", hex(device))
i2c.deinit()
ผลจากการ RUN
>>> %Run -c $EDITOR_CONTENT
Scan i2c bus…
i2c devices found: 1
Decimal address: 56 | Hexa address: 0x38
ตัวอย่าง 2.0 Sensor AHT10 แสดงค่าอุณหภูมิและความชื้นสัมพัทธ์
แสดง Library ที่ต้องใช้ โดย Copy จาก ไฟล์ที่บีบอัดไว้คือ adafruit-circuitpython-bundle-5.x-mpy-20200611.zip โดยสามารถ Download ไฟล์ล่าสุดได้จาก เวป Github ของ Adafruit https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases
import board
import adafruit_ahtx0
import busio
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_ahtx0.AHTx0(i2c)
print("\nTemperature: %0.1f C" % sensor.temperature)
print("Humidity: %0.1f %%" % sensor.relative_humidity)
i2c.deinit()
ผลจากการ RUN
>>> %Run -c $EDITOR_CONTENTTemperature: 27.6 C
Humidity: 51.7 %
ตัวอย่าง 3.0 LoRaWAN ส่ง Hello เข้า TheThingsNetwork
การเชื่อมกับมอดูล Maxiiot DL7612-AS923-TH ทำโดย เชื่อมสาย TX จากมอดูลเข้ากับขา X4 และ RX จากมอดูลเชื่อมเข้ากับขา X3 (หรือ Y10,Y9)
import time
uart = busio.UART(board.X3, board.X4, baudrate=115200, timeout=.1, receiver_buffer_size=512)
def sendATcommand(ATcommand):
print("{0}\r\n".format(ATcommand))
uart.write(bytearray(b"{0}\r\n".format(ATcommand)))
print(uart.read().decode('utf-8'))
# print(uart.read())
sendATcommand("AT+NRB")
time.sleep(5.0)
sendATcommand("AT+INFO")
sendATcommand("AT+NCONFIG")
time.sleep(10.0)
sendATcommand('AT+CGATT')
cnt = 1
while True:
print("Hello! #{}".format(cnt))
sendATcommand("AT+NCMGS=5,Hello")
# sendATcommand("AT+NMGS=7,01670119026873")
sendATcommand('AT+CGATT')
cnt = cnt + 1
time.sleep(5.0)