Encoder and Decoder for CayenneLLP
Project description
PyCayenneLPP
A Cayenne Low Power Payload (CayenneLPP) decoder and encoder written in Python.
PyCayenneLPP offers a concise interface with proper encoding and decoding functionality for the CayenneLPP format, supporting many sensor types. The project aims for overall high code quality and good test coverage.
See also myDevicesIoT/CayenneLPP for more information on the format and a reference implementation in C++.
The project is under active development. Releases will be published on the fly as soon as a certain number of new features and fixes have been made.
Supported Data Types
The following table lists the currently supported data types with the LPP code (which equals IPSO code - 3200), data size in bytes, dimensions, signedness, and data resolution.
Type Name | LPP | Size | Dim | Signed | Resolution |
---|---|---|---|---|---|
Digital Input | 0 | 1 | 1 | False | 1 |
Digital Output | 1 | 1 | 1 | False | 1 |
Analog Input | 2 | 2 | 1 | True | 0.01 |
Analog Output | 3 | 2 | 1 | True | 0.01 |
Generic Sensor | 100 | 4 | 1 | False | 1 |
Illuminance | 101 | 2 | 1 | False | 1 Lux |
Presence | 102 | 1 | 1 | False | 1 |
Temperature | 103 | 2 | 1 | True | 0.1°C |
Humidity | 104 | 1 | 1 | False | 0.5 % |
Accelerometer | 113 | 6 | 3 | True | 0.001 G |
Barometer | 115 | 2 | 1 | False | 0.1 hPa |
Voltage | 116 | 2 | 1 | False | 0.01 V |
Current | 117 | 2 | 1 | False | 0.001 A |
Frequency | 118 | 4 | 1 | False | 1 Hz |
Percentage | 120 | 1 | 1 | False | 1 % |
Altitude | 121 | 2 | 1 | True | 1 m |
Load | 122 | 3 | 1 | True | 0.001 kg |
Concentration | 125 | 2 | 1 | False | 1 |
Power | 128 | 2 | 1 | False | 1 |
Distance | 130 | 4 | 1 | False | 0.001 km |
Energy | 131 | 4 | 1 | False | 0.001 kJ |
Direction | 132 | 2 | 1 | False | 1 ° |
Time | 133 | 4 | 1 | False | 1 s |
Gyrometer | 134 | 6 | 3 | True | 0.01 °/s |
Colour | 135 | 3 | 3 | False | 1 RGB |
Location | 136 | 9 | 3 | True | 0.00001 lat |
0.00001 lon | |||||
0.01 alt | |||||
Switch | 142 | 1 | 1 | False | 1 on/off |
Getting Started
PyCayenneLPP does not have any external dependencies and only uses builtin functions and types of Python 3. It is compatible with all the latest and officially supported Python versions 3.6 and above, though even Python 3.4 will do.
Since PyCayenneLPP 1.2.0 MicroPython is officially supported, and published
as a separate package under micropython-pycayennelpp
.
Python 3 Prerequisites
The PyCayenneLPP package is available via PyPi using pip
. To install it run:
pip3 install pycayennelpp
MicroPython Prerequisites
Using MicroPythons upip
module PyCayenneLPP can be installed as follows
within MicroPython:
import upip
upip.install("micropython-pycayennelpp")
Or alternatively run with in a shell:
micropython -m upip install micropython-pycayennelpp
Usage Examples
The following show how to utilise PyCayenneLPP in your own application to encode and decode data into and from CayenneLPP. The code snippets work with standard Python 3 as well as MicroPython, assuming you have installed the PyCayenneLPP package as shown above.
Encoding
from cayennelpp import LppFrame
# create empty frame
frame = LppFrame()
# add some sensor data
frame.add_temperature(0, -1.2)
frame.add_humidity(6, 34.5)
# get byte buffer in CayenneLPP format
buffer = bytes(frame)
Note: MicroPython does not support bytes(frame)
utilising the internal
method LppFrame.__bytes__(self)
(yet).
Hence, you need to use LppFrame.to_bytes(self)
instead.
Decoding
from cayennelpp import LppFrame
# byte buffer in CayenneLPP format with 1 data item
# i.e. on channel 1, with a temperature of 25.5C
buffer = bytearray([0x01, 0x67, 0x00, 0xff])
# create frame from bytes
frame = LppFrame().from_bytes(buffer)
# print the frame and its data
print(frame)
JSON Encoding
The LppUtil class provides helper function for proper JSON encoding of PyCayenneLpp types, i.e. LppFrame, LppData and LppType.
import json
from cayennelpp import LppFrame, LppUtil
# create empty frame
frame = LppFrame()
# add some sensor data
frame.add_temperature(0, -1.2)
frame.add_humidity(6, 34.5)
# json encoding
print(json.dumps(frame, default=LppUtil.json_encode, indent=2))
There are two wrapper functions to explicitly encode the LPP type as a
number or string, number being default for LppUtil.json_encode
(see above):
# type as number
print(json.dumps(frame, default=LppUtil.json_encode_type_int, indent=2))
# type as string
print(json.dumps(frame, default=LppUtil.json_encode_type_str, indent=2))
Contributing
Contributing to a free open source software project can take place in many different ways. Feel free to open issues and create pull requests to help improving this project. Each pull request has to pass some automatic tests and checks run by Travis-CI before being merged into the master branch.
Please take note of the contributing guidelines and the Code of Conduct.
License
This is a free open source software project published under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file micropython-pycayennelpp-2.4.0.tar.gz
.
File metadata
- Download URL: micropython-pycayennelpp-2.4.0.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4980c69cdd25ecd527a8cd0de36e5ad5699e110a8beb76ae348197a0a177c9de |
|
MD5 | 40be6820edd54bb359fc7ffe77cdfc39 |
|
BLAKE2b-256 | 69b9f925d5c65f360d9fdebdb1cdf15ae4543e75b30cbf8a6faa0ef60ce59b10 |
File details
Details for the file micropython_pycayennelpp-2.4.0-py3-none-any.whl
.
File metadata
- Download URL: micropython_pycayennelpp-2.4.0-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8b5bb4fbbf940fe209eb36d4e557bdf45975d23723f112ff4b72210a1752125 |
|
MD5 | 4e9a5667563e32c66f85ae5ef9b3f2fd |
|
BLAKE2b-256 | 5b7866eb131f0effabf4075f81491a4db0c3035ddd53051a45eff0e971268edc |