Last active
July 14, 2019 13:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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