วิธี Upgrade Firmware MicroPython Sipeed Maixduino (kendryte-k210
)

Somsak Lima
5 min readJun 17, 2020

--

Maixduino เป็นบอร์ดที่ใช้ภาษา MicroPython ที่ชื่อ MaixPy ดู Spec ทั่วไปได้จาก คลิก

ก่อนอื่นเพื่อให้สามารถใช้ Board Maixduino ให้ได้เต็มประสิทธิภาพ เลยจะมา Upgrade Firmware กันก่อน

Download Firmware
1. คลิก หรือ คลิก ตัวที่ดาวน์โหลดมาคือ maixpy_v0.5.0_95_g05c4e70.bin
2.ดาวน์โหลด Face Detection model V.0.3.2 (face_kmodel) คลิก
ตำแหน่งของการ Flash Face Detection model คือ 0x300000

ดาวน์โหลดโปรแกรมใช้ Flash
ชื่อ kflash เช่น kflash_gui_v1.5.5_2.7z

เรียกใช้โปรแกรม

เลือกเปิดไฟล์ที่ดาวน์โหลดมาสองไฟล์ เลือก COM Port ตัวแรกที่เกิดขึ้นใหม่ (จะเจอ 2 com บอร์ดนี้มี chip ที่สร้าง Virtual Com ขึ้นมาได้ 2 com) แล้วคลิก Download

รอจนขึ้น Download success ให้ปิดหน้าต่าง Kflash

MaixPy IDE

ดาวน์โหลดได้จาก คลิก

คลิก Tab Serial Terminal ด้านล่างเพื่อดูผลการทำงาน

Download Demo Script

มี Demo Script สามารถ Download ได้จาก คลิก

คลิก Clone หรือ Download จะได้ไฟล์ MaixPy_scripts-master.zip นำไปแตกไว้ที่ Drive C: /users/XXX/Documents/MaixPy

*XXX คือชื่อ user name บน PC

เรียกใช้ Thonny

ไปที่ เมนู Tools /Options

เลือก MicroPython (Generic)

ได้ MicroPython รุ่นใหม่ คือ v0.5.0–95-g05c4e70 เพิ่งออกมาวันที่ 2020–06–05

  • การจัดการไฟล์โปรแกรมบน Flash Mem ของ Maixduino และการย้ายไฟล์ไปมาระหว่าง Flash กับ Hard Disk บน PC ยังมีปัญหา bug หรือ error

การใช้งาน

  1. scanI2C.py

ใช้ขา SCL, SDA บน Sipeed Maxiduino

from machine import I2Ci2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=31)
devices = i2c.scan()
print(devices)

2. แสดงข้อมูลระบบ

import sys
print(“implementation:”, sys.implementation)
print(“platform:”, sys.platform)
print(“path:”, sys.path)
print(“Python version:”, sys.version)

3.แสดงภาพจากกล้องออกหน้าจอ LCD

import sensor
import image
import lcd
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
while True:
img=sensor.snapshot()
lcd.display(img)

4. โปรแกรม demo_find_face.py ใช้ตรวจจับหน้าคน

เปิดจาก machine_vision แล้วทดสอบรันดูครับ สามารถตรวจจับหน้าคนจากภาพที่เห็นจากกล้องได้ ตัวโปรแกรม มีดังนี้

#refer to http://blog.sipeed.com/p/675.html
import sensor
import image
import lcd
import KPU as kpu
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
task = kpu.load(0x300000) # you need put model(face.kfpkg) in flash at address 0x300000
# task = kpu.load(“/sd/face.kmodel”)
anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)
a = kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)
while(True):
img = sensor.snapshot()
code = kpu.run_yolo2(task, img)
if code:
for i in code:
print(i)
a = img.draw_rectangle(i.rect())
a = lcd.display(img)
a = kpu.deinit(task)

5. ส่งข้อมูล แบบ LoRaWAN ด้วยมอดูล Maxiiot DL7612-AS923-TH
สั่งซื้อมอดูลได้จากเวป คลิก

Maxiiot TX เข้าขา 6 ของ Maixduino
Maxiiot RX เข้าขา 7 ของ Maixduino

import board
import time
from fpioa_manager import fm
from machine import UART
from board import board_info
fm.register(15, fm.fpioa.UART2_TX, force=True) #Pin 7
fm.register(32, fm.fpioa.UART2_RX, force=True) #Pin 6
uart = UART (UART.UART2, 115200)def sendATcommand(ATcommand):
print(“{0}\r\n”.format(ATcommand))
uart.write(bytearray(b”{0}\r\n”.format(ATcommand)))
print(uart.read().decode(‘utf-8’))

sendATcommand (“AT+INFO”)
sendATcommand (“AT+NCONFIG”)
cnt = 1
while True:
print( “Hello! #{}”.format( cnt ) )
#sendATcommand (“AT+NMGS=7,01670119026873”)
sendATcommand (“AT+NCMGS=5,HELLO\r\n”)
cnt = cnt + 1
time.sleep(5.0)

อาจลองเปลี่ยนเป็นส่ง Cayenne Format โดย ใช้บรรทัด

sendATcommand (“AT+NMGS=7,01670119026873”)

แทน ซึ่งเป็นตัวอย่าง Payload ข้อมูล Temp=28.1, Humid=57.5

รันโปรแกรมด้วย MaixPy IDE

เมื่อดูข้อมูลบน ChirpStack LoRaWAN Network Server จะเห็นข้อมูลดังนี้

5. ส่งข้อมูลจาก Sensor BME280 ด้วย LoRaWAN

ตัวอย่างโปรแกรมภาษา MicroPython ของ Maixduino เขียนได้ดังนี้

import board, ubinascii
import bme280
import board, time
from fpioa_manager import fm
from machine import UART,I2C
from board import board_info
from utime import sleep
from cayennelpp import CayenneLPP
temp = 0 ; hum = 0 ; pa=0; rstr=””fm.register(15, fm.fpioa.UART2_TX, force=True) #Pin 7
fm.register(32, fm.fpioa.UART2_RX, force=True) #Pin 6
uart = UART (UART.UART2, 115200)
i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=31)
bme280 = bme280.BME280(i2c=i2c)
def sendATcommand(ATcommand):
print(“{0}\r\n”.format(ATcommand))
uart.write(bytearray(b”{0}\r\n”.format(ATcommand)))
time.sleep(2) #Need to wait if decode error
rstr=uart.read()
if rstr:
rstr= rstr.decode(‘utf-8’)
print(rstr)
return (rstr)
sendATcommand (“AT+NRB”)
sendATcommand (“AT+NCONFIG”)
cnt = 1
while rstr !=’+CGATT:1':
rstr=sendATcommand (‘AT+CGATT’)
time.sleep(20.0) #Wait for Join Accept
print(‘Respond String’)
print(rstr)
if rstr.startswith(‘+CGATT:1’):
print(‘++++OTAA OK+++++’)
break
print(‘Retry OTAA Continue’)
while True:
print(“********Packet No #{}”.format( cnt ) )
print(“********BME280 values:***********”)
temp,pa,hum = bme280.values
print(‘temp:’,temp,’ Hum:’,hum )
c = CayenneLPP()
c.addTemperature(1, float(temp))
c.addRelativeHumidity(2, float(hum))
d = ubinascii.hexlify(c.getBuffer())
print(‘ — — — — -Start Send Status — — — — — — ‘)
sendATcommand(“AT+NMGS={0},{1}\r\n”.format(int(len(d)/2),(d.decode(‘utf-8’))))
print(‘ — — — — -End Send Status — — — — — — — ‘)
cnt = cnt + 1
time.sleep(10.0)

วิธี Flash Firmware พร้อม Model

เช็ค mem ที่เหลือใช้คำสั่ง kpu.memtest()

ปัญหาที่พบ

บริษัทผู้ผลิตไม้ได้ให้ข้อมูลในการลง Driver สำหรับ USB มา และ USB Chip ก็จะค่อนข้างแปลกแตกต่างจาก Dev Board อื่นๆ หากพบปัญหามองไม่เห็น Com Port ให้ลองดูว่าไฟเลี้ยงบอร์ดเพียงพอหรือเปล่าด้วย นอกจากคิดว่าเป็นปัญหาที่ Driver USB หากมีปัญหามีคำแนะนำดังนี้

  1. ลองดูคำแนะนำลอง Driver USB จากบทความ คลิก
  2. ทดสอบเปลี่ยนสาย USB Type C ที่ให้กระแสไฟเพียงพอสำหรับ บอร์ด
  3. เลือกใช้ USB Port ที่มีไฟเลี้ยง เช่น ติดตั้ง Add on USB PC Card เพิ่มใน Slot ด้านท้ายเครื่อง PC และเลือก USB PC Card รุ่นที่ขั้วสีขาว สามารถเสียบเพิ่มไฟเลี้ยงจาก Power Supply ได้ดังภาพ

อนาคต

ที่คิดจะทำคือ Edge Computing โดยทดสอบการใช้ AI และเอาผลจาก AI ส่งด้วย LoRa เข้า LoRaWAN Network Server ครับ

References

  1. https://www.cresco.co.jp/blog/entry/10093/?fbclid=IwAR2fw3Gdz6ll4eTC3-vp83o4ZVhO-KfwEZgT4707QtJmfifAluoTYsgio0s
  2. https://medium.com/chiang-mai-maker-club/%E0%B8%97%E0%B8%B3-face-detection-%E0%B8%87%E0%B9%88%E0%B8%B2%E0%B8%A2-%E0%B9%86-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-sipeed-maix-board-a135f5a57af9
  3. https://www.aiiotshop.com/article/15/corgidude-%E0%B8%9A%E0%B8%AD%E0%B8%A3%E0%B9%8C%E0%B8%94-image-clssification-%E0%B9%82%E0%B8%94%E0%B8%A2%E0%B9%83%E0%B8%8A%E0%B9%89-mobilenetv1
  4. https://qiita.com/nnn112358/items/ef3435d113c03d44d574#m5stickv%E3%81%AE%E3%83%A1%E3%83%A2%E3%83%AA%E6%9C%80%E9%81%A9%E5%8C%96
  5. https://github.com/sipeed/MaixPy_scripts/tree/master/machine_vision
  6. https://towardsdatascience.com/transfer-learning-using-mobilenet-and-keras-c75daf7ff299
  7. https://www.bualabs.com/archives/3439/tensorflow-js-tutorial-build-image-classification-javascript-mobilenet-pretrained-model-tfjs-ep-7/
  8. https://blog.sipeed.com/p/680.html

--

--

Somsak Lima
Somsak Lima

Written by Somsak Lima

สนับสนุนและส่งเสริมให้ผู้สนใจสามารถใช้งานเทคโนโลยี LoRa และ LoRaWAN ได้ โดยนำความรู้ที่ได้ไปต่อยอดเพื่อใช้งาน

No responses yet