Bluetooth raspberry pi arduino

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Repository of code used for Arduino and Raspberry Pi

Josh-Weston/ArduinoRPi

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

HC05 Bluetooth Communication between Arduino Uno and Raspberry Pi 3

To create bluetooth connection, we need to do 3 things:

  1. Wire the Bluetooth Module to our Arduino and enter AT+Commands to ensure everything is configured properly
  2. Ensure bluetooth connectivity is available on the Raspberry Pi and successfully connect to the Bluetooth Module powered by the Arduino
  3. Create a script on the Raspberry Pi to listen to and respond to Bluetooth Serial communication.

NOTE: Watch this video for more information, and for an explanation for why I chose the HC-05 over the HM10 knock-off Video: Arduino and Raspberry Pi Communication

Disclaimer: A lot of trial-and-error went into establishing the connection, so these steps may not be 100% complete; however, they are materially accurate, and should provide enough guidance to troubleshoot issues you encounter.

You can find my Arduino Code and the fritzing wiring diagram here

Information about HC05 accessing AT+Commands on HC05:

  • We do not need the state pin
  • Do not connect the VCC. Hold the button on the module while uploading your sketch. Once uploaded, then plug in the VCC. You should be in AT mode.
  • Advertised as 9600 BAUD rate but really 38400 when using AT Mode this way
  • This is considered “mini AT Mode” and certain commands are not available.
  • To use all of the commands, you either need the Arduino to control the module, or you can hold the button in while sending a command (easier method)
  • HC-05 commands are different that BLE CC41a (HM10 knockoff). Here is a table of commands:
Читайте также:  Шкода рапид настроить блютуз

HC05 AT

Step 2: Setup bluetooth communication on Raspberry Pi

This part can be really easy or tricky depending on your setup. If you are using a Raspberry Pi 3, you will already have a graphical Bluetooth interface that can help with this step. There are 3 things to remember:

  1. You need to know the MAC Address of your Bluetooth Module
  2. You need to pair to the Bluetooth Module
  3. You need to open an RFCOMM channel so Raspberry Pi knows it should be communicating. It is not enough to simply pair the devices, you need an RFCOMM channel open.

If you are using the graphical interface, you should be able to find your device and locate its unique MAC address. It will look something like: 98:D3:32:20:82:9D

If you do not have the graphical interface, or are having problems you can follow these steps:

  • Type sudo bluetoothctl into a terminal and input the admin password if prompted
  • Type agent on and press enter. Then type default-agent and press enter.
  • Type scan on and the raspberry pi will scan for broadcasting Bluetooth devices nearby.
  • Type pair 98:D3:32:20:82:9D replacing the MAC Address with that of your bluetooth module
  • Type info 98:D3:32:20:82:9D to see information about the device.

Once you have successfully paired the bluetooth module to your Raspberry Pi, you will need to bind to an RFCOMM channel before they can communicate:

  1. sudo rfcomm bind hci0 98:D3:32:20:82:9D 1 will bind the bluetooth device to the RPi bluetooth module
  2. sudo rfcomm release 0 will release the bluetooth module from rfcomm0

Step 3: Setup Raspberry Pi to listen and respond to Bluetooth messages

The Raspberry Pi code I used can be found in this here.

Источник

HC-05 Setup

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 
$ echo -e "AT+NAME=ARDUINOBT\r" | picocom -b 38400 -qrx 1000 /dev/ttyUSB0 OK 
$ echo -e "AT+PSWD=1379\r" | picocom -b 38400 -qrx 1000 /dev/ttyUSB0 OK 
$ 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 in /etc/systemd/system/dbus-org.bluez.service
Add the following lines

 ExecStart=/usr/lib/bluetooth/bluetoothd -C ExecStartPost=/usr/bin/sdptool add SP 
$ sudo systemctl daemon-reload 
$ sudo systemctl enable --now bluetooth 

Edit /etc/modules-load.d/modules.conf to load rfcomm module automatically
Add the following line

Читайте также:  Headset wireless usb bluetooth

Pair with HC-05 module

Access to Bluetooth console

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 
$ sudo rfcomm bind rfcomm0 's MAC> 

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

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 arduinoBT-HC05/ 

Test Communications

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 

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

The project is mainly to communicate between arduino and raspberry pi over bluetooth (HC05)

License

sayederfanarefin/iot-python-raspberry-pi-arduino-bluetooth-communication

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Читайте также:  Bluetooth marshall mid anc отличия

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Communication using Bluetooth between Raspberry Pi and Arduino

The project is mainly to communicate between arduino and raspberry pi over bluetooth (HC05)

I connected the Bluetooth module’s rx to 11, tx to 10 and gn to gnd and vcc to 5v. The en pin was also HIGH (applying 3.3V) Then I powered up the arduino and also the Bluetooth module. Then I uploaded this code to Arduino:

#include SoftwareSerial mySerial(10, 11); void setup() < // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) < ; // wait for serial port to connect. Needed for Leonardo only >Serial.println("Ready!"); // set the data rate for the SoftwareSerial port // for HC-05 use 38400 when poerwing with KEY/STATE set to HIGH on power on mySerial.begin(38400); > void loop() // run over and over < if (mySerial.available())< Serial.write(mySerial.read()); >if (Serial.available()) < mySerial.write(Serial.read()); >> 

I opened the serial monitor And using the «AT» command I set the name and also the Bluetooth module to Master. I used these commands to do so : «AT+NAME=ER» (for name) «AT+ROLE=1» (for Master) Then I bind this module with the other Bluetooth module using its Mac address. The command was: «AT+BIND= (mac of the Bluetooth connected with arduino)» Now, whenever both of the modules are on these two modules will automatically pair.

Firstly, I connected the Pi to my router and an IP was assigned to the Pi using the router’s DHCP. Then I figured out the Pi’s assigned address using the router’s admin panel, and used «Putty» to connect to Pi over ssh using the IP address. Then I connect the Bluetooth module’s rx to pi’s tx pin and Bluetooth module’s tx pin to pi’s rx pin. Since this module is capable of communicating using the serial, so I connected it with the Pi’s serial pins. Then I installed «» package in order to communicate with the Bluetooth. And wrote this script in python:

import serial ser = serial.Serial('/dev/ttyAMA0' , 9600) a=0 while (a < 10): name_out = raw_input ("to arduino: \n") ser.write(name_out + "\n") name_return = ser.readline() print("from arduino: " + name_return) a = a+1 

The Bluetooth module was in slave mode. Then I browse to that directory using the Linux commands and then ran that script.

Integration of the both parts: Now the modules get connected automatically whenever both of them are up. One thing about the HC05 is whenever they are paired the blinking of the led of the Bluetooth module gets slower as in it blinks in every 2sec, and if it is not paired it blinks in every second. And I tried to connect with those individually using my phone's Bluetooth but since they are already paired, both of them refused my Phone's Bluetooth connection prompt. The I send some data from the serial monitor of the arduino and that data showed up in putty which was connected with the Pi. I did it vise versa and it worked!

alt text

For further help you can contact me via email: erfanjordison@gmail.com

Источник

Оцените статью
Adblock
detector