Exemple #1
0
    def connect(self, dev_eui, app_eui, app_key, ports=1, callback=None):
        """
    Connect device to LoRa.
    Set the socket and lora instances.
    """

        dev_eui = unhexlify(dev_eui)
        app_eui = unhexlify(app_eui)
        app_key = unhexlify(app_key)
        self.callback = callback  # call back routine on LoRa reply callback(port,response)
        # Disable blue blinking and turn LED off
        LED.heartbeat(False)
        LED.off()

        # Initialize LoRa in LORAWAN mode
        self.lora = LoRa(mode=LoRa.LORAWAN)

        # Join a network using OTAA (Over the Air Activation)
        self.lora.join(activation=LoRa.OTAA,
                       auth=(dev_eui, app_eui, app_key),
                       timeout=0)

        # Wait until the module has joined the network
        count = 0
        while not self.lora.has_joined():
            LED.blink(1, 2.5, 0xff0000)
            if count > 20:
                return False
            print("Trying to join: ", count)
            count += 1

        # Create a LoRa socket
        LED.blink(2, 0.1, 0x009900)
        self.s = []
        self.s.append(socket.socket(socket.AF_LORA, socket.SOCK_RAW))

        # Set the LoRaWAN data rate
        self.s[0].setsockopt(socket.SOL_LORA, socket.SO_DR, 5)

        # Make the socket non-blocking
        self.s[0].setblocking(False)

        print("Success after %d tries" % count)
        # print("Create LoRaWAN socket")

        # Create a raw LoRa socket
        # default port 2
        self.s.append(None)
        for nr in range(ports):
            print("Setting up port %d" % (nr + 2))
            self.s.append(socket.socket(socket.AF_LORA, socket.SOCK_RAW))
            self.s[nr + 2].setblocking(False)
            if nr: self.s[nr + 2].bind(nr + 2)
        LED.off()
        return True
Exemple #2
0
    def connect(self, dev_eui, app_eui, app_key):
        """
    Connect device to LoRa.
    Set the socket and lora instances.
    """

        dev_eui = unhexlify(dev_eui)
        app_eui = unhexlify(app_eui)
        app_key = unhexlify(app_key)

        # Disable blue blinking and turn LED off
        LED.heartbeat(False)
        LED.off()

        # Initialize LoRa in LORAWAN mode
        self.lora = LoRa(mode=LoRa.LORAWAN)

        # Join a network using OTAA (Over the Air Activation)
        self.lora.join(activation=LoRa.OTAA,
                       auth=(dev_eui, app_eui, app_key),
                       timeout=0)

        # Wait until the module has joined the network
        count = 0
        while not self.lora.has_joined():
            LED.blink(1, 2.5, 0xff0000)
            # print("Trying to join: " ,  count)
            count = count + 1

        # Create a LoRa socket
        LED.blink(2, 0.1)
        self.s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)

        # Set the LoRaWAN data rate
        self.s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)

        # Make the socket non-blocking
        self.s.setblocking(False)

        # print ("Joined! ",  count)
        # print("Create LoRaWAN socket")

        # Create a raw LoRa socket
        self.s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
        self.s.setblocking(False)