Skip to content

Adding msgpack example #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 16, 2024
Merged

Conversation

FoamyGuy
Copy link
Contributor

This is similar to the exist simple transmit example but is modified to send a dictionary payload using msgpack

@FoamyGuy
Copy link
Contributor Author

@jerryneedell Great job on this new library! I set up two radio featherwings to try it out and everything was very smooth sailing to get them running under this new module.

@jerryneedell
Copy link
Collaborator

jerryneedell commented Aug 16, 2024

This is similar to the exist simple transmit example but is modified to send a dictionary payload using msgpack

Where do the io and msgpack modules come from for this example?

I should add that I am trying to run this on Raspberry Pi and pip is not finding them. Are they available for "blinka"
I see that they are part of the CP core. I'm just wondering if this example can also run on a Pi?

Nevermind -- pip install msgpack adafruit-io got them.

@FoamyGuy
Copy link
Contributor Author

FoamyGuy commented Aug 16, 2024

They are from core modules.

from io import BytesIO exists as a built-in module in CPython as well and I believe it works the same, but I have not tested it.

msgpack is in the circuitpython core. But in CPython it is not a built-in module. It's installed from pip https://pypi.org/project/msgpack/. However I am not 100% sure if the API is the exact same as CircuitPython.

@jerryneedell
Copy link
Collaborator

on the Pi, If I try to run this on two systems, I get this error when I start the second:

(blinka_venv) jerryneedell@gjnpirfm9x:~/projects/common_rfm/examples $ python rfm_msgpack_data.py 
Waiting for packets...
Received (raw data):  bytearray(b'message number 0')
Traceback (most recent call last):
  File "/home/jerryneedell/projects/common_rfm/examples/rfm_msgpack_data.py", line 101, in <module>
    unpacked_msg = msgpack.unpack(b)
                   ^^^^^^^^^^^^^^^^^
  File "/home/jerryneedell/blinka_venv/lib/python3.11/site-packages/msgpack/__init__.py", line 47, in unpack
    return unpackb(data, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "msgpack/_unpacker.pyx", line 201, in msgpack._cmsgpack.unpackb
msgpack.exceptions.ExtraData: unpack(b) received extra data.

@jerryneedell
Copy link
Collaborator

They are from core modules.

from io import BytesIO exists as a built-in module in CPython as well and I believe it works the same, but I have not tested it.

msgpack is in the circuitpython core. But in CPython it is not a built-in module. It's installed from pip https://pypi.org/project/msgpack/. However I am not 100% sure if the API is the exact same as CircuitPython.

I was getting an error that no module io was found until I installed adafruit-io

@jerryneedell
Copy link
Collaborator

The problem may be that the startup message is not constructed with msgpack - Is that causing the error.

@jerryneedell
Copy link
Collaborator

Yes -- I think taht is its -- I just commented out the startup message and it works fine.

is Fun!')
Received (unpacked): {'counter': 2, 'list': [True, False, None, 1, 3.14], 'str': 'CircuitPython is Fun!'}
Received (raw data):  bytearray(b'\x83\xa7counter\x03\xa4list\x95\xc3\xc2\xc0\x01\xcb@\t\x1e\xb8Q\xeb\x85\x1f\xa3str\xb5CircuitPython is Fun!')
Received (unpacked): {'counter': 3, 'list': [True, False, None, 1, 3.14], 'str': 'CircuitPython is Fun!'}

# initialize counter
counter = 0
# send a broadcast mesage
rfm.send(bytes(f"message number {counter}", "UTF-8"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This startup message is not formed with msgpack so it causes problems if send to another system expecting a msgpack encoded packet. Maybe remove this startup message or add error handling for non=msgpack packets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I've removed that one with the latest commit

@jerryneedell jerryneedell merged commit 91f2038 into adafruit:main Aug 16, 2024
1 check passed
@jerryneedell
Copy link
Collaborator

Looks great -- Nice example!

adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Aug 20, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15 to 2.4.0 from 2.3.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_ADS1x15#99 from RoaCode/comparator_settings

Updating https://github.com/adafruit/Adafruit_CircuitPython_RFM to 1.0.1 from 1.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_RFM#2 from FoamyGuy/msgpack_example

Updating https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad to 1.23.1 from 1.23.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_ImageLoad#85 from FoamyGuy/use_ruff

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants