Help with radio feature

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
mathiaspsyko
Posts: 1
Joined: Tue Sep 01, 2020 3:00 pm

Help with radio feature

Post by mathiaspsyko » Tue Sep 01, 2020 3:03 pm

Lets say i wanted to send something over to my friends micro:bit but i dont know his radio group, how would i make a system that tries radio group 1-255 until it send to the right one?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Help with radio feature

Post by jimmo » Wed Sep 02, 2020 4:50 am

The simplest way to do this is to just try each one individually. If you want to find out which is the correct one then you'll need to have the other one reply. Here's a simple example:

Code: Select all

from microbit import *
import radio

CHANNEL = 27

def scan_groups():
  for group in range(256):
    # Send a ping on the current group
    radio.config(channel=CHANNEL, group=group)
    radio.send('ping')
    
    # Wait 100ms for a reply
    t = running_time()
    while running_time() - t < 100:
      reply = radio.receive()
      if reply == 'pong':
        print('Reply on group', group)
        return group
  return -1
The problem is that you might not also know which channel and address the other device is using?

Post Reply