Skip to content

Instantly share code, notes, and snippets.

@jezmck
Last active July 14, 2019 13:47
Show Gist options
  • Save jezmck/eba71efe33df12450db13b6218945cdc to your computer and use it in GitHub Desktop.
Save jezmck/eba71efe33df12450db13b6218945cdc to your computer and use it in GitHub Desktop.
Overly complex Raspberry Pi LoRa Node pHAT python code to get up and running
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
print('Starting...', flush=True)
import logging
logging.basicConfig(level=logging.DEBUG,
filename='/home/pi/lora/mapping.log',
filemode='a',
format='[%(asctime)s] %(message)s')
logging.debug('')
logging.debug('Starting.')
try:
import pathlib
route = 'joined.txt'
path = pathlib.Path(route)
joined = (path.exists() and path.is_file())
import time
from rak811 import Rak811, Mode
lora = Rak811(event_timeout=10)
while not joined:
# noinspection PyBroadException
try:
print('HR', flush=True)
logging.debug('HR')
lora.hard_reset()
lora.mode = Mode.LoRaWan
lora.band = 'EU868'
lora.set_config(dev_eui='00259C84D0BD9257',
app_eui='70B3D57ED001CCC8',
app_key='10FE906CB03567CD2F84B70CF72C082C')
lora.join_otaa()
joined = True
print('J\t\t✅', flush=True)
logging.debug('J\t\t✅')
lora.dr = 5
# create file
path.open(mode="w+")
except (KeyboardInterrupt, SystemExit):
raise
except:
path.unlink()
print('J\t\t❌', flush=True)
logging.debug('J\t\t❌')
sent = 0
while True:
# noinspection PyBroadException
try:
lora.send(str(sent))
sent += 1
print('M\t' + str(sent) + '\t✅', flush=True)
logging.debug('M\t' + str(sent) + '\t✅')
time.sleep(30)
except (KeyboardInterrupt, SystemExit):
raise
except:
print('M\t' + str(sent) + '\t❌', flush=True)
logging.debug('M\t' + str(sent) + '\t❌')
time.sleep(5)
lora.close() # might never happen
except (KeyboardInterrupt, SystemExit):
print('\nQuitting.', flush=True)
logging.debug('Quitting.')
exit()
[2019-07-14 14:45:29,835]
[2019-07-14 14:45:29,837] Starting.
[2019-07-14 14:45:34,962] HR
[2019-07-14 14:45:40,050] J ✅
[2019-07-14 14:45:40,282] M 0 ❌
[2019-07-14 14:45:41,816] Quitting.
[2019-07-14 14:45:43,033]
[2019-07-14 14:45:43,035] Starting.
[2019-07-14 14:45:48,260] M 0 ❌
[2019-07-14 14:45:49,754] Quitting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment