microbit-foundation / micropython-microbit-v2

Temporary home for MicroPython for micro:bit v2 as we stablise it before pushing upstream
MIT License
41 stars 22 forks source link

radio packets are being dropped #144

Open rhubarbdog opened 1 year ago

rhubarbdog commented 1 year ago

Using 3 microbits, 2 transmitting and one counting packets, not all packets are being recieved by the counter.

yap.py 19682 received 318 dropped message.py 1822 received 178 dropped

counter.py.txt message.py.txt yap.py.txt

this may be related to https://github.com/bbcmicrobit/micropython/issues/755

microbit-carlos commented 1 year ago

If both micro:bits are transmitting on the same channel at the same time, then it's normal to get some collisions. I assume if you broadcast with only one micro:bit at a time there are not packet losses?

rhubarbdog commented 1 year ago

but surely not like ethernet. the radio should listen that the airwaves are quiet, acquire the airwave, transmit, releasing the airwave on completion

rhubarbdog commented 1 year ago

it's not just 2 microbit's communicating at the same time. I have this repo https://github.com/rhubarbdog/microbit-hands-up i have 2 pupil.py micobit's and one teacher.py. i press the button on the pupil microbits a second appart. When i send out the results from the teacher.py microbit the happy face isn't always shown

microbit-carlos commented 1 year ago

Right, but in your examples you can have two micro:bits transmitting data at exactly the same time and on the same channel, so the radio signals from each micro:bit can interfere with the other, and that can cause some data loss.

There is a few things you can try to reduce packet loses, for example, you could request acknowledgements and retransmit if an ack has not been received. There is a Networking With The micro:bit online book that contains useful information on this area: https://microbit.nominetresearch.uk/networking-book-online-python/retransmissions/retransmissions/

rhubarbdog commented 1 year ago

but i still loose packets in my second example https://github.com/rhubarbdog/microbit-hands-up and there are no transmission problems

rhubarbdog commented 1 year ago

also in the initial example counter.py counts message that are neither "yap." or "message." and receives none neither does radio.receive() throw an exception that a corrupt packet has been detected on the airwaves

martinwork commented 1 year ago

microbit-hands-up fails because the length of str(id) is 35 but the message received is only 29 long. radio.config(length=40) makes it work.

rhubarbdog commented 1 year ago

On the microbit's i have the string str(machine.unique_id()) must be less than 32 long because i have had both success and failure with 2 microbit's running pupil.py and one running teacher.py. with both successful runs where all pupils get a happy face in the correct order and other times only some get a happy face

microbit-carlos commented 1 year ago

Thanks for the additional info @rhubarbdog. Would you be able to provide a minimum example where not more than one micro:bit is transmitting at the same time and a receiver still loses packets?

rhubarbdog commented 6 months ago

transmitting with program ... on one microbit

from microbit import *
import radio

radio.config(data_rate = radio.RATE_2MBIT)
radio.on()

while True:
    if button_a.is_pressed():
        for _ in range(100):
            radio.send('message')
        while button_a.is_pressed():
            pass

    sleep(1)

and receiving on another microbit with program

from microbit import *
import radio

radio.config(length = 10, queue = 100, data_rate = radio.RATE_2MBIT)
radio.on()

count = 0 
while True:
    receive = radio.receive()
    if receive is None:
        sleep(1)
    else:
        count = count + 1

    if button_a.is_pressed():
        display.scroll(str(count))

before i added the radio.config argument queue = 100 packets were being dropped at data_rate 1 MBit when i change it to 2 Mbit most packets sometimes all are being dropped

martinwork commented 6 months ago

Should "length = 10" be included in both configs?