Giter VIP home page Giter VIP logo

rpi-bluetooth-arduino's Introduction

Basic connection between arduino and raspberry pi through Bluetooth

the-setup

The hardware

  • Raspberry pi 3b+ (it has inbuilt Bluetooth)
  • Arduino micro
  • HC-05 Bluetooth module

System requirements

  • arduino-cli
  • picocom
  • python 3

Configurations for Bluetooth module HC-05

  • Configure the module as master
  • Set PIN

Configurations for Raspberry Pi

Configurations for arduino

  • Install avr boards
  • Make physical connections
  • Copy arduino code and upload to arduino board

HC-05 Setup

First connect Bluetooth module to USB serial and press EN button, then send AT commands. at-commands Test AT commands in serial port from USB to serial converter. The default baud rate for AT commands is 38400

$ echo -e "AT\r" | picocom -b 38400 -qrx 1000 /dev/ttyUSB0
OK

Set with AT commands

$ echo -e "AT+NAME=ARDUINOBT\r" | picocom -b 38400 -qrx 1000 /dev/ttyUSB0
OK

Set password

$ echo -e "AT+PSWD=1379\r" | picocom -b 38400 -qrx 1000 /dev/ttyUSB0
OK

Set default baud rate

$ echo -e "AT+UART=115200,1,0\r" | picocom -b 38400 -qrx 1000 /dev/ttyUSB0
OK

Disconnect and test settings

Raspberry Pi Setup

Once installed raspbian and make the basic configurations, install Bluetooth required packages.

$ sudo apt install -y pi-bluetooth bluetooth bluez picocom blueman python3-pip

First edit Bluetooth service

$ sudo vi /etc/systemd/system/dbus-org.bluez.service

Add the following lines

  ExecStart=/usr/lib/bluetooth/bluetoothd -C
  ExecStartPost=/usr/bin/sdptool add SP

Reload systemd units

$ sudo systemctl daemon-reload

Enable Bluetooth service

$ sudo systemctl enable --now bluetooth

Edit the next file to load rfcomm automatically

$ sudo vi /etc/modules-load.d/modules.conf

Add the following line

rfcomm

Reboot the system

Pair with HC-05 module

Access to Bluetooth console

$ sudo bluetoothctl 

Pair with Bluetooth module

[bluetooth]# agent on
[bluetooth]# scan on
  Discovery started
  [NEW] Device 98:D3:31:50:4A:C1 98-D3-31-50-4A-C1
  [CHG] Device 98:D3:31:50:4A:C1 LegacyPairing: no
  [CHG] Device 98:D3:31:50:4A:C1 Name: ARDUINOBT
  [CHG] Device 98:D3:31:50:4A:C1 Alias: ARDUINOBT

[bluetooth]# scan off
  [CHG] Controller B8:27:EB:80:2D:06 Discovering: no
  Discovery stopped

[bluetooth]# pair 98:D3:31:50:4A:C1
  Attempting to pair with 98:D3:31:50:4A:C1
  [CHG] Device 98:D3:31:50:4A:C1 Connected: yes
  Request PIN code
  [agent] Enter PIN code: 1379
  [CHG] Device 98:D3:31:50:4A:C1 UUIDs: 00001101-0000-1000-8000-00805f9b34fb
  [CHG] Device 98:D3:31:50:4A:C1 ServicesResolved: yes
  [CHG] Device 98:D3:31:50:4A:C1 Paired: yes
  Pairing successful
  [CHG] Device 98:D3:31:50:4A:C1 ServicesResolved: no
  [CHG] Device 98:D3:31:50:4A:C1 Connected: no

[bluetooth]# trust 98:D3:31:50:4A:C1
  [CHG] Device 98:D3:31:50:4A:C1 Trusted: yes
  Changing 98:D3:31:50:4A:C1 trust succeeded

[bluetooth]# exit

Create a serial device

$ sudo rfcomm bind rfcomm0 <device's MAC>

If everything works, connect with picocom and test communication in both sides check-connection

Install pyserial library for python3

$ sudo pip3 install pyserial

Copy pythonClient dir to raspberry pi and execute client.py script

$ python pythonClient/client.py

Arduino Setup

Physical connections

Connections for HC-05 module

  • GND -> GND Arduino
  • VCC -> VCC Arduino
  • RX -> D14 Arduino
  • TX -> D15 Arduino
  • EN -> N/C

Compile and upload the sketch

Upload the code to arduino board, considering serial port of arduino micro is /dev/ttyACM0

$ arduino-cli compile -b arduino:avr:micro -u -p /dev/ttyACM0 ~/WorkSpace/tutorials/rpi-arduino-bluetooth/arduinoBT-HC05

Test Communications

test-arduino-connection

Raspberry pi output example

developer@raspberrypi:~ $ python pythonClient/client.py
  Enter your message below.
  Insert "exit" to leave the application.
  You message >> hi from raspberry pi
  Server Response >> hi
  
  You message >> test connection
  Server Response >> test connection from arduino
  
  You message >> exit

Arduino Output example

$ picocom -b 115200 /dev/ttyACM0
  Terminal ready
  Server response: hi from raspberry pi
  You response -> hi
  Server response: test connection
  You response -> test connection from arduino
  
  Terminating...
  Skipping tty reset...
  Thanks for using picocom

rpi-bluetooth-arduino's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

rpi-bluetooth-arduino's Issues

Sudo rfcomm bind, missing dev parameter

Readme.md

When creating a serial device, sudo rfcomm bind xx:xx:xx:xx:xx:xx, says missing dev parameter.
You need to run sudo rfcomm bind /dev/rfcom0 xx:xx:xx:xx:xx:xx instead.
Of course replace rfcom0 if that address isn't free.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.