nRF24 / RF24Network

OSI Layer 3 Networking for nRF24L01(+) and nRF52x on Arduino and Raspberry Pi
https://nrf24.github.io/RF24Network/
GNU General Public License v2.0
355 stars 164 forks source link

Multicast on Python (Master) with Arduino-Nodes #192

Closed SilentM4X closed 2 years ago

SilentM4X commented 2 years ago

I use a Raspberry Pi to communicate with 10 nodes via RF24Network. So far I've always addressed each node individually, which works quite well so far - but if devices are offline, there are delays and the code is very long:

        payload = pack('<LL', millis(), 0 )    
        network.write(RF24NetworkHeader(node2), payload)
        network.write(RF24NetworkHeader(node3), payload)
        network.write(RF24NetworkHeader(node4), payload)
        network.write(RF24NetworkHeader(node5), payload)
        network.write(RF24NetworkHeader(node012), payload)
        network.write(RF24NetworkHeader(node032), payload)
        network.write(RF24NetworkHeader(node042), payload)
        network.write(RF24NetworkHeader(node052), payload)
        network.write(RF24NetworkHeader(node053), payload)
        network.write(RF24NetworkHeader(node054), payload)
        network.write(RF24NetworkHeader(node055), payload)
        network.write(RF24NetworkHeader(node014), payload)
        network.write(RF24NetworkHeader(node015), payload)
        network.write(RF24NetworkHeader(node025), payload)
        network.write(RF24NetworkHeader(node035), payload)

Now I've read that there is network.multicast function, but I can only find information about handling in Arduino. I couldn't find informations how to use multicast on the Raspberry Pi with Python.

Does anyone have an idea how to do that?

2bndy5 commented 2 years ago

Multicast support in the python wrapper is something that was overlooked/incomplete for long time. I added some of it into the python wrapper for the last release.

I can only find information about handling in Arduino

I'm curious what info source(s) you imply here. The RF24Network::multicast() docs are meant to apply to both Linux and Arduino platforms.

Does anyone have an idea how to do that?

This is rather broad question. Multicasting is dependent on understanding the RF24Network topology (there's a section on that page about multicasting). Ultimately, if there are inactive nodes, then it severely limits the capability of the network.

IIRC, the radios' pipe 0 (for each network level) is set up to listen for any frames (multicasted or not) aimed at them. So, if a normal write() fails, then you could try multicasting to a specific level (and handle the payload appropriately). However, in the absence of forwarding nodes, the origin/transmitting node must be in range of a node on the specified network level.

but if devices are offline, there are delays and the code is very long

The RF24Network::txTimeout and RF24Network::routeTimeout can be used to reduce the delay. I would not recommend playing with RF24::setRetries() as it is used based on recommendations from the radio's datasheet and the context of the RF24Network operation.

SilentM4X commented 2 years ago

Since I always get the following error, I assumed that the documentation might exclude Linux.

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "Server.py", line 529, in bums
    callotherButtons(oct(header.from_node))
  File "Server.py", line 115, in callotherButtons
    network.multicast(RF24NetworkHeader(node023), payload, sizeof(payload), 01)
AttributeError: 'RF24Network' object has no attribute 'multicast'

The fact that not all nodes are always connected (does not happen regularly), is that this projekt is a quiz with wireless buzzers and not everything is used every time (eg. lightbars, movingheads etc.).

It results that usually only the first level and a few of the second level are used. I already switched all my led spots to wireless DMX 2.4 GHz to reduce the trouble.

Since now, i never used [RF24Network::txTimeout] and [RF24Network::routeTimeout], the [RF24::setRetries()] are set to radio.setRetries(10,15) (with default values it won't work)

2bndy5 commented 2 years ago

Since I always get the following error, I assumed ...

Please don't assume the devs know the entire context of your situation. I say this because it looks like this is the first issue you ever opened.


I think your setup is outdated. Python 2.7 is officially dead (since 2019 I believe) - the fact that your OS still has it tells me you're using an old RPi OS image. Furthermore, the python wrapper does have the multicast function, but you need to update your version of RF24Network (to v1.0.16) as this function was added in #176.

radio.setRetries(10,15) (with default values it won't work)

Are you calling the RF24::setRetries() after RF24Network::begin()? I ask because this lib appropriates that setting during begin().

SilentM4X commented 2 years ago

I think you are right - i set up the system 3 years ago and due to the complexity of the setup I never dared to change anything. But I take this as a lesson. Thanks for the tip. This will be the game changer :)

And yes, i call RF24::setRetries() after begin:

radio = RF24(22, 0)
network = RF24Network(radio)
radio.begin()
time.sleep(0.1)
network.begin(90, this_node)
radio.setDataRate(RF24_250KBPS)
radio.setRetries(10,15)
radio.setCRCLength(RF24_CRC_8)
radio.enableDynamicPayloads()
2bndy5 commented 2 years ago

We have an updated setup script that should make your life easier. See updated automated install instructions

EDIT: The newer setup script doesn't install the same python wrappers. Rather it uses the newer pyRF24 lib instead. I say this because the pyRF24 API is a little different (setRetries() is set_retries() in pyRF24 pkg). If you want to use the same python wrapper you're using now, then you'll have to manually build and install them after using the setup script.

SilentM4X commented 2 years ago

Nice, thank you very much. I will try it in the next days :)

2bndy5 commented 2 years ago

ping

Can we close this? Is there additional need for help?

SilentM4X commented 2 years ago

Hey, yes please and thank you :)

2bndy5 commented 2 years ago

If you have trouble building the python wrappers, feel free to ask on this thread or start a new one.