예제 #1
0
def unknown_length(TX_DATA):
    print("------------------------")

    print("send with unknown recive length")

    RESULT, RX_DATA = mcu.PROTOCOL_UART_TXRX_EX(0, TX_DATA, 250, 3000)
    print("RESULT:" + str(RESULT))
    print("RX_DATA:")
    print(RX_DATA)
    #print([hex(x).upper() for x in RX_DATA])
    #print("".join("0x%02x " % i for i in RX_DATA))

    time.sleep(3)
    print("------------------------")
예제 #2
0
def known_length(TX_DATA,RX_LENGTH):
    print("------------------------")

    print("send with known recive length")

    RESULT,RX_DATA = mcu.PROTOCOL_UART_TX_RX(0,TX_DATA,RX_LENGTH)

    print("RESULT:" + str(RESULT))
    print("RX_DATA:")
    #print([hex(x).upper() for x in RX_DATA])
    print("".join("0x%02x " % i for i in RX_DATA).upper())

    time.sleep(3)

    print("------------------------")
예제 #3
0
    print("open port & init mcu")
    #mcu.ser=serial.Serial("COM57",115200,timeout=1)
    mcu.ser = serial.Serial("/dev/ttyS0", 115200,
                            timeout=1)  #for PI (not ttyAMA0)(use /dev/ttyS0)
    time.sleep(5)
    print("mcu ok\n")
    print("------------------------")

    print("CHECK")
    print("========================")
    print("CHECK INTERNET")

    print("------------------------")
    print("CHECK TIME")
    #TODO
    current_mcu_time = mcu.GET_RTC_DATE_TIME()
    print(current_mcu_time)
    #get PI time
    #and sync time

    print("------------------------")
    print("CHECK PI VERSION")
    #TODO

    print("CHECK MCU VERSION")

    current_mcu_version = mcu.GET_INFO_VERSION()
    print(current_mcu_version)
    if (current_mcu_version < Conf.latest_mcu_version):
        #need update
        print("please update mcu")
예제 #4
0
    # else:
    #     print("-----no internet------")
    #     print("-----pass time check------")
    #     current_mcu_time = mcu.GET_RTC_DATE_TIME()
    #     print("MCU RTC time:")
    #     print(current_mcu_time)
    #     #set system time to mcu RTC clock time

    print("------------------------")
    print("CHECK PI VERSION")
    #TODO

    print("CHECK MCU VERSION")

    current_mcu_version = mcu.GET_INFO_VERSION()
    print(current_mcu_version)
    if (current_mcu_version < Conf.latest_mcu_version):
        #need update
        print("please update mcu")
    else:
        print("newest version")

    print("------------------------")
    print("SET SENSOR")

    mcu.SET_POLLING_SENSOR(Conf.POLL_TEMP, Conf.POLL_CO2, Conf.POLL_TVOC,
                           Conf.POLL_LIGHT, Conf.POLL_PMS, Conf.POLL_RTC)

    print("CHECK SENSOR")
    print(mcu.GET_INFO_SENSOR_POR())
예제 #5
0
def sim7600_sending_task():
    global initialize_flag
    global sim7600_fail_flag
    global stop_query_sensor
    global sim7600_moudule
    global gps_lat, gps_lon

    while (initialize_flag != 1):
        time.sleep(5)

    print("sim7600_sending_task start!!!")
    mcu.PROTOCOL_UART_BEGIN(0, 4)  #use port:0 / set to '4' as 115200 baud

    while (sim7600_moudule):
        try:
            time.sleep(
                Conf.sim7600_send_interval * 0.4
            )  #60 seconds/ but in seperate part / to shift away form upload
            stop_query_sensor = 1  #halt getting sensor data for a while

            #pairs = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S").split(" ")

            #msg = ""
            #message_package_part = []

            #if((gps_lat != "") and (gps_lon != "")):
            #    msg = msg + "|gps_lon="+ gps_lon + "|gps_lat=" + gps_lat
            #if(CO2 != 65535):
            #    msg = msg + "|s_g8=" + str(CO2)

            #msg = msg + "|s_t0=" + str(TEMP) + "|app=" + str(Conf.APP_ID) + "|date=" + pairs[0] + "|s_d0=" + str(PM25_AE) + "|s_h0=" + str(HUM) + "|device_id=" + Conf.DEVICE_ID + "|s_gg=" + str(TVOC) + "|ver_app=" + str(Conf.ver_app) + "|time=" + pairs[1] + "|s_s0=" + str(Leq_Median) + "|s_s0M=" + str(Leq_Max) + "|s_s0m=" + str(Leq_Min) + "|s_s0L=" + str(Leq)

            #print("------------------------")
            #print("msg_for_sim7600:",msg)
            print("=============================================")

            #clear input buffer
            #while(ser.inWaiting()!=0):
            #    data = mcu.ser.readline()
            mcu.ser.reset_input_buffer()

            at_cmd = "AT\r"
            check_cmd = mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250,
                                                  3000)

            if (check_cmd[1] == "empty"):
                print("NO moudle")
                sim7600_moudule = 0
                raise 'error'

            #print("----start GPS----")
            at_cmd = "AT+CGPS=1\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)

            #print("----get GPS data----")
            at_cmd = "AT+CGPSINFO\r"
            gps_info = mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)

            gps_info_str = ""

            for i in range(len(gps_info[1])):
                gps_info_str = gps_info_str + chr(gps_info[1][i])

            gps_info_str = gps_info_str.replace("\n", "").replace("\r",
                                                                  "").replace(
                                                                      "OK", "")
            #print(gps_info_str)

            gps_info_str = gps_info_str.split(":")[1]
            #print(gps_info_str)

            #put dummy here
            #gps_info_str = "3113.343286,N,12121.234064,E,250311,072809.3,44.1,0.0,0"

            gps_info_str = gps_info_str.strip(" ")
            gps_info_str = gps_info_str.split(",")

            #check if it is empty here
            if ((gps_info_str[0] != "") and (gps_info_str[2] != "")):

                gps_lat = gps_info_str[0]
                gps_lat_a = int(gps_lat[:2])
                gps_lat_b = float(gps_lat[2:])
                gps_lat = round(gps_lat_a + (gps_lat_b / 60), 4)
                gps_lat = str(gps_lat)

                gps_NS = gps_info_str[1]

                gps_lon = gps_info_str[2]
                gps_lon_a = int(gps_lon[:3])
                gps_lon_b = float(gps_lon[3:])
                gps_lon = round(gps_lon_a + (gps_lon_b / 60), 4)
                gps_lon = str(gps_lon)

                gps_EW = gps_info_str[3]
                gps_date = gps_info_str[4]
                gps_time = gps_info_str[5]
                gps_alt = gps_info_str[6]
                gps_speed = gps_info_str[7]
                gps_speed = round(float(gps_speed) * 1.852, 4)
            else:
                gps_lat = ""
                gps_lon = ""

            #print("-------!!---------")
            #print("gps_lat :" + gps_lat)
            #print("gps_lon :" + gps_lon)
            #print("-------!!---------")

            #print("----MQTT part----")
            at_cmd = "AT+CMQTTSTART\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)

            #print("----MQTT set client----")
            at_cmd = "AT+CMQTTACCQ=0,\"" + Conf.DEVICE_ID + "\"\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)

            #print("----MQTT set connection----")
            at_cmd = "AT+CMQTTCONNECT=0,\"tcp://35.162.236.171:8883\",20,1,\"maps\",\"iisnrl\"\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            time.sleep(3)

            #print("----MQTT set topic----")
            at_cmd = "AT+CMQTTTOPIC=0,23\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)

            at_cmd = "MAPS/MAPS6/" + Conf.DEVICE_ID + "\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)

            #set MQTT message
            pairs = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S").split(" ")

            msg = ""

            if ((gps_lat != "") and (gps_lon != "")):
                msg = msg + "|gps_lon=" + gps_lon + "|gps_lat=" + gps_lat
            if (CO2 != 65535):
                msg = msg + "|s_g8=" + str(CO2)

            msg = msg + "|s_t0=" + str(TEMP) + "|app=" + str(
                Conf.APP_ID) + "|date=" + pairs[0] + "|s_d0=" + str(
                    PM25_AE) + "|s_h0=" + str(
                        HUM) + "|device_id=" + Conf.DEVICE_ID + "|s_gg=" + str(
                            TVOC) + "|ver_app=" + str(
                                Conf.ver_app) + "|time=" + pairs[1]

            #if(Leq != 0):
            #    msg = msg + "|s_s0=" + str(Leq_Median) + "|s_s0M=" + str(Leq_Max) + "|s_s0m=" + str(Leq_Min) + "|s_s0L=" + str(Leq)
            if (temp_data != 0):
                msg = msg + "|add_temp=" + str(temp_data) + "|add_RH=" + str(
                    rh_data)

            msg = msg + "|MQ"

            payload_len = len(msg)

            #print("----MQTT set message----") #fix

            at_cmd = "AT+CMQTTPAYLOAD=0," + str(payload_len) + "\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)

            #add payload

            at_cmd = msg
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)

            #print("----publish MQTT message----")
            at_cmd = "AT+CMQTTPUB=0,1,60\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)

            #print("----Disconnect from the server----")
            at_cmd = "AT+CMQTTDISC=0,60\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            time.sleep(1)

            #print("----MQTT release client----")
            at_cmd = "AT+CMQTTREL=0"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            time.sleep(1)

            #print("----stop MQTT----")
            at_cmd = "AT+CMQTTSTOP\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)

            #should add clean buffer here
            #mcu.ser.readline()
            #mcu.ser.readline()
            #mcu.ser.readline()
            mcu.ser.reset_input_buffer()

            stop_query_sensor = 0  #resume getting sensor data
            sim7600_fail_flag = 0  #sim7600 is good
            time.sleep(Conf.sim7600_send_interval *
                       0.6)  #the rest of time interval

        except:
            print("=====sim7600 Fail====")
            traceback.print_exc()
            #should add clean buffer here
            #mcu.ser.readline()
            #mcu.ser.readline()
            #mcu.ser.readline()
            mcu.ser.reset_input_buffer()

            sim7600_fail_flag = 1
            stop_query_sensor = 0  #keep getting data
예제 #6
0
try:
    print("START")
    print("========================")

    print("open port & init mcu")
    #mcu.ser=serial.Serial("COM57",115200,timeout=1)
    mcu.ser=serial.Serial("/dev/ttyS0",115200,timeout=3) #for PI (not ttyAMA0)(use /dev/ttyS0)
    time.sleep(5)
    print("mcu ok\n")
    print("------------------------")

    print("check I/O and device busy")

    #mcu.GET_INFO_VERSION()
    print(mcu.GET_INFO_SENSOR_POR())

    print("open uart\n")

    mcu.PROTOCOL_UART_BEGIN(0,4)


    time.sleep(1)

    mcu.CLEAR_INPUT()

    time.sleep(1)


    #TX_DATA = AT +CR (0x41 0x54 0x0D)
    #TX_DATA = [0x41,0x54,0x0D]
예제 #7
0
try:
    print("START")
    print("========================")

    print("open port & init mcu")
    #mcu.ser=serial.Serial("COM57",115200,timeout=1)
    mcu.ser = serial.Serial("/dev/ttyS0", 115200,
                            timeout=3)  #for PI (not ttyAMA0)(use /dev/ttyS0)
    time.sleep(5)
    print("mcu ok\n")
    print("------------------------")

    print("check I/O and device busy")

    #mcu.GET_INFO_VERSION()
    print(mcu.GET_INFO_SENSOR_POR())

    print("open uart\n")

    mcu.PROTOCOL_UART_BEGIN(0, 4)

    time.sleep(1)

    mcu.CLEAR_INPUT()

    time.sleep(1)

    #TX_DATA = AT +CR (0x41 0x54 0x0D)
    #TX_DATA = [0x41,0x54,0x0D]
    TX_DATA = "AT\r".encode()
    #TX_DATA = bytearray()
예제 #8
0
def nbiot_sending_task():
    global initialize_flag
    global nbiot_fail_flag
    global stop_query_sensor
    global nbiot_moudule
    global gps_lat, gps_lon

    while (initialize_flag != 1):
        time.sleep(5)

    print("nbiot_sending_task start!!!")
    print(Conf.prifix)
    mcu.PROTOCOL_UART_BEGIN(0, 4)  #use port:0 / set to '4' as 115200 baud
    #mcu.PROTOCOL_UART_BEGIN(0,0)

    while (nbiot_moudule):
        try:
            time.sleep(
                Conf.nbiot_send_interval * 0.7
            )  #600 seconds/ but in seperate part / to shift away form upload
            stop_query_sensor = 1  #halt getting sensor data for a while

            #check if the nbiot module is on board
            at_cmd = "AT\r"
            check_cmd = mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250,
                                                  3000)

            #print(check_cmd)
            #print(check_cmd[1])
            #print(type(check_cmd[1]))
            if (check_cmd[1] == "empty"):
                print("NO moudle")
                nbiot_moudule = 0
                raise 'error'

            #get GPS info
            at_cmd = "AT+CGNSPWR=1\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)

            at_cmd = "AT+CGNSINF\r"
            gps_info = mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)

            #print("@@This is GPS 1 @@")
            #print(gps_info)

            gps_info_str = ""

            for i in range(len(gps_info[1])):
                gps_info_str = gps_info_str + chr(gps_info[1][i])

            gps_info_str = gps_info_str.replace("\n", "").replace("\r",
                                                                  "").replace(
                                                                      "OK", "")
            gps_info_str = gps_info_str.split(":")[1]
            gps_info_str = gps_info_str.strip(" ")
            gps_info_str = gps_info_str.split(",")

            print("@@This is GPS 2 @@")
            print(gps_info_str)

            #check if it is empty here
            if (gps_info_str[1] == "1"):

                gps_lat = gps_info_str[3]
                #gps_lat_a = int(gps_lat[:2])
                #gps_lat_b = float(gps_lat[2:])
                #gps_lat   = round(gps_lat_a + (gps_lat_b/60),4)
                #gps_lat   = str(gps_lat)

                #gps_NS    = gps_info_str[1]

                gps_lon = gps_info_str[4]
                #gps_lon_a = int(gps_lon[:3])
                #gps_lon_b = float(gps_lon[3:])
                #gps_lon   = round(gps_lon_a + (gps_lon_b/60),4)
                #gps_lon   = str(gps_lon)

                #gps_EW    = gps_info_str[3]
                #gps_date  = gps_info_str[4]
                #gps_time  = gps_info_str[5]
                #gps_alt   = gps_info_str[6]
                gps_speed = gps_info_str[6]
                #gps_speed = round(float(gps_speed)*1.852,4)
            else:
                print("no GPS")
                gps_lat = ""
                gps_lon = ""

#

            pairs = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S").split(" ")

            msg = ""
            message_package_part = []

            if ((gps_lat != "") and (gps_lon != "")):
                msg = msg + "|gps_lon=" + gps_lon + "|gps_lat=" + gps_lat
            if (CO2 != 65535):
                msg = msg + "|s_g8=" + str(CO2)

            msg = msg + "|s_t0=" + str(TEMP) + "|app=" + str(
                Conf.APP_ID) + "|date=" + pairs[0] + "|s_d0=" + str(
                    PM25_AE) + "|s_h0=" + str(
                        HUM) + "|device_id=" + Conf.DEVICE_ID + "|s_gg=" + str(
                            TVOC) + "|ver_app=" + str(
                                Conf.ver_app) + "|time=" + pairs[1]

            if ((Leq != 0) and (Leq != float("inf"))):
                msg = msg + "|s_s0=" + str(Leq_Median) + "|s_s0M=" + str(
                    Leq_Max) + "|s_s0m=" + str(Leq_Min) + "|s_s0L=" + str(Leq)

            msg = msg + "|MQ"

            print("------------------------")
            print("msg_for_nbiot:", msg)
            msg = Conf.prifix + msg
            payload_len = len(
                msg)  #remember to add tpoic length (2 byte in this case)
            payload_len = payload_len + 2

            #MQTT Remaining Length calculate
            #currently support range 0~16383(1~2 byte)
            if (payload_len < 128):
                payload_len_hex = hex(payload_len).split('x')[-1]
            else:
                a = payload_len % 128
                b = payload_len // 128
                a = hex(a + 128).split('x')[-1]
                b = hex(b).split('x')[-1]
                b = b.zfill(2)
                payload_len_hex = str(a) + " " + str(b)

            msg_hex = formatStrToInt(msg)

            # add_on = PUB_CMD / Remaim Length / topic length (MAPS/MAPS6/xxxxxxxxxxxx > 0x17)
            add_on = "30 " + str(payload_len_hex.upper()) + " 00 17 "
            end_line = "1A"
            message_package = add_on + msg_hex + end_line
            message_package = message_package.upper()

            print("=============================================")
            print("connect_pack:")
            print(Conf.connect_pack.upper())
            print("----------------")
            print("message_package:")
            print(message_package)
            #seperate message to little part / because buffer issue
            n = 141  #just because a line is about 141 char long
            for i in range(0, len(message_package), n):
                message_package_part.append(message_package[i:i + n])
            #print("this is part:")
            #print(message_package_part)
            print("=============================================")

            #should add clean buffer here

            #at_cmd = "AT\r"
            #check_cmd =  mcu.PROTOCOL_UART_TXRX_EX(0,at_cmd.encode(),250,3000)

            #print(check_cmd)
            #print(check_cmd[1])
            #print(type(check_cmd[1]))
            #if(check_cmd[1] == "empty"):
            #    print("NO moudle")
            #    nbiot_moudule = 0
            #    raise 'error'
            #

            #time.sleep(1)
            #print("----NBIOT init----")

            at_cmd = "AT+CIPSHUT\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            time.sleep(1)
            #print("-------CIPSHUT---------")

            at_cmd = "AT+CSQ\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            #time.sleep(1)
            #print("----check CSQ-----")

            at_cmd = "AT+CNMP=38\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            #time.sleep(1)
            #print("----LTE only-----")

            at_cmd = "AT+CMNB=2\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            #time.sleep(1)
            #print("----use NB-Iot-----")

            at_cmd = "AT+CIPSENDHEX=1\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            #time.sleep(1)
            #print("-------HEX---------")

            at_cmd = "AT+CGREG?\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            #time.sleep(1)
            #print("-------CGREG---------")

            at_cmd = "AT+CGATT?\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            #time.sleep(1)
            #print("-------CGATT---------")

            at_cmd = "AT+CGNAPN\r"
            apn_name = mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            time.sleep(1)
            print("----Get APN-----")

            str_apn = ""
            for i in range(len(apn_name[1])):
                str_apn = str_apn + chr(apn_name[1][i])
            #print(str_apn)
            #print("-------!!---------")
            str_apn = str_apn.replace("\n", "").replace("\r",
                                                        "").replace("OK", "")
            #print(str_apn)
            str_apn = str_apn.split(",")[1]
            #print(str_apn)

            at_cmd = "AT+CSTT=" + str_apn + "\r"
            #print("!!!!!!!!!!!!!!!!!!!!!!!!")
            print("this is CSTT cmd(with APN): " + at_cmd)
            #print("!!!!!!!!!!!!!!!!!!!!!!!!")

            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            time.sleep(2)
            #print("-------CSTT---------")

            #at_cmd = "AT+CIPSTATUS\r"
            #mcu.PROTOCOL_UART_TXRX_EX(0,at_cmd.encode(),250,3000)
            #time.sleep(1)
            #print("-------AT+CIPSTATUS---------")

            at_cmd = "AT+CIICR\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            #time.sleep(1)
            #print("------CIICR----------")

            at_cmd = "AT+CIFSR\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            #time.sleep(1)
            #print("----Get IP-----")

            at_cmd = "AT+CIPSTART=\"TCP\",\"35.162.236.171\",\"8883\"\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            time.sleep(2)
            print("----Start connection----")

            at_cmd = "AT+CIPSEND\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            time.sleep(2)
            #print("-------SEND---------")

            #connect pack
            at_cmd = Conf.connect_pack.upper()
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            time.sleep(2)
            print("----Conncet pack----")

            #send_pack_in_loop
            for i in range(len(message_package_part)):
                at_cmd = message_package_part[i]
                mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
                time.sleep(2)

            print("----Send OK----")

            at_cmd = "AT+CIPCLOSE\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            time.sleep(1)
            #print("-------close---------")

            at_cmd = "AT+CIPSHUT\r"
            mcu.PROTOCOL_UART_TXRX_EX(0, at_cmd.encode(), 250, 3000)
            time.sleep(1)
            print("----Chip shut----")

            #should add clean buffer here
            mcu.ser.readline()
            mcu.ser.readline()
            mcu.ser.readline()

            stop_query_sensor = 0  #resume getting sensor data
            nbiot_fail_flag = 0  #nbiot is good
            time.sleep(Conf.nbiot_send_interval *
                       0.3)  #the rest of time interval

        except:
            print("=====NBIOT Fail====")

            #should add clean buffer here
            mcu.ser.readline()
            mcu.ser.readline()
            mcu.ser.readline()

            nbiot_fail_flag = 1
            stop_query_sensor = 0  #keep getting data