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
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
def to_cbor(self): ''' render the content of this object to a cbor byte array :return: a byte array ''' naming_map = { 'bn': -2, 'bt': -3, 'bu': -4, 'bv': -5, 'bs': -16, 'n': 0, 'u': 1, 'v': 2, 'vs': 3, 'vb': 4, 'vd': 8, 's': 5, 't': 6, 'ut': 7 } converted = [] self._build_rec_dict(naming_map, converted) return cbor_encoder.dumps(converted)
# h_prev = h # # if len(memo_ts) == 0: # memo_ts.append(m) # else: # memo_ts.append(m - m_prev) # m_prev = m # if len(mois_ts) == 0: mois_ts.append(mo) else: mois_ts.append(mo - mo_prev) mo_prev = mo print("temperature", len(cbor.dumps(temp_ts)), len(temp_ts), temp_ts) print("pressure ", len(cbor.dumps(humi_ts)), len(humi_ts), humi_ts) print("humidity ", len(cbor.dumps(pres_ts)), len(pres_ts), pres_ts) print("memory ", len(cbor.dumps(memo_ts)), len(memo_ts), memo_ts) print("moisture ", len(cbor.dumps(mois_ts)), len(mois_ts), mois_ts) answer = False # if we exceed the MTU size, then remove the last item, send and generate # a new list with the value we have removed. This is used mainly by SIGFOX # since the MTU is too short to store 60 samples. if len(cbor.dumps( temp_ts)) > MTU - coap_header_size: # room for CoAP header answer = send_coap_message(s, destination, "temperature", temp_ts[:-1]) temp_ts = [t]