コード例 #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
コード例 #3
0
ファイル: sensor.py プロジェクト: pclavier92/SCHC-1
def compress_and_send():
    """ Compresses and sends dummy data """
    coap_message = CoAP.Message()

    coap_message.new_header(type=CoAP.CON,
                            code=CoAP.POST,
                            midSize=4,
                            token=0x82)
    coap_message.add_option_path('foo')
    coap_message.add_option_path('bar')
    coap_message.end_option()

    ipv6 = make_ipudp_buffer(IPV6_SOURCE, IPV6_DEST, 5682, 5555,
                             coap_message.buffer)

    fields, data = PARSER.parser(ipv6)
    rule = COMP.RuleMngt.FindRuleFromHeader(fields, "up")
    if rule != None:
        result = struct.pack('!B', rule["ruleid"])  # start with the ruleid
        res = COMP.apply(fields, rule["content"], "up")
        res.add_bytes(data)
        result += res.buffer()

        print("Compressed Header = ", result)

        LORA_SOCKET.setblocking(True)
        LORA_SOCKET.settimeout(10)
        try:
            LORA_SOCKET.send(result)
        except Exception as exception:
            print("TIMEOUT in sending : " + str(exception))

        try:
            data = LORA_SOCKET.recv(64)
            data_available = True
        except Exception as exception:
            print('timeout in receive' + str(exception))
            data_available = False

        LORA_SOCKET.setblocking(False)

        if data_available:
            print("receive DATA", data)
コード例 #4
0
ファイル: sensor.py プロジェクト: pclavier92/SCHC-1
if LORAWAN:
    lora = LoRa(mode=LoRa.LORAWAN)
    increase_delivary_chances_function = CoAP.increase_delivary_chances_functions["LORAWAN"]
if SIGFOX:
    sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ1)
    increase_delivary_chances_function = CoAP.increase_delivary_chances_functions["SIGFOX"]

RM = RuleManager()
RM.addRule (rule_coap0)
RM.addRule (rule_coap1)
RM.addRule (rule_coap2)

p = Parser()
comp = Compressor(RM)
dec  = Decompressor(RM)
coapC = CoAP.CoAPSM(p, comp, dec, IPv6_source, IPv6_dest, increase_delivary_chances_function)

app_eui = binascii.unhexlify('00 00 00 00 00 00 00 00'.replace(' ',''))
app_key = binascii.unhexlify('11 22 33 44 55 66 77 88 11 22 33 44 55 66 77 88'.replace(' ',''))

pycom.heartbeat(False)


if LORAWAN:
    mac = lora.mac()
    print ('MAC:')
    print(hex(mac[0]), end='-')
    print(hex(mac[1]), end='-')
    print(hex(mac[2]), end='-')
    print(hex(mac[3]), end='-')
    print(hex(mac[4]), end='-')
コード例 #5
0
#bmp = BMP280(i2c)

print ('MEM', gc.mem_free())


lora = LoRa(mode=LoRa.LORAWAN)

RM = RuleManager()
RM.addRule (rule_coap0)
RM.addRule (rule_coap1)

p = Parser()
comp = Compressor(RM)
dec  = Decompressor(RM)
coapC = CoAP.CoAPSM(p, comp, dec, IPv6_source, IPv6_dest)


app_eui = binascii.unhexlify('00 00 00 00 00 00 00 00'.replace(' ',''))
app_key = binascii.unhexlify('11 22 33 44 55 66 77 88 11 22 33 44 55 66 77 88'.replace(' ',''))

mac = lora.mac()
print ('MAC:')
print(hex(mac[0]), end='-')
print(hex(mac[1]), end='-')
print(hex(mac[2]), end='-')
print(hex(mac[3]), end='-')
print(hex(mac[4]), end='-')
print(hex(mac[5]), end='-')
print(hex(mac[6]), end='-')
print(hex(mac[7]))
コード例 #6
0
MAX_FRAME = 20

while True:
    (rp, p, t) = bmp.getValue(0)
    t100 = int(t * 100)

    deltaT = t100 - oldT
    d = CBOR(deltaT)

    print(deltaT)
    if ((temp_record.length() + d.length()) < MAX_FRAME):
        temp_record.addList(d)
        temp_record.dump()
    else:
        print("Sending CoAP")
        CoAPMsg = CoAP.Message()
        CoAPMsg.new_header(type=CoAP.CON,
                           code=CoAP.POST,
                           midSize=4,
                           token=0x82)
        CoAPMsg.add_option_path('foo')
        CoAPMsg.add_option_content(60)  #CBOR
        CoAPMsg.end_option()
        CoAPMsg.add_value(temp_record)

        s.setblocking(True)
        s.settimeout(10)

        try:
            s.send(CoAPMsg.buffer)
            print(CoAPMsg.buffer)