Page 1 of 1

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

Posted: Sun May 12, 2019 6:11 am
by csanyipal
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?

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

Posted: Sun May 12, 2019 7:42 am
by jimmo
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.

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

Posted: Sun May 12, 2019 7:45 am
by jimmo
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

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

Posted: Sun May 12, 2019 7:58 am
by lujo
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)

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

Posted: Sun May 12, 2019 8:21 am
by csanyipal
All right!
Then I won't change the code.

Thank you very much!

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

Posted: Thu May 16, 2019 3:13 pm
by rhubarbdog
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.