From bbceb8683bc62d400cf740ecf911e64e2a5d2702 Mon Sep 17 00:00:00 2001 From: Akira Naru Takizawa Date: Sat, 19 Oct 2019 21:17:10 +0200 Subject: [PATCH] Start properties for RN2483 class Create modul structure --- .gitignore | 3 +- .idea/dictionaries/akira.xml | 8 +++ .idea/rn2483lora.iml | 1 + __init__.py => rn2483lora/__init__.py | 7 ++- rn2483lora/cli.py | 76 ++++++++++++++++++++++++ rn2483lora.py => rn2483lora/rn2483.py | 85 ++++++--------------------- setup.py | 44 ++++++++++++++ 7 files changed, 155 insertions(+), 69 deletions(-) create mode 100644 .idea/dictionaries/akira.xml rename __init__.py => rn2483lora/__init__.py (57%) create mode 100644 rn2483lora/cli.py rename rn2483lora.py => rn2483lora/rn2483.py (54%) create mode 100644 setup.py diff --git a/.gitignore b/.gitignore index bfc8025..a5114a4 100644 --- a/.gitignore +++ b/.gitignore @@ -114,4 +114,5 @@ dmypy.json # Pyre type checker .pyre/ -/test/lora.conf +/test/ +/make.conf diff --git a/.idea/dictionaries/akira.xml b/.idea/dictionaries/akira.xml new file mode 100644 index 0000000..e8d6525 --- /dev/null +++ b/.idea/dictionaries/akira.xml @@ -0,0 +1,8 @@ + + + + hweui + preprogrammed + + + \ No newline at end of file diff --git a/.idea/rn2483lora.iml b/.idea/rn2483lora.iml index d99a03d..f391f26 100644 --- a/.idea/rn2483lora.iml +++ b/.idea/rn2483lora.iml @@ -3,6 +3,7 @@ + diff --git a/__init__.py b/rn2483lora/__init__.py similarity index 57% rename from __init__.py rename to rn2483lora/__init__.py index 38e7af4..06ed000 100644 --- a/__init__.py +++ b/rn2483lora/__init__.py @@ -1,5 +1,10 @@ # -*- coding: utf-8 -*- """Module for LoRaWAN chip RN2483.""" +from rn2483 import RN2483 __author__ = "Sven Sager" __copyright__ = "Copyright (C) 2019 Sven Sager" -__license__ = "GPLv3" +__license__ = "LGPLv3" +__version__ = "0.1.0" +__all__ = [ + "RN2483", +] diff --git a/rn2483lora/cli.py b/rn2483lora/cli.py new file mode 100644 index 0000000..8aa65c7 --- /dev/null +++ b/rn2483lora/cli.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +"""Command line for RN2483 LoRaWAN.""" +from rn2483 import RN2483 + +__author__ = "Sven Sager" +__copyright__ = "Copyright (C) 2019 Sven Sager" +__license__ = "LGPLv3" + +if __name__ == '__main__': + from configparser import ConfigParser + from time import sleep + + conf = ConfigParser() + conf.read("lora.conf") + + root = RN2483(conf.get("DEFAULT", "port")) + + def first_init() -> None: + """Init our RN2483 after firmware startup.""" + + print(root.version) + print(root.hweui) + + # Configuration: ["cmd", results] + cmd_list = [ + ["mac reset 868", 1], + + # Set TTN configurations form .conf file + ["mac set appskey {0}".format(conf.get("DEFAULT", "appskey")), 1], + ["mac set nwkskey {0}".format(conf.get("DEFAULT", "nwkskey")), 1], + ["mac set devaddr {0}".format(conf.get("DEFAULT", "devaddr")), 1], + + ["mac join abp", 2], + ["mac set ar on", 1], + ] + for do_cmd in cmd_list: + print("<-", do_cmd[0]) + print("->", root.send(do_cmd[0])) + + # Get awaited extra results results + for i in range(do_cmd[1] - 1): + print(" ", root.get_result()) + + first_init() + + # Command mode + while True: + results = 1 + + cmd_input = input("<- ") + if cmd_input == "exit": + break + elif cmd_input == "reset": + root.send("sys reset") + sleep(3) + first_init() + continue + + # Functions to make live more easier + elif cmd_input.find("send ") > -1: + lst = cmd_input.split() + cmd_input = "mac tx uncnf {port} {bytes}".format( + port=lst[2] if len(lst) > 2 else 1, + bytes=lst[1] if len(lst) > 1 else "", + ) + results = 2 + + # Always send command + if cmd_input: + print("->", root.send(cmd_input)) + + # Get awaited extra results results + for i in range(results - 1): + print(" ", root.get_result()) + + root.close() diff --git a/rn2483lora.py b/rn2483lora/rn2483.py similarity index 54% rename from rn2483lora.py rename to rn2483lora/rn2483.py index 92fb495..9a2fe10 100644 --- a/rn2483lora.py +++ b/rn2483lora/rn2483.py @@ -7,7 +7,7 @@ from serial import Serial __author__ = "Sven Sager" __copyright__ = "Copyright (C) 2019 Sven Sager" -__license__ = "GPLv3" +__license__ = "LGPLv3" class RN2483: @@ -83,71 +83,22 @@ class RN2483: if get_result: return self.get_result(timeout) + @property + def hweui(self): + """ + Reads the preprogrammed EUI node address. -if __name__ == '__main__': - from configparser import ConfigParser - from time import sleep + This command reads the preprogrammed EUI node address from the RN2483 + module. The value returned by this command is a globally unique number + provided by Microchip. + :return: Preprogrammed EUI node address + """ + return self.send("sys get hweui", True) - conf = ConfigParser() - conf.read("lora.conf") - - root = RN2483(conf.get("DEFAULT", "port")) - - def first_init() -> None: - """Init our RN2483 after firmware startup.""" - - # Configuration: ["cmd", results] - cmd_list = [ - ["sys get ver", 1], - ["mac reset 868", 1], - ["sys get hweui", 1], - - # Set TTN configurations form .conf file - ["mac set appskey {0}".format(conf.get("DEFAULT", "appskey")), 1], - ["mac set nwkskey {0}".format(conf.get("DEFAULT", "nwkskey")), 1], - ["mac set devaddr {0}".format(conf.get("DEFAULT", "devaddr")), 1], - - ["mac join abp", 2], - ["mac set ar on", 1], - ] - for do_cmd in cmd_list: - print("<-", do_cmd[0]) - print("->", root.send(do_cmd[0])) - - # Get awaited extra results results - for i in range(do_cmd[1] - 1): - print(" ", root.get_result()) - - first_init() - - # Command mode - while True: - results = 1 - - cmd_input = input("<- ") - if cmd_input == "exit": - break - elif cmd_input == "reset": - root.send("sys reset") - sleep(3) - first_init() - continue - - # Functions to make live more easier - elif cmd_input.find("send ") > -1: - lst = cmd_input.split() - cmd_input = "mac tx uncnf {port} {bytes}".format( - port=lst[2] if len(lst) > 2 else 1, - bytes=lst[1] if len(lst) > 1 else "", - ) - results = 2 - - # Always send command - if cmd_input: - print("->", root.send(cmd_input)) - - # Get awaited extra results results - for i in range(results - 1): - print(" ", root.get_result()) - - root.close() + @property + def version(self) -> str: + """ + Get information on hardware platform, firmware version, release date. + :return: RN2483 X.Y.Z MMM DD YYYY HH:MM:SS + """ + return self.send("sys get ver", True) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1962342 --- /dev/null +++ b/setup.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +"""Setup for rn2482lora module.""" +from setuptools import setup + +__author__ = "Sven Sager" +__copyright__ = "Copyright (C) 2019 Sven Sager" +__license__ = "LGPLv3" + +setup( + version='0.1.0', + + packages=['rn2483lora'], + python_requires="~=3.4", + install_requires=["pyserial"], + keywords="lora network lorawan iot", + + # Additional meta-data + name='rn2483lora', + author='Sven Sager', + author_email='akira@narux.de', + maintainer="Sven Sager", + maintainer_email="akira@narux.de", + url='https://narux.de', + description='Wraper module for microchips LoRaWAN chip RN2483', + long_description="Add LoRaWAN capabilities to your python program. This module will help you " + "to use the RN2483 chip of microchips. It will manage the serial communication " + "and send data over LoRaWAN.", + download_url="", + classifiers=[ + "Development Status :: 1 - Planning", + # "Development Status :: 2 - Pre-Alpha", + # "Development Status :: 3 - Alpha", + # "Development Status :: 4 - Beta", + # "Development Status :: 5 - Production/Stable", + "Environment :: Console", + "Intended Audience :: Developers", + "License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)", + "Operating System :: OS Independent", + "Programming Language :: Python :: 3 :: Only", + "Topic :: Software Development :: Libraries :: Python Modules", + "Topic :: System :: Networking", + ], + license='LGPLv3', +)