ส่ง พิกัด LoRaWAN AI RA-08H เข้า Chirpstack
3 min readJan 2, 2024
เพื่อให้การใช้งานง่ายขึ้น เราจะใช้มอดูล Ai Ra-08H กับ PCB Adapter ของ M2M ดังภาพ แผ่น PCB และชุดอุปกรณ์ประกอบไม่รวมมอดูล Ra-08H สามารถสั่งซื้อได้ที่ คลิก
เพื่อให้สามารถเชื่อมต่อกับขาที่จำเป็นต้องการใช้งานง่ายๆ
หลังจากบัดกรีมอดูลลงบนแผ่น PCB และอุปกรณ์อื่นๆ แล้ว ก็จะได้ดังภาพ มอดูลที่สั่งมาต้องตรงความถี่กับที่ใช้กับ LoRaWAN Gateway เช่น AS923, US915, EU868 หรือไม่ตรงอาจจะ Flash Formware ใหม่ได้
ภาษา MicroPython ที่ใช้
import struct
import time, ubinascii, machine
from machine import UART, Pin, SoftI2C
from micropython import const
from micropyGPS import MicropyGPS
stop = False
LED_GPIO = const(2) # define a constant
led = machine.Pin(LED_GPIO, mode=machine.Pin.OUT) # GPIO output
led = Pin(2, Pin.OUT)
relay1 = Pin(12, Pin.OUT)
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) # ESP32 Dev Board /myown
uart1 = machine.UART(1,baudrate=9600,rx=25,tx=12,timeout=10)
uart2 = UART(2, 9600, timeout=300)
my_gps = MicropyGPS(+7)
rstr = ""
def sendATcommand(ATcommand):
print("Command: {0}\r\n".format(ATcommand))
print(ATcommand)
uart2.write("{0}\r\n".format(ATcommand))
rstr = uart2.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)
arstr = ''
arstr = sendATcommand('AT+CSTATUS?')
while '+CSTATUS:04' not in arstr:
print('Retry OTAA Continue')
sendATcommand('AT+CJOIN=1,0,8,2')
time.sleep(20.0)
arstr = ''
arstr = sendATcommand('AT+CSTATUS?')
if '+CSTATUS:04' in arstr:
print('++++OTAA OK+++++')
break
if '+CSTATUS:04' in arstr:
print('++++OTAA OK+++++')
#Send Sample data
sendATcommand('AT+DTRX=0,2,6,445566')
relay1 = Pin(12, Pin.OUT)
button = Pin(4, Pin.IN)
button.value()
time.sleep(5.0)
cnt = 1
while True:
print("\r\n\r\nPacket No #{}".format(cnt))
my_sentence = uart1.readline()
print(my_sentence)
for x in str(my_sentence):
my_gps.update(x)
print("--------------------------- ")
print('Date:',my_gps.date_string('s_dmy'))
print('Time:',my_gps.timestamp)
print("Time: {}:{}:{}".format( my_gps.timestamp[0], my_gps.timestamp[1], my_gps.timestamp[2]))
print('Direction:',my_gps.course)
print('satellites_in_use',my_gps.satellites_in_use)
print('my_gps.altitude',my_gps.altitude)
lat1 = my_gps.latitude[0] + my_gps.latitude[1] / 60
lon1 = my_gps.longitude[0] + my_gps.longitude[1] / 60
print("LATITUDE %2.6f" % float(lat1))
print("LONGIITUDE %2.6f" % float(lon1))
print("ALTITUDE %f" % float(my_gps.altitude))
print('Speed (Km/hr):',my_gps.speed_string('kph'))
print('Speed (mph):',my_gps.speed_string('mph'))
print('Speed (mph):',my_gps.speed[1])
speed=my_gps.speed[1] # 0 Knot, 1 mph, 2 kph
print (speed)
print("--------------------------- ")
bat=3
speed = struct.pack('h', int(speed))
bat_pack = struct.pack('h', int(bat))
lat_pack = struct.pack('i', int(lat1 * 1000000))
lon_pack = struct.pack('i', int(lon1 * 1000000))
b = (bat_pack + speed + lat_pack + lon_pack)
b = ubinascii.hexlify(b)
print("************ Sending Data Status **************")
led.value(1)
ATresp = sendATcommand(
'AT+DTRX=0,1,{0},{1}'.format(int(len(b)),(b.decode('utf-8')))
)
print("********Finish Sending & Receiving Data Status******")
led.value(0)
cnt = cnt + 1
time.sleep(5.0)
Codec Javascript
function decodeUplink(input) {
var decoded = {};
decoded.bat = (input.bytes[1] << 8) | input.bytes[0];
decoded.speed = (input.bytes[3] << 8) | input.bytes[2];
decoded.latitude = (input.bytes[7]<<24 | input.bytes[6]<<16 | input.bytes[5]<<8 | input.bytes[4]) / 1000000;
decoded.longitude = (input.bytes[11]<<24 | input.bytes[10]<<16 | input.bytes[9]<<8 | input.bytes[8]) / 1000000;
decoded.altitude = 2;
decoded.accuracy = 2;
return data:decoded;
}