0

I have 2 dual-h bridge controllers(used to supple power to fans) that are connected to my raspberry pi 3 b via GPIO and a battery for power I have created a script in python 3 that uses Bluetooth classic to ask the connected phone in a terminal how long the fan should be on for and how long they should be off for in a loop I have got this to work GREAT, but this works only on android as it is Bluetooth classic i now want to make this work for my iPhone as well i found out i would need to use BLE so my question is how can i convert my original script in Bluetooth classic(using RF COMM Sockets) to use Bluetooth Low Energy.

Here is my original script

# 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()

I would imagine this would be as simple as changing anything that was Bluetooth classic code to BLE code. Max like 20 lines to change?

Mike
  • 4,041
  • 6
  • 20
  • 37
  • BLE doesn't have a serial console type of interface, so it's a fairly large change. You'd need to create a GATT server and have an attribute that a client could modify to turn the fan on/off. Then you'd have to create the clients for the phones to connect to the GATT server. I'm not familiar with Bluetooth on iOS, but it seems like it should be capable of regular Bluetooth. – Tim Tisdall Dec 11 '18 at 16:12
  • I think you are right i was under the misunderstanding that i had to sign up for the MFI program under apple to get access to it apparently that is not the case according to this post: https://stackoverflow.com/questions/18214023/how-to-use-bluetooth-classic-instead-of-le – Michael Lowell Dec 12 '18 at 02:33

0 Answers0