Elecrow Lora RA-08H Node Board กับ MicroPython

Somsak Lima
3 min readJul 21, 2023

เตรียม Firmware

  1. MicroPython สำหรับ RP4020
    ใช้ Thonny 4.1.1 Flash MicroPython ลงใน RP4020
    โดยกด Boot ค้างแล้วกด RST หนึ่งครั้งแล้วปล่อยทั้งสองปุ่ม จะเห็น Virtual Drive เข้าตรงส่วน Flash Firmware เช่น เห็นเป็น Drive D ตามรูป มี File 2 ไฟล์

คือ INFO_UF2 และ INDEX.HTM ใช้ Thonny Flash Firmware MicroPython โดยเลือกตัวอย่างตามภาพ

2. Firmware AS923 สำหรับ Ra-08H เชื่อมสายเข้ากับ Elecrow Node

ตัวอย่างการใช้ภาษา MiroPython สำหรับ Elecrow LoRa Node Board

  1. ฺBlink
from machine import Pin
import machine
from time import sleep
led = Pin(25, Pin.OUT)
while True:
led.value(1)
sleep(0.5)
led.value(0)
sleep(0.5)

2.ScanI2C

# Scanner i2c en MicroPython | MicroPython i2c scanner
from machine import Pin,I2C
import machine
i2c = machine.I2C(0)
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))

3.Send Uart AT Command

from machine import UART
import time
from time import sleep
uart = UART(0, 9600,timeout=1000)
rstr=""
def sendATcommand(ATcommand):
print("Command: {0}\r\n".format(ATcommand))
uart.write("{0}\r\n".format(ATcommand))
rstr=uart.read().decode('utf-8')
print(rstr)
return (rstr)

sendATcommand ("AT+CDEVEUI?")
sendATcommand ("AT+CAPPKEY?")
sendATcommand ("AT+CAPPEUI?")
sendATcommand ("AT+CJOIN=1,0,8,1")
time.sleep(20.0)
rstr=sendATcommand ('AT+CSTATUS?')
print('++++')
print('Res='+rstr)
print('++++')
while '+CSTATUS:04' not in rstr:
print('Retry OTAA Continue')
sendATcommand ('AT+CJOIN=1,0,8,2')
time.sleep(20.0)
rstr=sendATcommand ('AT+CSTATUS?')
print('################Respond String')
print(rstr)
if '+CSTATUS:04' in rstr:
print('++++OTAA OK+++++')
break


if '+CSTATUS:04' in rstr:
print('++++OTAA OK+++++')
sendATcommand ('AT+DTRX=0,2,36,016701120268950373006406010107032ee0')
sendATcommand ('AT+DTRX=0,2,6,445566')

4.Print BME280

from machine import Pin, I2C
import machine
import bme280
temp = 0
pa = 0
hum = 0
i2c = machine.I2C(0)
bme = bme280.BME280(i2c=i2c)
temp,pa,hum = bme.values
print(bme.values)
print(temp)
print(pa)
print(hum)

5. Send Temp&Humid LoRaWAN

# By Somsak Lima and Itti  Srisumalai
import machine
import ure
import bme280, time, ubinascii, machine
import utime as time
import _thread
from machine import UART, Pin, I2C
from struct import unpack
from cayennelpp import CayenneLPP
from time import sleep
from micropython import const
stop = False
motion=0
motion2=0
led = Pin(2, Pin.OUT)
relay1 = Pin(12, Pin.OUT)
temp = 0
pa = 0
hum = 0
i2c = machine.I2C(0) # Raspberry Pi Pico default assignment: scl=Pin(9), sda=Pin(8)
#i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))
bme = bme280.BME280(i2c=i2c)
uart = UART(0, 9600,timeout=300)
rstr=""
def sendATcommand(ATcommand):
print("Command: {0}\r\n".format(ATcommand))
uart.write("{0}\r\n".format(ATcommand))
rstr=uart.read().decode('utf-8')
print(rstr)
return (rstr)

mot = machine.Pin(15)
motion=(mot.value())
sendATcommand ('AT+CADR=0')
sendATcommand ('AT+CDATARATE=2')
sendATcommand ('AT+CULDLMODE=1')

sendATcommand ("AT+CDEVEUI?")
sendATcommand ("AT+CAPPKEY?")
sendATcommand ("AT+CAPPEUI?")
sendATcommand ("AT+CJOIN=1,0,8,1")
time.sleep(20.0)
rstr=sendATcommand ('AT+CSTATUS?')
print('++++')
print('Res='+rstr)
print('++++')
while '+CSTATUS:04' not in rstr:
print('Retry OTAA Continue')
sendATcommand ('AT+CJOIN=1,0,8,2')
time.sleep(20.0)
rstr=sendATcommand ('AT+CSTATUS?')
print('################Respond String')
print(rstr)
if '+CSTATUS:04' in rstr:
print('++++OTAA OK+++++')
break

if '+CSTATUS:04' in rstr:
print('++++OTAA OK+++++')
#Send Sample data
sendATcommand ('AT+DTRX=0,2,6,445566')
time.sleep(10.0)

cnt = 1
while True:
print( "\r\n\r\nPacket No #{}".format( cnt ) )
temp,pa,hum = bme.values
print("*********************")
print("BME280/BMP280 values:")
print("*********************")
temp,pa,hum = bme.values
print('temp:',temp,' Hum:',hum ,'PA:', pa)
c = CayenneLPP()
c.addTemperature(1, float(temp))
c.addBarometricPressure(2, float(pa))
c.addRelativeHumidity(3, float(hum))
c.addDigitalOutput(8, 1)
c.addAnalogOutput(9, 120.0)
b = (ubinascii.hexlify(c.getBuffer()))
led.value(1)
time.sleep(10.0)
print('************ Sending Data Status **************')
sendATcommand("AT+DTRX=0,2,{0},{1}".format(int(len(b)),(b.decode('utf-8'))))
print('********Finish Sending & Receiving Data Status******')
led.value(0)
cnt = cnt + 1
time.sleep(10.0)

6. ต่อ GPS WAVGAT GPS Module NEO-6M

ใช้ Port UART1

from machine import UART,Pin
from time import sleep
uart1 = UART(1, 9600 ,timeout=1000)
uart1 = UART(1, baudrate=9600, tx=Pin(8), rx=Pin(9))
print (uart1)
sleep(1)
while True:
data = uart1.read(32) # read up to 32 bytes
if data is not None:
data_string = ''.join([chr(b) for b in data])
print(data_string, end="")

ผลการ RUN ไม่จำเป็นต้องจับ GPS ได้ (ค่าที่อ่านได้ยังแสดงไม่ครบ ใช้ทดสอบการเชื่อมต่อเท่านั้น) การใช้งานบางครั้งยังไม่เสถียร อาจจะเกิดจาก V ที่แตกต่างกัน

--

--

Somsak Lima

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