title: Raspberry Pi and ESP32 communication over bluetooth
body: <p>This is my first time trying to establish a connection between a <strong>Raspberry Pi Zero</strong> and an <strong>ESP32</strong> over <strong>bluetooth</strong> and I can't get it work. The goal is to just exchange simple text (not more, not less)</p>
  <h1>My Setup</h1>
  <p>I have two different platforms (the ESP32 and the Raspberry Pi Zero) and I want them to communicate over Bluetooth using <code>RFCOMM</code>.</p>
  <h2>Raspberry Pi</h2>
  <p>On my Raspberry Pi, an RFCOMM Server (a slightly adapted version of this code from <a href="https://stackoverflow.com/questions/36696535/communicating-via-bluetooth-serial-with-python">here</a>, which apparently has been taken from <a href="http://blog.davidvassallo.me/2014/05/11/android-linux-raspberry-pi-bluetooth-communication/" rel="nofollow noreferrer">this site</a>) is running.</p>
  <pre class="lang-py prettyprint-override"><code>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((&quot;&quot;,PORT_ANY))
  server_sock.listen(1)
  
  port = server_sock.getsockname()[1]
  
  uuid = &quot;94f39d29-7d6d-437d-973b-fba39e49d4ee&quot;
  
  advertise_service( server_sock, &quot;AquaPiServer&quot;,
                     service_id = uuid,
                     service_classes = [ uuid, SERIAL_PORT_CLASS ],
                     profiles = [ SERIAL_PORT_PROFILE ], 
  #                  protocols = [ OBEX_UUID ] 
                      )
  while True:          
      print(&quot;Waiting for connection on RFCOMM channel %d&quot; % port)
  
      client_sock, client_info = server_sock.accept()
      print(&quot;Accepted connection from &quot;, client_info)
  
      try:
          data = client_sock.recv(1024)
          if len(data) == 0: break
          print(&quot;received [%s]&quot; % data)
          data = 'Hello!' 
          client_sock.send(data)
          print(&quot;sending [%s]&quot; % data)
  
      except IOError:
          pass
  
      except KeyboardInterrupt:
          print(&quot;disconnected&quot;)
          client_sock.close()
          server_sock.close()
          print(&quot;all done&quot;)
          break
  </code></pre>
  <h2>ESP32</h2>
  <p>On my ESP32 a very simple example application is running. This example has been taken from <a href="https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/" rel="nofollow noreferrer">here</a>.</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &quot;BluetoothSerial.h&quot;
  
  #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(&quot;ESP32test&quot;); //Bluetooth device name
    Serial.println(&quot;The device started, now you can pair it with bluetooth!&quot;);
  }
  
  void loop() {
    if (Serial.available()) {
      SerialBT.write(Serial.read());
    }
    if (SerialBT.available()) {
      Serial.write(SerialBT.read());
    }
    delay(20);
  }
  </code></pre>
  <h2>Connection</h2>
  <p>I'm attempting to connect my ESP32 via <code>bluetoothctl</code>.</p>
  <pre class="lang-sh prettyprint-override"><code>[bluetooth]# pair 24:62:AB:**:**:**
  </code></pre>
  <p>This seems to work at first, but I receive a <em>Connected: no</em> after some seconds (last line).</p>
  <pre class="lang-sh prettyprint-override"><code>[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
  </code></pre>
  <p><em>Anyhow, I see my paired device:</em></p>
  <pre class="lang-sh prettyprint-override"><code>[bluetooth]# paired-devices
  Device 24:62:AB:**:**:** ESP32test
  </code></pre>
  <h1>Issues and my experience so far</h1>
  <p>--- BUT my server on the RPi Zero does not detect the new device:</p>
  <pre><code>sudo python3 BluetoothServer.py 
  Waiting for connection on RFCOMM channel 1
  </code></pre>
  <p>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?</p>
  <h2>What works?</h2>
  <p>If I'm connection to the RPi using a Bluetooth Terminal for Android like <a href="https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal&amp;hl=de_AT" rel="nofollow noreferrer">Serial Bluetooth Terminal</a> my server seems to be working correctly. I can send and receive commands over Bluetooth. This software is also available as <a href="https://github.com/kai-morich/SimpleBluetoothTerminal" rel="nofollow noreferrer">Open Source</a> but I'm not that familiar with Java and have a hard time understanding what going on here. <strong>This seems to work as well if I'm connecting to my ESP32...</strong> ...but not between my to devices.</p>
  <h2>What did I try</h2>
  <ul>
  <li>Tried different code snipptes, including <a href="https://github.com/espressif/arduino-esp32/blob/master/libraries/BLE/examples/BLE_client/BLE_client.ino" rel="nofollow noreferrer">BLE_client.ino</a>, but apparently BLE does not support RFCOMM according to <a href="https://stackoverflow.com/questions/50503701/reading-data-from-a-bluetooth-device-low-energy-rfcomm-python3">this stack-overflow post</a></li>
  <li>Tried connecting it by hand (so not using my server) similar to <a href="https://unix.stackexchange.com/questions/92255/how-do-i-connect-and-send-data-to-a-bluetooth-serial-port-on-linux">this stackexchange post</a></li>
  <li>Played around with different server settings..</li>
  <li>I'm not having a clue what I'm doing wrong....</li>
  </ul>