def connect(self): """ Connect to the board and configure it. Note: recreates various objects upon call. """ print("Init BLE connection with MAC: " + self.port) print("NB: if it fails, try with root privileges.") self.gang = Peripheral(self.port, 'random') # ADDR_TYPE_RANDOM print("Get mainservice...") self.service = self.gang.getServiceByUUID(BLE_SERVICE) print("Got:" + str(self.service)) print("Get characteristics...") self.char_read = self.service.getCharacteristics(BLE_CHAR_RECEIVE)[0] print("receive, properties: " + str(self.char_read.propertiesToString()) + ", supports read: " + str(self.char_read.supportsRead())) self.char_write = self.service.getCharacteristics(BLE_CHAR_SEND)[0] print("write, properties: " + str(self.char_write.propertiesToString()) + ", supports read: " + str(self.char_write.supportsRead())) self.char_discon = self.service.getCharacteristics( BLE_CHAR_DISCONNECT)[0] print("disconnect, properties: " + str(self.char_discon.propertiesToString()) + ", supports read: " + str(self.char_discon.supportsRead())) # set delegate to handle incoming data self.delegate = GanglionDelegate(self.scaling_output) self.gang.setDelegate(self.delegate) # enable AUX channel if self.aux: print("Enabling AUX data...") try: self.ser_write(b'n') except Exception as e: print("Something went wrong while enabling aux channels: " + str(e)) print("Turn on notifications") # nead up-to-date bluepy, cf https://github.com/IanHarvey/bluepy/issues/53 self.desc_notify = self.char_read.getDescriptors(forUUID=0x2902)[0] try: self.desc_notify.write(b"\x01") except Exception as e: print( "Something went wrong while trying to enable notification: " + str(e)) print("Connection established")
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()
if __name__ == "__main__": import time #tag = SensorTag("BC:6A:29:AB:D3:7A") 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'
tipping.input['temp'] = float(temp) tipping.input['light'] = float(light) tipping.input['hum'] = float(hum) tipping.compute() output = float(tipping.output['switch_power']) print('temp=%f, `hum=%f`, `light=%f`, `sound=%f`, `air=%f`, `output=%f`' % (temp, hum, light, Sound, Air, output)) last = sqlget() print('last=%s' % last) if output >= 5: print("on") switch_staus = "on" if (sqlauto(1) == 1): print("Auto Model") with Peripheral('C1:8F:CC:DC:57:19', 'random') as p: ch = p.getCharacteristics(uuid=TX_CHAR_UUID)[0] val = binascii.a2b_hex('AA0630306655') ch.write(val) p.disconnect() if (last != switch_staus): print("Sql Insert On") sqlinsert(1, temp, hum, light, Sound, Air, output, 'on') else: print("Manual Model") else: print("off") switch_staus = "off" if (sqlauto(1) == 1): print("Auto Model") with Peripheral('C1:8F:CC:DC:57:19', 'random') as p:
import sys from btle import UUID, Peripheral if len(sys.argv) != 2: print("Fatal, must pass device address:", sys.argv[0], "<device address=" ">") quit() p = Peripheral(sys.argv[1], "random") services = p.getServices() #displays all services for service in services: print(service)