BBC MicroBit radio send/receive - data packet is not a string?

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
joolsbamford
Posts: 4
Joined: Mon Oct 10, 2016 5:46 am

BBC MicroBit radio send/receive - data packet is not a string?

Postby joolsbamford » Mon Oct 10, 2016 11:41 pm

Evening all,

I am trying to get a seemingly simple remote control car project to work with 2 Microbits but keep getting the same error message. The controller code measures which way the controller is being tilted and sends out a f,b,l or r message to the motor board. The controller code seems to work fine.

The driver microbit always returns an error for the radio.receive() line - data packet received is not a string. This happens as soon as I tilt the controller. I have played around with the code and looked at similar examples online but cannot see where the problem is.

I use Mu which I downloaded a few weeks ago and it has worked fine. I just don't know where I am going wrong here and would like to have it ready to show the kids at this week's code club.

I have commented out the motor board outputs and am asking the driver microbit to just display images until i work this out.

Thanks in advance for any help. Let me know if you need any more info.

____Controller code____

from microbit import *
import radio
radio.on()

while True:

readingy = accelerometer.get_y()
readingx = accelerometer.get_x()

if readingy > 550:
display.show(Image.ARROW_S)
radio.send('b')

elif readingy < -550:

display.show(Image.ARROW_N)
radio.send('f')

elif readingx < -550:
display.show(Image.ARROW_W)
radio.send('l')

elif readingx > 550:
display.show(Image.ARROW_E)
radio.send('r')

else:
display.show("-")


____Driver code____

from microbit import *
import radio
radio.on()

while True:

msgin = radio.receive() ## error always reported on this line

if msgin == 'f':
#pin12.write_digital(1)
#pin16.write_digital(1)
display.show(Image.ARROW_N)

elif msgin == 'b':
#pin8.write_digital(1)
#pin0.write_digital(1)
display.show(Image.ARROW_S)

elif msgin == 'l':
#pin12.write_digital(1)
#pin0.write_digital(1)
display.show(Image.ARROW_W)

elif msgin == 'r':
#pin8.write_digital(1)
#pin16.write_digital(1)
display.show(Image.ARROW_E)

else:
#pin12.write_digital(0)
#pin16.write_digital(0)
#pin8.write_digital(0)
#pin0.write_digital(0)
display.show("-")

User avatar
deshipu
Posts: 882
Joined: Thu May 28, 2015 5:54 pm

Re: BBC MicroBit radio send/receive - data packet is not a string?

Postby deshipu » Tue Oct 11, 2016 8:02 am

That looks like you found a bug. What happens when you send longer strings than a single character?

joolsbamford
Posts: 4
Joined: Mon Oct 10, 2016 5:46 am

Re: BBC MicroBit radio send/receive - data packet is not a string?

Postby joolsbamford » Tue Oct 11, 2016 7:35 pm

Thanks for the reply. Always get the same message. I started using forward, backwards, left and right. When this didn't work I Shortened it as shown in code. Also tried numbers. Also tried Send_bytes and receive_bytes. Same error msg every time.

User avatar
deshipu
Posts: 882
Joined: Thu May 28, 2015 5:54 pm

Re: BBC MicroBit radio send/receive - data packet is not a string?

Postby deshipu » Tue Oct 11, 2016 9:48 pm

I dug out the second micro:bit and tested your code, and I can reproduce your problem in 100%. It seems that the sending program is the problem, I'm investigating further.

User avatar
deshipu
Posts: 882
Joined: Thu May 28, 2015 5:54 pm

Re: BBC MicroBit radio send/receive - data packet is not a string?

Postby deshipu » Tue Oct 11, 2016 9:52 pm

Seems like the problem is sending the strings very fast in the while loop without any pause.

If I add a sleep(1) after every radio.send(), the program works as intended.

User avatar
deshipu
Posts: 882
Joined: Thu May 28, 2015 5:54 pm

Re: BBC MicroBit radio send/receive - data packet is not a string?

Postby deshipu » Tue Oct 11, 2016 10:01 pm


joolsbamford
Posts: 4
Joined: Mon Oct 10, 2016 5:46 am

Re: BBC MicroBit radio send/receive - data packet is not a string?

Postby joolsbamford » Wed Oct 12, 2016 5:59 am

Good work. Thank you.

I will try that when I get back from work tonight. I think I tried some sleep commands in the driver code but it didn't solve the problem. I didn't think to add them to the sending code as you suggest.

It will be really great to get this working for the kids on Thursday.

Thanks again!

joolsbamford
Posts: 4
Joined: Mon Oct 10, 2016 5:46 am

Re: BBC MicroBit radio send/receive - data packet is not a string?

Postby joolsbamford » Thu Oct 13, 2016 5:58 am

I had a play with it and got it working just right for me by adding sleep(50) to both the controller and driver code. I found I had to add a sleep to both codes to get the desired result. :D

Thanks for all your help.

_____CONTROLLER_____

from microbit import *
import radio
radio.on()

while True:

readingy = accelerometer.get_y()
readingx = accelerometer.get_x()

if readingy > 550:
display.show(Image.ARROW_S)
radio.send('b')
sleep(50)

elif readingy < -550:

display.show(Image.ARROW_N)
radio.send('f')
sleep(50)

elif readingx < -550:
display.show(Image.ARROW_W)
radio.send('l')
sleep(50)

elif readingx > 550:
display.show(Image.ARROW_E)
radio.send('r')
sleep(50)

else:
display.show("-")



_____DRIVER_____

from microbit import *
import radio
radio.on()

while True:

msgin = radio.receive()

if msgin == 'f':
pin12.write_digital(1)
pin16.write_digital(1)
display.show(Image.ARROW_N)
sleep(50)

elif msgin == 'b':
pin8.write_digital(1)
pin0.write_digital(1)
display.show(Image.ARROW_S)
sleep(50)

elif msgin == 'l':
pin12.write_digital(1)
pin0.write_digital(1)
display.show(Image.ARROW_W)
sleep(50)

elif msgin == 'r':
pin8.write_digital(1)
pin16.write_digital(1)
display.show(Image.ARROW_E)
sleep(50)

else:
pin12.write_digital(0)
pin16.write_digital(0)
pin8.write_digital(0)
pin0.write_digital(0)
display.show("-")

User avatar
deshipu
Posts: 882
Joined: Thu May 28, 2015 5:54 pm

Re: BBC MicroBit radio send/receive - data packet is not a string?

Postby deshipu » Thu Oct 13, 2016 6:52 am

Another solution would be to just wrap the receive in a try-except and catch and ignore the exception.

User avatar
titimoby
Posts: 10
Joined: Thu Sep 29, 2016 6:54 am

Re: BBC MicroBit radio send/receive - data packet is not a string?

Postby titimoby » Tue Oct 18, 2016 8:23 pm

This code looks promising.
I tried it but nothing seems to be received.
Is there a way to have some debug details?

One thing I wonder is: do I need to specify the radio channel in each board?
Like adding radio.config(channel=7) in each code to be sure?

Edit: that's it
I had to precise the radio.config(channel=7) on both code.
To be able to see the change on the receiver, I had to add a sleep(100) after each display.show

Thanks a million, I have something to add for the most advanced kids in my workshop on Sunday!


Return to “The micro:bit”

Who is online

Users browsing this forum: No registered users and 1 guest