尝试通过蓝牙低功耗控制树莓派3 GPIO上的风扇

时间:2018-12-09 21:34:01

标签: ios python-3.x bluetooth-lowenergy raspberry-pi3 gpio

我有2个双H桥控制器(用于为风扇提供足够的电源),它们通过GPIO和树莓派pi 3 b连接到电源,我已经在python 3中创建了一个脚本,该脚本使用Bluetooth Classic来询问终端中的已连接电话风扇应开启多长时间以及风扇应关闭多长时间,我已经可以很好地工作了,但这仅适用于android,因为它是蓝牙经典产品,我现在想使其正常工作对于我的iPhone来说,我也发现我需要使用BLE,所以我的问题是我如何才能将我的原始脚本转换为Bluetooth Classic(使用RF COMM套接字)以使用Bluetooth Low Energy。

这是我的原始剧本

# Importing the Bluetooth Socket library
import bluetooth
# Importing the GPIO library to use the GPIO pins of Raspberry pi
import RPi.GPIO as GPIO
import time

fan_pin = 16# Initializing pin 16 for fan
fan2_pin = 18
GPIO.setmode(GPIO.BOARD)    # Using BCM numbering
GPIO.setup(fan_pin, GPIO.OUT)   # Declaring the pin 16 as output pin
GPIO.setup(fan2_pin, GPIO.OUT)  # Declaring the pin 16 as output pin

host = ""
port = 1    # Raspberry Pi uses port 1 for Bluetooth Communication

# Creaitng Socket Bluetooth RFCOMM communication
server = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
print('Bluetooth Socket Created')

try:
    server.bind((host, port))
    print("Bluetooth Binding Completed")
except:
    print("Bluetooth Binding Failed")

server.listen(1) # One connection at a time
# Server accepts the clients request and assigns a mac address. 
client, address = server.accept()
print("Connected To", address)
print("Client:", client)
while 1:


    # Receivng the data. 
    client.send("How long do you want the fans to be on for?/n")
    On = int(client.recv(1024)) # 1024 is the buffer size.
    time.sleep(5)
    client.send("Ok, Now how long do you want the fans to be off for?/n")
    Off = int(client.recv(1024))
    print(On)
    print(Off)
    while True:
        GPIO.output(fan_pin, GPIO.HIGH)
        GPIO.output(fan2_pin, GPIO.HIGH)
        time.sleep(On)
        GPIO.output(fan_pin, GPIO.LOW)
        GPIO.output(fan2_pin, GPIO.LOW)
        time.sleep(Off)



# Making all the output pins LOW
GPIO.cleanup()
# Closing the client and server connection
client.close()
server.close()

我想这就像将蓝牙经典代码更改为BLE代码一样简单。最多可以更改20行?

0 个答案:

没有答案