Python and hardware Study Notes: Bluetooth (2)

Source: Internet
Author: User

Python and hardware Study Notes: Bluetooth (2)

You can also set up the bluetooth module according to the AT command in the Appendix. Next we use the bluetooth module to connect to Raspberry Pi to create a bluetooth wireless communication switch. The mobile phone acts as the remote control, we need to set the Bluetooth mode to the slave mode, the baud rate is 9600, And the paired password is 0000.

Bluetooth wireless communication switch Creation

1. Required materials

1) bluetooth module; 2) Raspberry Pi

3) LED lights; 4) Number of connection lines

2. Pin Connection

Bluetooth module Raspberry Pi
GND GND
5 V 5 V
TX RX (Pin 10)
RX TX (Pin 8)
LED Raspberry Pi
Foot GND
Feet GPIO 21 (Pin 40)

 

3. Configuration and use of Raspberry Pi UART

A. Modify the file/boot/cmdline.txt: sudo nano/boot/cmdline.txt

Dwc_otg.lpm_enable = 0 [console = ttyAMA0, 115200 kgdboc = ttyAMA0, 115200] console = tty1root =/dev/mmcblk0p2 rootfstype = ext4 elevator = deadline rootwait

Delete the [...] Part and change it:

Dwc_otg.lpm_enable = 0 console = tty1 root =/dev/mmcblk0p2rootfstype = ext4 elevator = deadline rootwait

B. Modify the file/etc/inittab: sudo nano/etc/inittab.

Comment out the last line:

# T0: 23: respawn:/sbin/getty-L ttyAMA0 115200 vt100

C. Use minicom to test whether Raspberry Pi UART works properly.

Install minicom: sudo apt-get install minicom

Start minicom: minicom-B 9600-o-D/dev/ttyAMA0

Use minicom to send content: Enter the content directly on the console. If minicom enables Echo, you can observe the output content in the console. If ECHO is disabled, the console does not respond, never think that minicom is not working properly.

Minicom echo control: Enable ECHO: press Ctrl + A, then E

Disable ECHO: press Ctrl + A and then Q

D. Use pyserial

Minicom is only applicable for debugging purposes. If programming is required to solve the problem, run the following command to install the python serial extension Library: pyserial: sudo apt-get install python-serial. After installing this library, I can call this library to compile the program.

4. Software Design

First find a step Android phone, and then here link: http://pan.baidu.com/s/1pJNwpdT password: x5f3download a pair of Bluetooth switch .apk ".

Log on to Raspberry Pi and create a Python file: bluetooth. py:

#-*-Coding: UTF-8-* # encoding method import serial # contains the corresponding library import timeimport RPi. GPIO as GPIO. setwarnings (False) # cancel warning GPIO. setmode (GPIO. BCM) # Set the pin encoding method GPIO. setup (21, GPIO. OUT) # Set the GPIO 21 port to the output mode ser = serial. serial ('/dev/ttyAMA0', 9600, timeout = 0.5) # Open the Serial port and set the parameter while True in parentheses: # loop if ser. isOpen () = False: ser. open () switch = ser. read () # print switch for reading data from the serial port # print data read from the serial port # ser. write ('A') # output to the user relative to Bluetooth, this program does not need to be used, only inform if switch = 'y ': # judge whether the read data is 'y' GPIO. output (21, GPIO. HIGH) # ledif switch = 'n' if it is 'y': # judge whether the read data is 'n' GPIO. output (21, GPIO. LOW) # If 'n' is used to extinguish ledtime. sleep (0.1) # necessary latency

Save the file for execution.

Next, we turn on the Android mobile phone Bluetooth, and then search for the device. We will search for the Bluetooth connected to Raspberry Pi, click it, enter the matching password, and the matching is successful. Then open the just downloaded Android program "Bluetooth .apk. 1. Click "Connect Bluetooth". The Bluetooth list is displayed. Select Raspberry Pi Bluetooth. After the connection is successful, the page is displayed, as shown in figure 2.

Figure 1

Now you can test your remote wireless light on button, observe the changes of the LED light. If you are interested, you can use two Raspberry Pi and Bluetooth modules, one for the master and the other for communication.

Related Article

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.