BBC MicroBit radio send/receive - data packet is not a string?
-
- Posts: 4
- Joined: Mon Oct 10, 2016 5:46 am
BBC MicroBit radio send/receive - data packet is not a string?
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("-")
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("-")
Re: BBC MicroBit radio send/receive - data packet is not a string?
That looks like you found a bug. What happens when you send longer strings than a single character?
-
- Posts: 4
- Joined: Mon Oct 10, 2016 5:46 am
Re: BBC MicroBit radio send/receive - data packet is not a string?
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.
Re: BBC MicroBit radio send/receive - data packet is not a string?
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.
Re: BBC MicroBit radio send/receive - data packet is not a string?
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.
If I add a sleep(1) after every radio.send(), the program works as intended.
-
- Posts: 4
- Joined: Mon Oct 10, 2016 5:46 am
Re: BBC MicroBit radio send/receive - data packet is not a string?
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!
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!
-
- Posts: 4
- Joined: Mon Oct 10, 2016 5:46 am
Re: BBC MicroBit radio send/receive - data packet is not a string?
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.
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("-")
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
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("-")
Re: BBC MicroBit radio send/receive - data packet is not a string?
Another solution would be to just wrap the receive in a try-except and catch and ignore the exception.
Re: BBC MicroBit radio send/receive - data packet is not a string?
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!
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!