Skip to content

Instantly share code, notes, and snippets.

@nemik
Created April 9, 2018 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nemik/c8be84ef2fa8c3f89c4a2a49f2643d8d to your computer and use it in GitHub Desktop.
Save nemik/c8be84ef2fa8c3f89c4a2a49f2643d8d to your computer and use it in GitHub Desktop.
from network import LoRa
import socket
import binascii
import struct
import time
import utime
# Initialize LoRa in LORAWAN mode.
# Please pick the region that matches where you are using the device:
# Asia = LoRa.AS923
# Australia = LoRa.AU915
# Europe = LoRa.EU868
# United States = LoRa.US915
def select_subband(lora, subband):
if (type(subband) is int):
if ((subband<1) or (subband>8)):
raise ValueError("subband out of range (1-8)")
else:
raise TypeError("subband must be 1-8")
for channel in range(0, 72):
lora.remove_channel(channel)
for channel in range((subband-1)*8, ((subband-1)*8)+8):
lora.add_channel(channel, frequency=902300000+channel*200000, dr_min=0, dr_max=3)
sb = 1 #Change to desired conduit frequency sub-band
print("starting")
# Initialize LoRa in LORAWAN mode.
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.US915, public=True)
#lora.nvram_restore()
select_subband(lora,sb)
print('Device ID: ' + binascii.hexlify(lora.mac()).lower().decode('utf-8'))
app_eui = binascii.unhexlify('MY_APP_ID_HEX')
app_key = binascii.unhexlify('MYS_APP_SECRET_HEX')
time.sleep(2)
# join a network using OTAA (Over the Air Activation)
print('before join')
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
print('did join')
# wait until the module has joined the network \
while not lora.has_joined():
time.sleep(5)
print('Not yet joined...')
print("join open socket")
# 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, 3)
# make the socket non-blocking
#s.setblocking(False)
s.setblocking(True)
pressure = 1
while lora.has_joined():
payload = '{"pressure":'+str(pressure)+',"time":'+str(utime.time())+'}'
print(payload)
s.send(payload)
time.sleep(10)
pressure +=1
if pressure > 100:
pressure = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment