Example #1
0
# boot.py -- run on boot-up
from network import LoRa
import pycom
import ubinascii
import socket
import time
import lora_keys as keys

# Disable LTE radio
if pycom.lte_modem_en_on_boot():
    print("LTE is enabled. Disabling...")
    pycom.lte_modem_en_on_boot(False)

# Join LoRa network
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)
lora.set_battery_level(
    255)  # We are on battery but have no way to measure this.

# Try to read state from nvram otherwise join a network using OTAA
lora.nvram_restore()
if not lora.has_joined():
    lora.join(activation=LoRa.OTAA,
              auth=(ubinascii.unhexlify(keys.dev_eui),
                    ubinascii.unhexlify(keys.app_eui),
                    ubinascii.unhexlify(keys.app_key)),
              timeout=0)

    # wait until network joined
    while not lora.has_joined():
        time.sleep(2.5)
        print("Waiting to join...")
class Transceiver:
    def __init__(self,
                 app_eui=0,
                 app_key=0,
                 dev_addr=0,
                 nwk_swkey=0,
                 app_swkey=0,
                 dataRate=0,
                 adr=False,
                 useSavedState=True):
        self.app_eui = app_eui
        self.app_key = app_key
        self.dev_addr = dev_addr
        self.nwk_swkey = nwk_swkey
        self.app_swkey = app_swkey
        self.dataRate = dataRate
        self.adr = adr
        self.useSavedState = useSavedState

        # Initialize LoRa in LORAWAN mode
        self.lora = LoRa(mode=LoRa.LORAWAN,
                         region=LoRa.EU868,
                         power_mode=LoRa.TX_ONLY,
                         adr=self.adr)
        # Restore LoRa state from memory (join state, frame counter)
        if self.useSavedState:
            self.lora.nvram_restore()
        else:
            self.lora.nvram_erase()
        # Only (re)join if not joined before
        if not self.lora.has_joined():
            if self.app_eui != 0 and self.app_key != 0:
                # join a network using OTAA (Over the Air Activation). Start with sf12
                self.lora.join(activation=LoRa.OTAA,
                               auth=(self.app_eui, self.app_key),
                               dr=0,
                               timeout=0)
            elif self.dev_addr != 0 and self.nwk_swkey != 0 and self.app_swkey != 0:
                # join a network using ABP (Authentication By Personalization)
                self.lora.join(activation=LoRa.ABP,
                               auth=(self.dev_addr, self.nwk_swkey,
                                     self.app_swkey))
            else:
                print("Invalid ABP or OTAA keys")
                return
            # wait until the module has joined the network (ToDo: timeout to prevent indefinite loop)
            while not self.lora.has_joined():
                time.sleep(2.5)
                print('.', end='')
        return

    def transmit(self, payload):
        self.lora.set_battery_level(self.__getLoraBattLevel())
        # create a LoRa socket
        self.s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
        # set the LoRaWAN data rate
        self.s.setsockopt(socket.SOL_LORA, socket.SO_DR, self.dataRate)
        # selecting non-confirmed type of messages
        self.s.setsockopt(socket.SOL_LORA, socket.SO_CONFIRMED, False)
        # make the socket blocking
        # (waits for the data to be sent and for the 2 receive windows to expire)
        self.s.setblocking(True)
        # select port
        self.s.bind(1)
        # Send the LoRa message
        self.s.send(payload)
        # make the socket non-blocking
        # (because if there's no data received it will block forever...)
        self.s.setblocking(False)
        # get any data received (if any...)
        self.receivePayload = self.s.recv(64)
        #print("Store LoRa state in memory")
        self.lora.nvram_save()
        return

    def receive(self):
        return self.receivePayload

    def getMac(self):
        return binascii.hexlify(self.lora.mac()).upper().decode('utf-8')

    def stats(self):
        return self.lora.stats()

    def __getLoraBattLevel(self):
        adc = machine.ADC()
        battReg = adc.channel(pin='P16', attn=1)
        #print('Battery voltage:', battReg() * 3 * 1.334 / 4095)
        loraBattLevel = int(battReg() / 14.9)
        if loraBattLevel > 254:
            loraBattLevel = 0
        battVoltage = battReg() * 3 * 1.334 / 4095
        #print("Battery level: %d (%f V)" % (loraBattLevel, battVoltage))
        return loraBattLevel
Example #3
0
print(os.uname())
print("DevEUI: %s" % (binascii.hexlify(lora.mac())))
print("AppEUI: %s" % (binascii.hexlify(app_eui)))
print("AppKey: %s" % (binascii.hexlify(app_key)))

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

# wait until the module has joined the network
while not lora.has_joined():
    pycom.rgbled(0x140000)
    time.sleep(2.5)
    pycom.rgbled(0x000000)
    time.sleep(1.0)
    print('Not yet joined...')

print('OTAA joined')
pycom.rgbled(0x001400)

# create a LoRa socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)

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

# battery info for MAC Commands
lora.set_battery_level(254)

machine.main('main.py')
Example #4
0
app_swkey = binascii.unhexlify('C8C566468F877992625D3D8DA84896C3')

# remove all the channels
for channel in range(0, 72):
    lora.remove_channel(channel)

# set all channels to the same frequency (must be before sending the OTAA join request)
for channel in range(0, 72):
    lora.add_channel(channel,
                     frequency=config.LORA_FREQUENCY,
                     dr_min=0,
                     dr_max=3)

# join a network using ABP (Activation By Personalization)
lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey))
lora.set_battery_level(0)

# create a LoRa socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)

# set the LoRaWAN data rate
s.setsockopt(socket.SOL_LORA, socket.SO_DR, config.LORA_NODE_DR)

# make the socket blocking
s.setblocking(False)

# update node information

for i in range(200):
    pkt = b'PKT #' + bytes([i])
    value = "%.2f" % (si.temperature())