ใช้บอร์ด BBC micro:bit กับ LoRaWAN
Maxiiot DL7612-AS923-TH
อุปกรณ์
- บอร์ด BBC micro:bit
- IObit V2.0 www.kittenbot.cc DOCK Station
- Maxiiot DL7612-AS923-TH
- I2C Sensor Temp & Humid หรืออาจใช้ Sensor บนบอร์ด
โปรแกรม
1.thornny
การเชื่อมต่อ
เสียบบอร์ด micro:bit เข้า dock station หงายด้านมี led ขึ้น
เสียบสาย micro usb เข้าที่พอร์ดบน microbit
ใช้สายเชื่อม Maxiiot Dl7612-AS923-TH กับ Dock Station
Maxiiot - ขาบน DOCK Station
TX - 1
RX - 0
3V3 - ขาสีแดง 3V
GND - ขาสีดำ GND
เสียบ Sensor I2C
SCL, SDA, 3V3, GND เข้ากับ Dock Station ตรงตามชื่อขา
การ Flash Firmware
เปิดโปรแกรม Thonny เข้าที่ Tools/Options เลือก MicroPython (BBC micro:bit) Port ให้เลือกให้ตรงกับที่ใช้กับ micro:bit
คลิก open the dialog for installing or upgrading MicroPython on your device
หลัง Upgrade แล้วเมื่อ Soft Reset จะเห็นข้อความ เช่น
MicroPython v1.9.2–34-gd64154c73 on 2017–09–01; micro:bit v1.0.1 with nRF51822
Type “help()” for more information.
*ขณะใช้งานด้วย thonny จะมี bug ค่อนข้างแยะ เมื่อ hang ให้กดปุ่ม Reset บน บอร์ด micro:bit และ stop ของ thonny หลายๆ ครั้ง
ตัวอย่างโปรแกรม
- scanI2C
import os
from microbit import *
sleep(200)# init ic2 object
i2c.init(freq=100000) #microbit
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))
ผลการ run
2. แสดงค่าอุณหภูมิเมื่อกดปุ่ม Switch A
import os
from microbit import *
while True:
if button_a.was_pressed():
display.scroll(temperature())
3. แสดงค่าอุณหภูมิหน้าจอ Shell
from microbit import *
while True:
print(temperature())
sleep(1000)
4. โปรแกรมเชื่อมต่อกับมอดูล LoRaWAN
บอร์ด micro:bit ใช้ ้hardware UART ร่วมกัน ทำให้ต้องสลับกันเรียกใช้งาน หลังเรียกใช้ UART กับ LoRaWAN แล้วให้ใช้คำสั่ง uart.init(115200) เพื่อคืนค่ากลับมาใช้ Serial Terminal
import os
from microbit import *
uart.init(baudrate=115200, tx = pin0, rx = pin1)
sleep(5)
uart.write ("AT\r\n")
sleep(10)
result1=uart.read()uart.write ("AT+INFO\r\n")
sleep(10)
result2=uart.read()uart.write ("AT+NCONFIG\r\n")
sleep(10)
result3=uart.read()uart.init(115200)print(str(result1, 'utf-8'))
print(str(result2, 'utf-8'))
print(str(result3, 'utf-8'))
ผลการ Run เช่น
5.ส่งข้อความ Hello เข้า LoRaWAN Network Server ไปเรื่อยๆ
import os,time
from microbit import *def sendATcommand(ATcommand):
print(“{0}\r\n”.format(ATcommand))
uart.init(baudrate=115200, tx = pin0, rx = pin1)
sleep(5)
uart.write(bytearray(b”{0}\r\n”.format(ATcommand)))
sleep(2) #Need to wait if decode error
rstr=uart.read()
if rstr:
rstr=str(rstr,’utf-8')
uart.init(115200)
print(rstr)
return (rstr)sendATcommand (“AT+NRB”)
time.sleep(20.0)
sendATcommand (“AT+INFO”)
sendATcommand (“AT+NCONFIG”)
sendATcommand (‘AT+CGATT’)
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)
sendATcommand (‘AT+CGATT’)