Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
titlemain.py
linenumberstrue
""" Cayenne LPP with LoPy Nano Gateway """
from network import LoRa
import time
import socket
import binascii
import ubinascii
from CayenneLPP import cayenneLPP
from pysense import Pysense
from SI7006A20 import SI7006A20
import pycom
import micropython
import machine


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

# create an OTA authentication params
dev_eui = ubinascii.unhexlify('') # these settings can be found from TTN
app_eui = ubinascii.unhexlify('') # these settings can be found from TTN
app_key = ubinascii.unhexlify('') # these settings can be found from TTN

# set the 3 default channels to the same frequency (must be before sending the OTAA join request)
lora.add_channel(0, frequency=868100000916800000, dr_min=0, dr_max=5)
lora.add_channel(1, frequency=868100000916800000, dr_min=0, dr_max=5)
lora.add_channel(2, frequency=868100000916800000, dr_min=0, dr_max=5)

# join a network using OTAA
lora.join(activation=LoRa.OTAA, auth=(dev_eui, app_eui, app_key), timeout=0)

# wait until the module has joined the network
while not lora.has_joined():
    time.sleep(2.5)
    print('Not joined yet...')


# remove all the non-default channels
for i in range(3, 16):
    lora.remove_channel(i)

# 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)

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

time.sleep(5.0)

print('Network joined!')

""" Your own code can be written below! """

while True:
    # create a LoRa socket
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    s.setsockopt(socket.SOL_LORA, socket.SO_DR, 0)
    s.setblocking(True)

    # creating Cayenne LPP packet
    lpp = cayenneLPP.CayenneLPP(size = 100, sock = s)

    temperature = (machine.rng() * .0000001) + 30
    lpp.add_temperature(temperature)
    lpp.send(reset_payload = True)
    print('packet sent')

    humidity = (machine.rng() * .0000001) + 50
    lpp.add_relative_humidity(humidity)
    lpp.send(reset_payload = True)
    print('packet sent')

    time.sleep(30)

...