Connect Raspberry Pi 3B with App inventor app

125 views
Skip to first unread message

Svein-Tore Narvestad

unread,
Sep 3, 2019, 2:45:56 PM9/3/19
to MIT App Inventor Forum
HI
  
I have the latest days been working on an example app to send messages from an app inventor app to raspberry PI:


But with no luck.  When I try to connect I get error 507:unable to connect, is the device turned on? (for sure it is!!!!)

Have someone else tested this example and get it work? I just wonder, I have RPi 3B, which means the RPI has BT 4, should I for that reason use BLE extension instead?   

Any suggestions?  Have somenone here fixed BT with RPI 3B and app inventor?  Is App Inventor's IOT compatible with raspberry PI? 

Svein-Tore

ABG

unread,
Sep 3, 2019, 4:51:54 PM9/3/19
to MIT App Inventor Forum
The IF block at http://www.digitalpolymath.co.uk/wp-content/uploads/sites/7/2017/03/600x338xappinventor-bluetooth-client.jpg.pagespeed.ic.a1E2-sjfGH.webp
is obscured by its tool tip.

Do a Download Blocks Image and post it here
so we can verify the IF block is right?

ABG


ABG

unread,
Sep 3, 2019, 4:54:20 PM9/3/19
to MIT App Inventor Forum

Svein-Tore Narvestad

unread,
Sep 3, 2019, 5:23:37 PM9/3/19
to MIT App Inventor Forum
Hi

The block hided by the tooltip is this (as I guess you know by now):

block_connect.JPG


BTW: Searching around the internet, one have to use Raspbian Jessie, I'm using Buster.  Don't know why though.  I'll give it a try tomorrow.

Thanks!!

Svein-Tore Narvestad

unread,
Sep 4, 2019, 8:09:06 AM9/4/19
to MIT App Inventor Forum
OK so I have tested a bit more.

I have downloaded an  app called blueterm.  With this app running and the py script running on the PI  I am able to connect, and the script I have created, prints what I'm typing on the phone in blueterm.  Trying to do the same in the app inventor app gives error 507, unable to connect....

Any suggestions?

ABG

unread,
Sep 4, 2019, 11:30:19 AM9/4/19
to MIT App Inventor Forum
I haven't seen your blocks, only the original tutorial,
so that limits my ability to help you.

If you use the board's Search for messages box for '507', you might find
solutions .

ABG

Chris Ward

unread,
Sep 4, 2019, 12:48:06 PM9/4/19
to MIT App Inventor Forum
Hello Svein-Tore

Have you applied all of these instructions for BT by digititalpolymath?


Svein-Tore Narvestad

unread,
Sep 4, 2019, 12:59:27 PM9/4/19
to MIT App Inventor Forum
Hi Chris!

I 've looked at this one: 


also from digitalpolymath, But I think I will look at your suggestions, maybe something I have forgot in the start.

Tank you!

Svein-Tore

Chris Ward

unread,
Sep 4, 2019, 1:10:37 PM9/4/19
to MIT App Inventor Forum
Hi Svein-Tore

Isn't your Pi using BT 4.2? If so, then you should use BLE via the MIT BLE extension:

Svein-Tore Narvestad

unread,
Sep 4, 2019, 1:32:48 PM9/4/19
to MIT App Inventor Forum
Yes, I know, and I have tried that also, with no luck.......   What I find very strange is that it work for the blueterm app.  I think this means there is nothing wrong with the python script.  So I just wonder, will it work if I change to BLE on the App Inventor part and use the same on the "PI side"?  Let me look at your link. 

Thanks a lot Chris!


Svein-Tore   

Svein-Tore Narvestad

unread,
Sep 4, 2019, 2:28:03 PM9/4/19
to MIT App Inventor Forum
Hi

For sure its best to have the code, here is a screenshot, and the .aia is attached as well:

screenshot_code.JPG


I just now  did a test. I tried to use this app to connect a lego Minstroms NXT, and it connected at once, strange....... 


Svein-Tore




bluetooth (1).aia

ABG

unread,
Sep 4, 2019, 3:51:00 PM9/4/19
to MIT App Inventor Forum
I see in your blocks you trim the address to 17 bytes,
and the original sample uses the raw address.

For a different device with a short address, the trim would make no difference.

So how many bytes in the address you can't connect to?
(and why do trim to 17 bytes?)

ABG

Svein-Tore Narvestad

unread,
Sep 4, 2019, 4:59:20 PM9/4/19
to MIT App Inventor Forum
Well, the block bluetoothclient.AdresssAndNames gives you the MAC adress + the name.  The connect block ask for the adress, and the length of a mac adress i 17. I have also tried without, and then  I get error 504 no such device adress, I think it was something like that.  And: Since it connects fine to the Lego mindstorms NXT this must be correct. BTW:   I think Chris got the point.  The reason why  I can connect to the NXT is that it is old (started back in 2006), not much BLE at that time.   The blueterm app i've downloaded uses   BLE, so I'm pretty sure I must use the BLE extension.    The next problem then will be the uuid's.

Svein-Tore

Svein-Tore Narvestad

unread,
Sep 5, 2019, 4:24:39 AM9/5/19
to MIT App Inventor Forum
OK So  i have done this tutorial:



I select scan but the rpi won't appear.  The rpi is running my python script, and it work perfect for the blueterm app though, strange to me......


Svein-Tore

Ghica

unread,
Sep 5, 2019, 8:06:56 AM9/5/19
to MIT App Inventor Forum
One reason for it not to work is that the tutorial points to the old BLE extension. Use this one instead:
Cheers, Ghica.

Juan Antonio

unread,
Sep 5, 2019, 11:50:30 AM9/5/19
to MIT App Inventor Forum
Hi, trying to make Bluetooth work ...

- Testing on Android 6 and Android 9.

- Classic Bluetooth.

- "App Inventor" sends a continuous cycle of 5 bytes (as SendText).
- We need Mac Bluetooth of Raspberry: B8:27:EB:XX:XX:XX
- Clock timer: 1000

bluetoothraspberry_i.png





p9B3_enviar_recibir_RaspBerry.aia

Juan Antonio

unread,
Sep 5, 2019, 11:51:28 AM9/5/19
to mitappinv...@googlegroups.com
Now in Raspberry... 

- Raspberry Pi 3 B.

- Raspbian (Buster)

sudo apt-get update 
sudo apt-get install bluetooth
sudo apt-get install bluez
sudo apt-get install python-bluez 

service bluetooth start
service bluetooth status

sudo bluetoothctl 
[bluetooth]# power on 
[bluetooth]# agent on
[bluetooth]# discoverable on 
[bluetooth]# pairable on 
[bluetooth]# scan on 
[bluetooth]# pair MAC BLUETOOTH MOBILE

bluetoothraspberry_i2.png



- Now in Raspberry, copy this code. Python.

I created a file called bluetooth4.py with this code:

import bluetooth
import RPi.GPIO as GPIO        #calling for header file which helps in using GPIOs of PI
LED=21

GPIO.setmode(GPIO.BCM)     #programming the GPIO by BCM pin numbers. (like PIN40 as GPIO21)
GPIO.setwarnings(False)
GPIO.setup(LED,GPIO.OUT)  #initialize GPIO21 (LED) as an output Pin
GPIO.output(LED,0)
 
server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
(..............)

I didn't use the GPIOs or the LED

python2 bluetooth4.py
(with pyton3 it didn't work for me)

bluetoothraspberry_i3.png


Svein-Tore Narvestad

unread,
Sep 6, 2019, 9:59:12 AM9/6/19
to MIT App Inventor Forum
Hi Juan!

After going through what you have done I see it is pretty much the same as I have done (I have also looked and tested  the web site you have attached as link, circuitdigest.com).  The only difference I can see is that you have used python2, I have used python3, and, as you said, It won't work. I'm not able to get the python 2 idle, though, guess I have to install it. But will look into it.


NICE WORK!!!   Thanks A LOT Juan:)!

Svein-Tore Narvestad

unread,
Sep 7, 2019, 8:47:31 AM9/7/19
to MIT App Inventor Forum
Hi Juan!

So I have downloaded your .aia file and  I have installed python 2.7.  But I can't get it work. I get the message error 515: not connected to a Bluetooth device.
I get this:

============== RESTART: /home/pi/Documents/python/juan_file.py ==============
Accepted connection from  ('**:**:**:**:**:*', 1)
Received: r
Received: h
Received: h
Received: r
Received: h
Received: 4
Received: g
Received: e
Received: g
Received: h
Received: h
Received: r
Received: 8

So what is wrong? Any suggestion?


Svein-Tore  

Svein-Tore Narvestad

unread,
Sep 7, 2019, 9:10:25 AM9/7/19
to MIT App Inventor Forum
OK So I have discovered something new.  I start the py script, I open the terminal and write sudo bluetothctl:

pi@raspberrypi:~ $ sudo bluetoothctl
Agent registered


I then run the app, I press Inicio - Start, and then I get:

[CHG] Device **:**:**:**:** Connected: yes
About 3-4 seconds later I get:'
[CHG] Device **:**:**:**:** Connected: no

After pressing the button inicio Start several times:

pi@raspberrypi:~ $ sudo bluetoothctl
Agent registered
[CHG] Device **:**:**:**:** Connected: yes
[CHG] Device **:**:**:**:** Connected: no
[CHG] Device **:**:**:**:** Connected: yes
[CHG] Device **:**:**:**:** Connected: no
[CHG] Device **:**:**:**:** Connected: yes
[CHG] Device **:**:**:**:** Connected: no
[CHG] Device **:**:**:**:** Connected: yes
[CHG] Device **:**:**:**:** Connected: no
[CHG] Device **:**:**:**:** Connected: yes
[CHG] Device **:**:**:**:** Connected: no
[CHG] Device **:**:**:**:** Connected: yes
[CHG] Device **:**:**:**:** Connected: no
[bluetooth]# 


So there is  a kind of connection here, but why is it broken after 3-4 secs?

Svein-Tore

Juan Antonio

unread,
Sep 7, 2019, 10:50:03 AM9/7/19
to MIT App Inventor Forum
Maybe it's the Channel (port)

sudo sdptool add --channel=22 SP

In bluetooth4.py 
port=22
bluetooth4.py

import bluetooth 
server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM ) 
port = 22
server_sock.bind(("",port)) 
server_sock.listen(1) 
client_sock,address = server_sock.accept() 
print ("Conexion realizada con: ", address) 
while True: 
    recvdata = client_sock.recv(1024) 
    print ("Informacion recibida: %s" % recvdata)
    if (recvdata == "Q"): 
        print ("Finalizado.") 
        break
         
client_sock.close() 
server_sock.close()



Here is the process in Spanish: http://kio4.com/raspberry/31_bluetooth.htm


I've also put its operation to Python3
https://stackoverflow.com/questions/23985163/python3-error-no-module-named-bluetooth-on-linux-mint

Svein-Tore Narvestad

unread,
Sep 7, 2019, 12:17:35 PM9/7/19
to MIT App Inventor Forum
YESS!!


Now it works!!!!  THANKS!!!! Juan!

Best Regards, Svein-Tore


Reply all
Reply to author
Forward
0 new messages