Raspberry Pi Pico กับ LoRaWAN
ใช้มอดูล LoRaWAN Maxiiot-DL7612-AS923-TH
บอร์ดนี้แตกต่างจากบอร์ด Arduino อื่นๆ ที่เมื่อเสียบสาย USB แล้วจะไม่มีไฟ LED สีแดงติดให้ทราบว่าได้ป้อนไฟแล้ว
มาลองใช้เป็น LoRaWAN Node ดูหลังจากได้ Raspberry Pi Pico มาสดๆร้อนๆ
เราจะใช้กับ Thonny IDE ในการ Flash MicroPython ลง Pi Picoครับ
สามารถ Downlaod Thonny มาติดตั้งได้จากเวป https://thonny.org/
เรียก Tool/ Option/ Interpreter
คลิก Install or Update MicroPython ด้านล่างขวาจอ
กดปุ่ม MicroSwitch BootSel แล้วเสียบสาย USB เข้า PC
Taget volume จะขึ้นเป็น RPI-RP2 (E:) Family RP2 MicroPython Family ขึ้นเป็น RP2 ไม่ต้องเปลี่ยน
เลือก Variant Raspbery Pi Pico แล้วเลือก version เป็นเลขล่าสุด แล้วคลิก Install
เมื่อขึ้น Done! ให้ กด Close
ก่อนอืื่นให้ตั้งค่า Thonny ให้เห็นหน้าต่างชื่อ File ก่อน โดยคลิกที่ View/Files
ถัดไปให้เลือก tool/option และ Interpreter
เลือกช่องบนเป็น MicroPython(Raspberry Pi Pico) ช่องล่างเลือก USB Serial Device (COM ให้ตรงกับที่เครื่องเราสร้างขึ้น) ของผู้เขียนเห็นเป็น Board CDC@COM4
หากพร้อมใช้งานจะเป็นดังรูป
หน้าต่าง Shell จะได้ Prompt >>> พร้อมรับคำสั่ง
มีข้อความก่อน Prompt ดังนี้
MicroPython v1.23.0 on 2024-06-02; 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.โปรแกรมสั่ง มอดูล LoRaWAN DL7612
from rp2 import PIO
import machine
import time
from time import sleep
from machine import Pin
uart = machine.UART(1, 115200)
def sendATcommand(ATcommand):
decoded_data = ""
print("{0}\r\n".format(ATcommand))
output=bytearray(ATcommand + "\r\n")
uart.write(output)
while(decoded_data != "OK\r\n"):
data = uart.readline()
decoded_data = data.decode('utf-8')
print(decoded_data)
sendATcommand ('AT+NCONFIG')
sendATcommand ('AT+INFO')
sendATcommand ("AT+DEVEUI")
sendATcommand ("AT+APPKEY")
sendATcommand ("AT+APPEUI")
4.โปรแกรมแสดงค่า Sensor BME280
import machine
import bme280
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)
temp,pa,hum = bme.values
print(bme.values)
print(temp)
print(pa)
print(hum)
5. โปรแกรมส่งค่า 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'))
decoded_rtnd=''
while(decoded_rtnd != "OK\r\n"):
rtnd = uart.readline()
decoded_rtnd = rtnd.decode('utf-8')
print("Return Data after first boot=",decoded_rtnd)
rtnd = uart.readline()
decoded_rtnd = rtnd.decode('utf-8')
print("Return Data after reboot=",decoded_rtnd)
time.sleep(15.0)
tryno = 1
while(decoded_rtnd != "+CGATT:1\r\n"):
uart.write("{0}\r\n".format('AT+CGATT'))
rtnd = uart.readline()
decoded_rtnd = rtnd.decode('utf-8')
print("Return Data after check CGATT =",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 % 10 == 0 :
print("Last retry by module not success")
print("Time to try reboot")
uart.write("{0}\r\n".format('AT+NRB'))
decoded_rtnd=''
while(decoded_rtnd != "OK\r\n"):
rtnd = uart.readline()
decoded_rtnd = rtnd.decode('utf-8')
print("Return Data after reboot=",decoded_rtnd)
tryno = 1
else:
print("Status Not Connect Yet")
print("This check is NO# ", str(tryno)[-1:])
print("Check more. NO# Check still less than 10 times ")
tryno = tryno+1
time.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