def enableAllMovementAxis(self): # alle Achsen aktivieren, weil das speparate Aktivieren der Achsen die andere Achsen(bits) deaktiviert v = b'\xff\x00' # ByteString von 11111111 (=alle bits) Peripheral.writeCharacteristic(self, handle=0x003f, val=v, withResponse=True)
def handle(self): data = 'dummy' print("Client connected with: ", self.client_address) devaddr = "f1:99:d1:ce:d9:1d random" bleconn = Peripheral(devaddr) while len(data): data = self.request.recv(1024) self.request.send(data) cmd = asc2hex(data.decode('utf-8')).rstrip('0d0a') print("data", len(data)) if len(data) != 0: bleconn.writeCharacteristic(0x0011, cmd) print("Client exited") bleconn.disconnect() self.request.close()
def handle(self): data = 'dummy' print ("Client connected with: ", self.client_address) devaddr = "f1:99:d1:ce:d9:1d random" bleconn = Peripheral(devaddr) while len(data): data = self.request.recv(1024) self.request.send(data) cmd=asc2hex(data.decode('utf-8')).rstrip('0d0a') print ("data",len(data)) if len(data)!=0: bleconn.writeCharacteristic(0x0011,cmd) print("Client exited") bleconn.disconnect() self.request.close()
def threaded_client(conn): conn.send(str.encode('Welcome, to smartLink\n')) while True: a='s' while a=='s': try: bleconn = Peripheral("f1:99:d1:ce:d9:1d random") while True: #-----------receive data from telnet--------------------- data=conn.recv(2048) if not data: a='n' break print("data",data[0]) if data[0]!=255: reply = 'Server output:' +data.decode('utf-8') print("raw server ouput",data.decode('utf-8')) cmd=asc2hex(data.decode('utf-8')).rstrip('0d0a') print("raw cmd:",cmd) bleconn.writeCharacteristic(0x0011,cmd) conn.sendall(str.encode(reply)) except socket.error as e: if isinstance(e.args, tuple): print("errno is %d" % e[0]) if e[0] == errno.EPIPE: # remote peer disconnected print("Detected remote disconnect") else: # determine and handle different error pass else: print("socket error ", e) bleconn.disconnect() conn.close() break except BTLEException as e: #conn.sendall(str.encode("ERROR!!!!")) print(e) #conn.sendall(str.encode("Try again (y/n)?")) bleconn.disconnect() conn.close()
def handle(self): data = 'dummy' isConnected=False print ("Client connected with: ", self.client_address) while len(data): data = self.request.recv(1024) if len(data)>0 and len(data)<5 and data[0]==99 and isConnected==False: #Not null, its (C)onnect command, 4 len command if data[1]>=48 and data[1]<=57: try: bleconn = Peripheral(elements[data[1]-48]) except BTLEException as e: self.request.send(b'error connecting to device\r\n') isConnected=False else: isConnected=True self.request.send(b'connected\r\n') bleconn.writeCharacteristic(0x000f,binascii.a2b_hex("0100")) if len(data)!=0 and isConnected==True: cmd=data.rstrip(b'\r\n') if cmd!=b'' and cmd[0]!=99 and cmd[0]!=100: #if command is not (c)onnect or (d)isconnect try: notify=bleconn.writeCharacteristicWn(0x0011,cmd,True) except BTLEException as e: isConnected=False self.request.send(b'error writing to device\r\n') else: isConnected=True self.request.send(notify['d'][0]) self.request.send(b'\r\n') if len(data)>0 and len(data)<5 and data[0]==100 and isConnected==True: # cmd=data.rstrip(b'\r\n') if cmd!=b'': bleconn.disconnect() self.request.send(b'disconnected\r\n') isConnected=False print("Client exited") if isConnected==True: bleconn.disconnect() self.request.close()
resp = pexpect.spawn('hciconfig hci0 up') resp.expect('.*') Debugging = False #devaddr = sys.argv[1] + " random" devaddr = "f1:99:d1:ce:d9:1d random" print("Connecting to:", devaddr) a = 's' while a == 's': try: conn = Peripheral(devaddr) while True: n = input("Ponga (s) para salir:") cmd = asc2hex(n) try: conn.writeCharacteristic(0x0011, cmd) except BTLEException as e: print("write error:") print(e) print("Try again? (s/n)") b = input() if b == 's': a = 's' break else: b = 'n' if n.strip() == 's': a = 'n' break except BTLEException as e: print("ERROR!!!!")
import binascii if __name__ == "__main__": resp = pexpect.spawn('hciconfig hci0 up') resp.expect('.*') Debugging = False devaddr = "f1:99:d1:ce:d9:1d random" print("Connecting to:", devaddr) a = 's' while a == 's': try: conn = Peripheral(devaddr) while True: n = input("Ponga (s) para salir:") try: conn.writeCharacteristic(0x0011, str.encode(n)) except BTLEException as e: print("write error:") print(e) print("Try again? (s/n)") b = input() if b == 's': a = 's' break else: b = 'n' if n.strip() == 's': a = 'n' break conn.disconnect() except BTLEException as e:
def enableBarometer(self): Peripheral.writeCharacteristic(self, handle=0x0037, val=b'\x01', withResponse=True)
def enableHumidity(self): Peripheral.writeCharacteristic(self, handle=0x002f, val=b'\x01', withResponse=True)
def enableLight(self): Peripheral.writeCharacteristic(self, handle=0x0047, val=b'\x01', withResponse=True)
def enableGyroscope(self): v = b'\x07\x00' # aktiviert nur Gyroskop-Achsen Peripheral.writeCharacteristic(self, handle=0x003f, val=v, withResponse=True)
def enableAccelerometer(self): v = b'8\x01' # aktiviert nur Beschleunigungs-Achsen Peripheral.writeCharacteristic(self, handle=0x003f, val=v, withResponse=True)
resp=pexpect.spawn('hciconfig hci0 up') resp.expect('.*') Debugging = False #devaddr = sys.argv[1] + " random" devaddr = "f1:99:d1:ce:d9:1d random" print("Connecting to:", devaddr) a='s' while a=='s': try: conn = Peripheral(devaddr) while True: n = input("Ponga (s) para salir:") cmd=asc2hex(n) try: conn.writeCharacteristic(0x0011,cmd) except BTLEException as e: print ("write error:") print (e) print ("Try again? (s/n)") b=input() if b == 's': a='s' break else: b='n' if n.strip() == 's': a='n' break except BTLEException as e: print ("ERROR!!!!")
#devaddr = sys.argv[1] + " random" devaddr = "de:2d:06:53:4b:ad random" print("Connecting to:", devaddr) a='s' while a=='s': try: conn = Peripheral(devaddr) while True: n = input("Ponga (s) para salir:") cmd='00:00' if n.strip() == '1': cmd='01:00' if n.strip() == '2': cmd='02:00' try: conn.writeCharacteristic(14,cmd) except BTLEException as e: print ("write error:") print (e) print ("Try again? (s/n)") b=input() if b == 's': a='s' break else: b='n' if n.strip() == 's': a='n' break except BTLEException as e: print ("ERROR!!!!")