This is my first time trying to establish a connection between a Raspberry Pi Zero and an ESP32 over bluetooth and I can't get it work. The goal is to just exchange simple text (not more, not less)
My Setup
I have two different platforms (the ESP32 and the Raspberry Pi Zero) and I want them to communicate over Bluetooth using RFCOMM
.
Raspberry Pi
On my Raspberry Pi, an RFCOMM Server (a slightly adapted version of this code from here, which apparently has been taken from this site) is running.
import os
import glob
import time
import RPi.GPIO as GPIO
from bluetooth import *
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
advertise_service( server_sock, "AquaPiServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
# protocols = [ OBEX_UUID ]
)
while True:
print("Waiting for connection on RFCOMM channel %d" % port)
client_sock, client_info = server_sock.accept()
print("Accepted connection from ", client_info)
try:
data = client_sock.recv(1024)
if len(data) == 0: break
print("received [%s]" % data)
data = 'Hello!'
client_sock.send(data)
print("sending [%s]" % data)
except IOError:
pass
except KeyboardInterrupt:
print("disconnected")
client_sock.close()
server_sock.close()
print("all done")
break
ESP32
On my ESP32 a very simple example application is running. This example has been taken from here.
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
Connection
I'm attempting to connect my ESP32 via bluetoothctl
.
[bluetooth]# pair 24:62:AB:**:**:**
This seems to work at first, but I receive a Connected: no after some seconds (last line).
[bluetooth]# pair 24:62:AB:**:**:**
Attempting to pair with 24:62:AB:**:**:**
[CHG] Device 24:62:AB:**:**:** Connected: yes
Request confirmation
[agent] Confirm passkey 101977 (yes/no): yes
[CHG] Device 24:62:AB:**:**:** UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device 24:62:AB:**:**:** UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Device 24:62:AB:**:**:** UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device 24:62:AB:**:**:** ServicesResolved: yes
[CHG] Device 24:62:AB:**:**:** Paired: yes
Pairing successful
[CHG] Device 24:62:AB:**:**:** ServicesResolved: no
[CHG] Device 24:62:AB:**:**:** Connected: no
Anyhow, I see my paired device:
[bluetooth]# paired-devices
Device 24:62:AB:**:**:** ESP32test
Issues and my experience so far
--- BUT my server on the RPi Zero does not detect the new device:
sudo python3 BluetoothServer.py
Waiting for connection on RFCOMM channel 1
I was expecting that the server recognises my device but that's not the case. I guess my ESP32 application is not recognized as a client? How to a make the server be aware of my ESP32 device, so I can communicate over bleutooth?
What works?
If I'm connection to the RPi using a Bluetooth Terminal for Android like Serial Bluetooth Terminal my server seems to be working correctly. I can send and receive commands over Bluetooth. This software is also available as Open Source but I'm not that familiar with Java and have a hard time understanding what going on here. This seems to work as well if I'm connecting to my ESP32... ...but not between my to devices.
What did I try
- Tried different code snipptes, including BLE_client.ino, but apparently BLE does not support RFCOMM according to this stack-overflow post
- Tried connecting it by hand (so not using my server) similar to this stackexchange post
- Played around with different server settings..
- I'm not having a clue what I'm doing wrong....