How to send with radio a numerical value of a variable? [SOLVED]

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
csanyipal
Posts: 6
Joined: Sat May 11, 2019 10:43 am
Location: Serbia
Contact:

How to send with radio a numerical value of a variable? [SOLVED]

Post by csanyipal » Sun May 12, 2019 6:11 am

Hello,

I have two micro:bit's: 'mb1' and 'mb2'.
  • mb2 count steps and measures time.
  • mb1 starts these processes on mb2 by sending a radio signal as a string, and after ten seconds mb1 receives a radio signal from mb2, which contains a numerical value ( the counted steps ) which is an integer type. After the value was received, mb1 is doing some mathematical operations with that number of steps and other constants.
So how can one send a numerical value of a variable through radio on micro:bit?

I am currently using this code to achieve this goal:
on mb2:

Code: Select all

radio.send(str(step))
on mb1:

Code: Select all

while True:
    receivedSignal = radio.receive()
    if receivedSignal is not None:
        break
    else:
        display.show(anotherImage)

steps = int(receivedSignal) * 2
but maybe there is another way to send numerical value but not as string out there?
Maybe the 'radio.send_bytes(message)' and 'radio.receive_bytes()' are what I want?
If yes, please give me an example how to use these functions to send a numerical value of a variable?
Last edited by csanyipal on Sun May 12, 2019 8:22 am, edited 1 time in total.
Best, Pal

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

Re: How to send with radio a numerical value of a variable?

Post by jimmo » Sun May 12, 2019 7:42 am

There are lots of ways but generally what you're doing with str and int is the easiest.

As you add more features (perhaps you'll have multiple step counters) you might want to come up with a scheme like "id:n:time" or something. You'll find split and join helpful here.

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

Re: How to send with radio a numerical value of a variable?

Post by jimmo » Sun May 12, 2019 7:45 am

Btw the way to use send_bytes would be with ustruct.pack but from memory I don't think ustruct is available on the microbit port. https://docs.micropython.org/en/latest/ ... truct.html

lujo
Posts: 24
Joined: Sat May 11, 2019 2:30 pm

Re: How to send with radio a numerical value of a variable?

Post by lujo » Sun May 12, 2019 7:58 am

The micro:bit makes it very easy to send and receive strings.

Converting from and back to bytes doesn't make it more understandable. You can also
do this with struct pack and unpack. But then it looks even more complicated.


from microbit import *
import radio

radio.config(channel=11,group=11, power=3)
radio.on()

display.show(Image.YES, delay=100, wait=False, loop=False, clear=True)

steps = 123
buf = steps.to_bytes(2, "little")

while True:
if button_a.was_pressed():
radio.send_bytes(buf)




from microbit import *
import radio

radio.config(channel=11,group=11, power=3)
radio.on()

display.show(Image.YES, delay=100, wait=False, loop=False, clear=True)

while True:
incoming = radio.receive_bytes()
if incoming:
num = int.from_bytes(incoming, "little")
display.show(num, delay=200, wait=False, loop=False, clear=True)

csanyipal
Posts: 6
Joined: Sat May 11, 2019 10:43 am
Location: Serbia
Contact:

Re: How to send with radio a numerical value of a variable?

Post by csanyipal » Sun May 12, 2019 8:21 am

All right!
Then I won't change the code.

Thank you very much!
Best, Pal

rhubarbdog
Posts: 168
Joined: Tue Nov 07, 2017 11:45 pm

Re: How to send with radio a numerical value of a variable? [SOLVED]

Post by rhubarbdog » Thu May 16, 2019 3:13 pm

ustruct is a available for microbit its just the documentation got lost from one version to the next. There's an outstanding issue on github to resolve this.

Post Reply