Esempio n. 1
0
def lora_unpacking(packet_data):
    packet_data.pos = 56
    crc = packet_data.read(8)
    packet_data.pos = 0
    if crc == crc_func(packet_data.read(56)):
        packet_data.pos = 0
    else:
        pass
Esempio n. 2
0
def on_message(mqttc, obj, msg):
    curtime = datetime.datetime.now()
    strcurtime = curtime.strftime("%Y-%m-%d %H:%M:%S")
    logger.info(strcurtime + ": " + msg.topic + " " + str(msg.qos) + " " +
                str(msg.payload))

    payload_length = len(msg.payload)
    un_int = struct.unpack(str(payload_length) + 'B', msg.payload)

    logger.info('*********---NEW MESSAGE----*********')
    print(5 * '\n')
    print(2 * '*********---NEW MESSAGE----*********')
    logger.info(payload_length)
    print('-------payload_length------')
    print(payload_length)
    logger.info(un_int)
    print(un_int)
    uints = list(un_int)
    print(uints)

    if uints[payload_length - 1] == crc_func(uints[:payload_length - 1]):
        logger.info('CRC checked!')

        if payload_length == 5:
            lora_unpacking_ack(uints)
        elif payload_length == 8:
            print('----msg.payload------')
            print(msg.payload)
            print(type(msg.payload))
            print('--------receive-time-------')
            print(datetime.datetime.now())

            # packet_data = BitStream('0x4001004751E47533')
            # '{:0>2x}'.format(1) #dic to hex,append 0
            packet_data = BitStream(msg.payload)
            logger.info('--------packet_data--------')
            logger.info(packet_data)
            logger.info(len(packet_data))
            print('-----packet-data------')
            print(packet_data)
            logger.info('--------packet_data.bin--------')
            logger.info(packet_data.bin)
            print(packet_data.bin)
            logger.info(len(packet_data.bin))

            realtime_data = lora_unpacking_realtime_data(packet_data)

            save_realtime_data(realtime_data)
        else:
            logger.debug('bytes unknown!')

    else:
        logger.error('CRC check fail!')
Esempio n. 3
0
def on_message(mqttc, obj, msg):
    curtime = datetime.datetime.now()
    strcurtime = curtime.strftime("%Y-%m-%d %H:%M:%S")
    logger.info(strcurtime + ": " + msg.topic + " " + str(msg.qos) + " " +
                str(msg.payload))

    payload_length = len(msg.payload)
    un_int = struct.unpack(str(payload_length) + 'B', msg.payload)

    logger.info('-------units-----')
    logger.info(un_int)

    uints = list(un_int)

    if uints[payload_length - 1] == crc_func(uints[:payload_length - 1]):
        logger.info('CRC checked!')

        if payload_length == 5:
            lora_unpacking_ack(uints)
        elif payload_length == 8:
            b = binascii.b2a_hex(msg.payload)
            # packet_data = BitStream('0x4001004751E47533')
            # '{:0>2x}'.format(1) #dic to hex,append 0
            packet_data = BitStream('0x' + b)

            logger.info('--------packet_data--------')
            logger.info(packet_data)
            logger.info('--------packet_data.bin--------')
            logger.info(packet_data.bin)

            realtime_data = lora_unpacking_realtime_data(packet_data)

            # save_realtime_data(realtime_data)
        elif payload_length == 16:
            b = binascii.b2a_hex(msg.payload)
            # packet_data = BitStream('0x4001004751E47533')
            # '{:0>2x}'.format(1) #dic to hex,append 0
            packet_data = BitStream('0x' + b)

            logger.info('--------packet_data--------')
            logger.info(packet_data)
            logger.info('--------packet_data.bin--------')
            logger.info(packet_data.bin)
            realtime_data_6 = lora_unpacking_realtime_6ch(packet_data)
            save_realtime_data_6ch(realtime_data_6)

        else:
            logger.info('bytes unknown!')

    else:
        logger.error('CRC check fail!')
def return_crc(str_bin):
    print('----str_bin------')
    print(str_bin)
    print('----len_str_bin------')
    print(len(str_bin))

    units = []
    for i in range(int(len(str_bin) / 8)):
        units.append(str_bin.read(8).uint)
    print('units', units)

    crc = crc_func(units)
    print('-------send-hex------')
    print(str_bin + hex(crc))
    return units, crc