import simplepyble def isFloat(str): if(str.replace(".","").replace("-","").isnumeric()): return True else: return False if __name__ == "__main__": adapters = simplepyble.Adapter.get_adapters() if len(adapters) == 0: print("No adapters found") exit() #hard coded values to identify arduino arduinoName = "Power Supply" arduinoAddress = "30:c6:f7:03:40:9e" serviceID = "7def8317-7300-4ee6-8849-46face74ca2a" characteristicID = "7def8317-7302-4ee6-8849-46face74ca2c" connection = False #choose first adapter, probably the only one computer has adapter = adapters[0] while(not connection): print("scanning for power supply") # Scan for 5 seconds arduinoIndex = -1 arduinoFound = False peripherals = [] #scan until arduino found #goes back to this loop when arduino needs to be found while(not arduinoFound): adapter.scan_for(5000) peripherals = adapter.scan_get_results() for i, peripheral in enumerate(peripherals): if(peripheral.address() == arduinoAddress and peripheral.identifier() == arduinoName): arduinoFound = True arduinoIndex = i if(not arduinoFound): print("could not find power supply, scanning again") #select arduino as specific BLE device peripheral = peripherals[arduinoIndex] #connect to arduino print(f"Connecting to: {peripheral.identifier()} [{peripheral.address()}]") peripheral.connect() connection = True #gather all servies arduino advertises services = peripheral.services() service_characteristic_pair = [] for service in services: for characteristic in service.characteristics(): service_characteristic_pair.append((service.uuid(), characteristic.uuid())) #find specific hard coded characteristic selection = -1 for i, (service_uuid, characteristic) in enumerate(service_characteristic_pair): if(service_uuid == serviceID and characteristic == characteristicID): selection = i #select specific hard service/char service_uuid, characteristic_uuid = service_characteristic_pair[selection] #loops select menu when arduino is connected while(connection): print("select option") print("1: change voltage") print("2: exit") optionSelect = "not a number" while(optionSelect != "1"): optionSelect = input("Enter option: ") if(optionSelect == "2"): exit() elif(optionSelect != "1"): print("invalid channel!") #ask user what channel they want to modify channel = "not a channel" print("which channel would you like to change?") print("1: channel 1") print("2: channel 2") while(channel != "1" and channel != "2"): channel = input("Enter channel to change: ") if(channel != "1" and channel != "2"): print("invalid channel!") #ask user what value they want to set channel to (checks if it is a float) content = "this is not a number" while(not isFloat(content)): content = input("Enter desired voltage: ") if(not isFloat(content)): print("value is not valid!") #checks if value is out of bounds voltage = float(content) if(voltage > 14): print("input voltage exceeds maximum, setting to 14V") voltage = 14 if(voltage < 0): print("input voltage is less than minimum, setting to 0V") voltage = 0 #multiply by 10 to fit arduino formatting voltage *= 10 #converting to int rounds down to fit arduino formatting intVolt = int(voltage) #convert to string to add channel to message message = str(intVolt) #adds channel to end of message if(channel == "1"): message += "A" else: message += "B" while(len(message) < 4): message = "0" + message #write message to arduino, catches exception if disconnected try: peripheral.write_request(service_uuid, characteristic_uuid, str.encode(message)) print("message sent") except: print("lost connection to power supply!") connection = False peripheral.disconnect()