@@ -0,0 +1,2298 @@
+#!/usr/bin/env python%0A#Communicate with end devices via LoRa.%0A#Communicate with server via MQTT(hbmqtt) and HTTP POST.%0A#Save data in the sqlite database.%0A#Parse JSON from MQTT and LoRa protocol.%0A%0A#Communication module: LoRa.%0A#Communication method with device via LoRa.%0A#Uart port drive LoRa module.%0A#Parse JSON between device and gateway via LoRa channel.%0A#LoRa module: E32-TTL-100%0A#Pin specification:%0A#M0    %3C--%3E GPIO(OUT)     #mode setting  connct to GND is OK!(Low)%0A#M1    %3C--%3E GPIO(OUT)     #mode setting  connct to GND is OK!(Low)%0A#RXD   %3C--%3E 8(TXD)        #ttyS0%0A#TXD   %3C--%3E 10(RXD)       #ttyS0%0A#AUX   %3C--%3E GPIO/INT(IN)  #module status detecting%0A#VCC%0A#GND%0A%0A#You need to install pyserial manually, install command is below:%0A#pip install pyserial%0A%0Aimport serial%0Aimport time%0Aimport json%0A#ser  = serial.Serial(%22/dev/ttyS0%22, 9600)%0Aser  = serial.Serial(%22/dev/ttyS0%22, 9600, timeout=0.2)%0Adef main():%0A    while True:%0A        #Waiting for LoRa module message from uart port.%0A        count = ser.inWaiting()%0A        if count != 0:%0A            recv = ser.readline() #readline() need to set timeout, otherwise results block%0A            ser.flushInput()%0A            print(recv)%0A            json_lora = json.loads(recv)%0A            #Parse JSON%0A            #print(json_lora.get(%22ID%22))%0A            #print(json_lora%5B%22ID%22%5D)%0A            #if json_lora.get(%22ID%22) == '1' : #Device ID-1 existed in gateway database%0A            if int(json_lora.get(%22ID%22)) == 1 : #Device ID-1 existed in gateway database%0A              if json_lora.get(%22CMD%22) == 'Online':%0A                response = '%7B%22ID%22:%221%22, %22CMD%22:%22Online%22, %22TYPE%22:%22Light2%22, %22VALUE%22:%22On%22%7D'%0A                print(response)%0A                ser.write(response)%0A              elif json_lora.get(%22CMD%22) == 'Env':%0A                if json_lora.get(%22TYPE%22) == 'moisture':%0A                  if int(json_lora.get(%22VALUE%22)) %3C 2000: # soil moisture is lower than standard%0A                    response = '%7B%22ID%22:%221%22, %22CMD%22:%22irrigate%22, %22TYPE%22:%22Open%22, %22VALUE%22:%22100%22%7D'%0A                    ser.write(response)%0A            else:%0A              print('init_device')%0A              #init_device()  #Create sqlite table for device 1.%0A        time.sleep(0.1)%0A%0Aif __name__ == '__main__':%0A    try:%0A        main()%0A    except KeyboardInterrupt:%0A        if ser != None:%0A            ser.close()%0A

Create main.py