Raspberry Pi connects to Arduino via Bluetooth

Source: Internet
Author: User

* * Just beginning to contact if there are errors please leave a message, thank you * *

Equipment
    • Raspberry Pi third-generation B + version
    • Arduino Pro Mini (5v,16mhz) w/atmega328 + Write device (or use other Arduino version)
    • Bluetooth HC-06
    • Light Emitting Diode One
    • 10,000 ohms One
    • DuPont Line several
Packages and tools that are required during installation

In a Python environment, using "Import Bluetooth" If you report the error message "Importerror:no module named Bluetooth" indicates that the appropriate package is not installed, perform a command installation.

$ sudo apt-get update$ sudoinstall bluetooth  BlueZ  Python-bluez
Connecting a Bluetooth device (Arduino)

Use the following command to view the configuration information for Bluetooth

$ hciconfig

Use the following command to scan a paired device

$ hcitool Scan

Enter the Bluetooth connection tool

$ bluetoothctl
[NEW] Controller B8: -: eb:d3: A: B0 raspberrypi [default][bluetooth]# agent onagent registered[bluetooth]# default-Agentdefault Agent Request successful//scanning devices that can be paired[bluetooth]# scan on Discovery started[chg] Controller B8: -: eb:d3: A: B0 discovering:yes[new] Devicexx: -: on:Ten:Ten: +hc- .//Connecting Devices[bluetooth]# Pairxx: -: on:Ten:Ten: +attempting to pair withxx: -: on:Ten:Ten: +[CHG] Devicexx: -: on:Ten:Ten: +connected:yesrequest pin code[agent] Enter PIN code:1234[CHG] Devicexx: -: on:Ten:Ten: +UUIDs:00001101-0000- +-8000-00805f9b34fb[chg] Devicexx: -: on:Ten:Ten: +paired:yespairing Successful//to view connected devices[bluetooth]# paired-Devices Devicexx: -: on:Ten:Ten: +hc- .//to delete a device that is already paired[bluetooth]# Removexx: -: on:Ten:Ten: +[DEL] Devicexx: -: on:Ten:Ten: +hc- .Device has been removed

Test to see if you can ping through

sudo xx:+:£ º

Bluetooth devices connected to Arduino

sudo 0 xx:: £º:+ PressCTRL for Hangup

When a Bluetooth device is connected, a virtual file/dev/rfcomm0 for a Bluetooth device is created in the Raspberry Pi "/dev" directory, and the Arduino Bluetooth LED is solid, indicating "connected."

Bluetooth devices that bind Arduino

sudo 0 xx:+:£ º

When a Bluetooth device is bound, the/dev/rfcomm0 file is also created in the Raspberry Pi "/dev" directory, where the Bluetooth indicator of the Arduino is blinking, which means "not connected", and when the Raspberry Pi sends a message to the Bluetooth device, it does the connection operation.

Unbind a Bluetooth device that is already tied to the Arduino

sudo 0 xx:+:£ º

After unbinding the Bluetooth device, the file/dev/rfcomm0 disappears.

Programming programs
    • Arduino:
void Setup () {Pinmode (11, OUTPUT); Digitalwrite (11, high); Delay (500); Digitalwrite (11, low); Serial.begin (9600);} void Loop () { while(Serial.available ()) {char C=Serial.read (); Digitalwrite (11, high); Delay (500); Digitalwrite (one, low); //Receive Message light (LED) flash a bitif(c = ='A') {serial.println ("B"); //receive a on return to B}Else{serial.println ("Please input [A]"); //all other characters returned }}}

    • Python code:
#Coding=utf-8#Send to ArduinoImportSerialImportSysport="/dev/rfcomm0"Serial= Serial. Serial (port,9600)ifLen (SYS.ARGV) > 1: Sendstr= Sys.argv[1]Else: Sendstr="A"serial.write (SENDSTR) serial.flushinput ()ifSerial.isopen () = =False:serial.open () Line=Serial.readline ()PrintLine

Run Tests

How to test it and not send it.

Raspberry Pi connects to Arduino via Bluetooth

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.