コード例 #1
0
    def send_coap_message(sock,
                          destination,
                          uri_path,
                          message,
                          unique_id=None):
        if destination[0] == "SIGFOX":  # do SCHC compression
            global sigfox_MID
            """ SCHC compression for Sigfox, use rule ID 0 stored on 2 bits,
            followed by MID on 4 bits and 2 bits for an index on Uri-path.

            the SCHC header is RRMMMMUU
            """
            uri_idx = ['temperature', 'pressure', 'humidity',
                       'memory'].index(uri_path)

            schc_residue = 0x00  # ruleID in 2 bits RR
            schc_residue |= (sigfox_MID << 2) | uri_idx  # MMMM and UU

            sigfox_MID += 1
            sigfox_MID &= 0x0F  # on 4 bits
            if sigfox_MID == 0: sigfox_MID = 1  # never use MID = 0

            msg = struct.pack("!B",
                              schc_residue)  # add SCHC header to the message
            msg += cbor.dumps(message)

            print("length", len(msg), binascii.hexlify(msg))
            s.send(msg)
            return None  # don't use downlink

        # for other technologies we wend a regular CoAP message
        coap = CoAP.Message()
        coap.new_header(type=CoAP.NON, code=CoAP.POST)
        coap.add_option(CoAP.Uri_path, uri_path)
        if unique_id:
            coap.add_option(CoAP.Uri_path, unique_id)
        # /proxy/mac_address
        coap.add_option(CoAP.Content_format, CoAP.Content_format_CBOR)
        coap.add_option(CoAP.No_Response,
                        0b00000010)  # block 2.xx notification
        coap.add_payload(cbor.dumps(message))
        coap.dump(hexa=True)
        answer = CoAP.send_ack(s, destination, coap)

        return answer
コード例 #2
0
def send_coap_message(sock, destination, uri_path, message, unique_id = None):
    if destination == "LORAWAN": # do SCHC compression
        global lorawan_MID # /!\ change name to lorawan_token
        """ SCHC compression for LoraWAN, use rule ID 98 stored in fPort,
        followed by MID on 4 bits and 4 bits for an index on Uri-path.
        the SCHC header is MMMM UUUU
        """
        # uri_index = ["humidity_l", "temperature_l", "pressure_l", "memory_l", None, None, None, None, None,
        #           None, None, None, None, None, None, None,].index(uri_path)
        # print("uri_index",uri_index)
        print("MID", lorawan_MID)
        print("MID", bin(lorawan_MID))
        #schc_residue = (lorawan_MID << 4) | uri_index # MMMM and UUUU
        schc_residue = (lorawan_MID & 0xFF)
        print("SCHC_RESIDUE", bin(schc_residue))
        print("SCHC_RESIDUE normal", schc_residue)
        lorawan_MID += 1
        lorawan_MID &= 0x0F
        if lorawan_MID == 0: lorawan_MID = 1 # never use MID = 0
        msg = struct.pack("!B", schc_residue) # add SCHC header to the message
        msg += cbor.dumps(message)
        print ("length", len(msg), binascii.hexlify(msg))
        rule_ID = 98
        sock.bind(rule_ID)
        sock.send(msg)
        return None # don't use downlink
    else:
        # for WiFi we wend a regular CoAP message
        coap = CoAP.Message()
        coap.new_header(type=CoAP.NON, code=CoAP.POST)
        coap.add_option(CoAP.Uri_path, uri_path)
        if unique_id:
            coap.add_option(CoAP.Uri_path, unique_id)
        coap.add_option (CoAP.Content_format, CoAP.Content_format_CBOR)
        coap.add_option (CoAP.No_Response, 0b00000010) # block 2.xx notification
        coap.add_payload(cbor.dumps(message))
        coap.dump(hexa=True)
        answer = CoAP.send_ack(sock, destination, coap)
        return answer