Raspberry Pi Pico กับ LoRaWAN
ใช้มอดูล LoRaWAN Maxiiot-DL7612-AS923-TH
บอร์ดนี้แตกต่างจากบอร์ด Arduino อื่นๆ ที่เมื่อเสียบสาย USB แล้วจะไม่มีไฟ LED สีแดงติดให้ทราบว่าได้ป้อนไฟแล้ว
มาลองใช้เป็น LoRaWAN Node ดูหลังจากได้ Raspberry Pi Pico มาสดๆร้อนๆ
เสียบสาย USB ขณะกด micro switch บน PCB ค้างไว้ ก็จะเห็น Drive H RPI-RP2 ขึ้นมา
ให้ Download Firmware MicroPython ได้จาก ลิ้งค์
คลิกที่ Download UF2 File ก็จะได้ไฟล์ เช่น pico_micropython_20210121.uf2
ให้ Copy ไฟล์ที่โหลดได้ไปวางใน Drive H:
บอร์ด Pico จะ Reboot อัตโนมัติหลัง Copy ไฟล์เสร็จ
เราจะใช้กับ Thonny IDE ครับ
สามารถ Downlaod มาติดตั้งได้จากเวป https://thonny.org/
ก่อนอืื่นให้ตั้งค่า Thonny ให้เห็นหน้าต่างชื่อ File ก่อน โดยคลิกที่ View/Files
ถัดไปให้เลือก tool/option และ Interpreter
เลือกช่องบนเป็น MicroPython(generic) ช่องล่างเลือก USB Serial Device (COM ให้ตรงกับที่เครื่องเราสร้างขึ้น) ของผู้เขียนเห็นเป็น COM20
หากพร้อมใช้งานจะเป็นดังรูป
หน้าต่าง Shell จะได้ Prompt >>> พร้อมรับคำสั่ง
มีข้อความก่อน Prompt ดังนี้
MicroPython v1.13–290-g556ae7914 on 2021–01–21; Raspberry Pi Pico with RP2040Type “help()” for more information.
เป็น MicroPython Version 1.13 ซึ่ง Make มาเมื่อวันที่ 21 มกราคม 2564 สำหรับ Chip RP2040
- ลองทดสอบโปรแกรมแรกโปรแกรม Blink.py
เมื่อ run จะเห็นไฟ LED สีเขียวอ่อนบน กระพริบ
from machine import Pin
from time import sleep
led = Pin(25, Pin.OUT)
while True:
led.value(1)
sleep(0.5)
led.value(0)
sleep(0.5)
2. SCAN ดูว่ามีอุปกรณ์ I2C เชื่อมต่ออยู่รึเปล่า
ให้เชื่อม sensor BME280 เข้ากับบอร์ดโดยเชื่อม SDA จาก Sensor เข้าขา 1 และ SCL จาก Sensor เข้าขา 2 ป้อนไฟ 3 V จากขา 36 และ GND เข้า Sensor ด้วย
import machine
i2c =
machine.I2C(0,scl=machine.Pin(1),sda=machine.Pin(0),freq=400000)
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 โปรแกรมจะเห็น Hex Address ที่ Sensor BME280 ใช้อยู้
Scan i2c bus…
i2c devices found: 1
Decimal address: 118 | Hexa address: 0x76
3. โปรแกรมส่งค่า Sensor BME280 ด้วย LoRaWAN
เราจะใช้ UART1 ขา 6 และขา 7 เชื่อม RX จากมอดูล Maxiiiot DL7612-AS923-TH เข้าขา 6 และ TX จากมอดูลเข้าขา 7 ป้อนไฟ 3 V จากขา 36 และ GND เข้ามอดูลด้วย
# By Somsak Lima @ 30JAN2021
# LOOP JOIN
import bme280, time, ubinascii, machine
from machine import UART, Pin, I2C
from cayennelpp import CayenneLPP
from micropython import const
led = Pin(25, Pin.OUT)
temp = 0
pa = 0
hum = 0
i2c = machine.I2C(0, scl=machine.Pin(1), sda=machine.Pin(0), freq=400000)
bme = bme280.BME280(i2c=i2c)
uart = UART(1, 115200)def sendATcommand(ATcommand):
print("Command: {0}\r\n".format(ATcommand))
print(ATcommand)
uart.write("{0}\r\n".format(ATcommand))
decoded_rtnd = ''
while(decoded_rtnd != "OK\r\n"):
rtnd = uart.readline()
decoded_rtnd = rtnd.decode('utf-8')
print(decoded_rtnd)
return(decoded_rtnd)### LOOP OTAA
print("!Wait 15 sec after reboot.")
uart.write("{0}\r\n".format('AT+NRB'))
rtnd = uart.readline()
decoded_rtnd = rtnd.decode('utf-8')
time.sleep(15.0)
tryno = 1while(decoded_rtnd != "+CGATT:1\r\n"):
uart.write("{0}\r\n".format('AT+CGATT'))
rtnd = uart.readline()
decoded_rtnd = rtnd.decode('utf-8')
print(decoded_rtnd)
if "+CGATT:1" in decoded_rtnd:
print("*******OTAA OK*******")
break
else:
print("Waiting for Module.......")
print("Module still try OTAA Join Request")
if tryno % 8 == 0 :
print("Last retry by module not success")
print("Time to try reboot")
uart.write("{0}\r\n".format('AT+NRB'))
tryno = 1
else:
print("Status Not Connect Yet")
print("This check is NO# ", str(tryno)[-1:])
print("Check more. NO# Check still less than 8 times ")
tryno = tryno+1time.sleep(10.0)
print("Join Success")
### END LOOP OTAA
cnt = 1
while True:
print("\r\n\r\nPacket No #{}".format(cnt))
print("**************")
print("BME280 values:")
print("**************")
temp, pa, hum = bme.values
print('temp:', temp, ' Hum:', hum , 'PA:', pa)
c = CayenneLPP()
c.addTemperature(2, float(temp))
c.addRelativeHumidity(3, float(hum))
c.addBarometricPressure(4, float(pa))
b = (ubinascii.hexlify(c.getBuffer()))
print('************ Sending rtnd Status **************')
led.value(1)
ATresp = sendATcommand("AT+NMGS={0},{1}".format(int(len(b)/2), (b.decode('utf-8'))))
print('********Finish Sending & Receiving rtnd Status******')
time.sleep(1.0)
led.value(0)
cnt = cnt + 1
time.sleep(10.0)
ทดสอบการ RUN
หากเชื่อมต่อได้สำเร็จก็จะเห็นข้อมูล payload ที่ Decode แล้วตามภาพดังนี้
หมายเหตุ
ค่าที่ได้จากคำสั่ง AT+NCONFIG ซึ่งเป็นค่าที่ Save ไว้ในมอดูล และถูกเรียกใช้งานตอน Reboot มอดูล
AT+NCONFIG
+NCONFIG:CLASS,C
+NCONFIG:ISMBAND,2
+NCONFIG:CHMASK,00ff,0000,0000,0000,0000,0000
+NCONFIG:RXWIN2,923200000,2
+NCONFIG:ACTIVATE,1
+NCONFIG:POWER,0
+NCONFIG:PORT,2
+NCONFIG:ADR,0
+NCONFIG:DR,0,12,0,0
+NCONFIG:CFM,1
+NCONFIG:NNMI Enable,1
+NCONFIG:PNMI Enable,1
OK