def run(self): bluetooth = Bluetooth() bluetooth.set_advertisement(name='LoPy', service_uuid=b'1234567890123456') bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=self.conn_cb) bluetooth.advertise(True) #Create a new service on the internal GATT server. Returns a object of type BluetoothServerService. srv1 = bluetooth.service( uuid=self.uuid2bytes('00001819-0000-1000-8000-00805f9b34fb'), isprimary=True) #Creates a new characteristic on the service. Returns an object of the class GATTSCharacteristic self.char1 = srv1.characteristic( uuid=self.uuid2bytes('00002a67-0000-1000-8000-00805f9b34fb'), properties=Bluetooth.PROP_BROADCAST | Bluetooth.PROP_INDICATE, value=1) #Creates a callback that will be executed when any of the triggers occurs char1_cb = self.char1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.char1_cb_handler)
class bluetooth: def __init__(self): self.bluetooth = Bluetooth() self.bluetooth.set_advertisement(name=ble_device_name, manufacturer_data=manufacturer_data, service_data=None, service_uuid=private_UUID) self.bluetooth.advertise(True) #service_acc self.service_acc = self.bluetooth.service(uuid=service_acc_UUID, isprimary=True, nbr_chars=3, start=True) self.character_acc_x = self.service_acc.characteristic( uuid=character_acc_x_UUID, properties=None, value=33) self.character_acc_y = self.service_acc.characteristic( uuid=character_acc_y_UUID, properties=None, value=34) self.character_acc_z = self.service_acc.characteristic( uuid=character_acc_z_UUID, properties=None, value=35) def updateValue(self, accelerate=None, gps=None): if (accelerate != None): float_bytes = struct.pack('f', accelerate[0]) #print(float_bytes) print(accelerate[0], accelerate[1], accelerate[2]) total_bytes = b'' total_bytes = struct.pack('<f', accelerate[0]) + struct.pack( '<f', accelerate[1]) + struct.pack( '<f', accelerate[2]) + struct.pack( '<f', gps[0]) + struct.pack('<f', gps[1]) self.character_acc_x.value(total_bytes) self.character_acc_y.value(total_bytes) self.character_acc_z.value(total_bytes)
class ble: def __init__(self): self.bluetooth = Bluetooth() self.bluetooth.set_advertisement(name=ble_device_name, manufacturer_data=manufacturer_data, service_data=None, service_uuid=private_UUID) self.bluetooth.advertise(True) #service self.service_acc = self.bluetooth.service(uuid=service_acc_UUID, isprimary=True, nbr_chars=3, start=True) self.character_acc_x = self.service_acc.characteristic( uuid=character_acc_x_UUID, properties=None, value=33) self.character_acc_y = self.service_acc.characteristic( uuid=character_acc_y_UUID, properties=None, value=34) self.character_acc_z = self.service_acc.characteristic( uuid=character_acc_z_UUID, properties=None, value=35) def updateValue(self, accelerate=None): if (accelerate != None): self.character_acc_x.value(accelerate[0]) self.character_acc_y.value(accelerate[1]) self.character_acc_z.value(accelerate[2])
class PyBluetooth: def __init__(self, name, serviceUUID): self._bt = Bluetooth() self.setAdvertisement(name=name, serviceUUID=serviceUUID) self._bt.callback(trigger=Bluetooth.CLIENT_CONNECTED, handler=self.onClientConnect, arg=self) self._bt.callback(trigger=Bluetooth.CLIENT_DISCONNECTED, handler=self.onClientConnect, arg=self) self._services = {} def __del__(self): self._bt.disconnect_client() def addService(self, uuid, characteristicNumber=0, isPrimary=False): self._services[uuid] = self._bt.service(self.formatUUID(uuid), nbr_chars=characteristicNumber, isprimary=True) return self._services[uuid] def getService(self, uuid): return self._services[uuid] def setAdvertisement(self, name, manufacturerData=None, serviceData=None, serviceUUID=None): self._name = name if serviceUUID != None: serviceUUID = self.formatUUID(serviceUUID) self._bt.set_advertisement(name=name, manufacturer_data=manufacturerData, service_data=serviceData, service_uuid=serviceUUID) def startAdvertisement(self): self._bt.advertise(True) def stopAdvertisement(self): self._bt.advertise(False) def formatUUID(self, uuid): uuid = uuid.encode().replace(b'-', b'') tmp = binascii.unhexlify(uuid) return bytes(reversed(tmp)) def onClientConnect(self): events = self._bt.events() print("[" + self._name + "]: Client connected") pycom.heartbeat(False) pycom.rgbled(0x110011) def onClientDisconnect(self): print("[" + self._name + "]: Client disconnected") pycom.heartbeat(True)
def __init__(self, mesh_mac): self.status = { 'connected' : False } bluetooth = Bluetooth(modem_sleep=False) bluetooth.set_advertisement(name='PyGo (mac:' + str(mesh_mac) + ')', service_uuid=0xec00) bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=self.conn_cb) bluetooth.advertise(True) srv_rx = bluetooth.service(uuid=0xec00, isprimary=True) self.chr_rx = srv_rx.characteristic(uuid=0xec0e, value=0) srv_tx = bluetooth.service(uuid=0xed00, isprimary=True) self.chr_tx = srv_tx.characteristic(uuid=0xed0e, value=0) self.unpacker = None
def _init(self): self.status = {'connected': False} bluetooth = Bluetooth(modem_sleep=False) adv_name = self.ble_name bluetooth.set_advertisement(name=adv_name, service_uuid=0xec00) print("BLE name:", adv_name) bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=self.conn_cb) bluetooth.advertise(True) srv_rx = bluetooth.service(uuid=0xec00, isprimary=True) self.chr_rx = srv_rx.characteristic(uuid=0xec0e, value=0) srv_tx = bluetooth.service(uuid=0xed00, isprimary=True) self.chr_tx = srv_tx.characteristic(uuid=0xed0e, value=0) self.unpacker = None
def BLEServer(): ###### Second, set up BLE server service ###### pycom.heartbeat(False) bluetooth = Bluetooth() #create a bluetooth object bluetooth.set_advertisement(name='LoPyServer' + str(globalvar.device_id), service_uuid=b'3333333333333333') #using callback conn_cb to check client's connection ##Function: conn_cb(callback for bluetooth object events checking) ##Description:check there is any client connected to the service def conn_cb(bt_o): events = bt_o.events( ) #using events to check if there is any client connected to the service if events & Bluetooth.CLIENT_CONNECTED: #2 print("Client connected") pycom.rgbled(0x007f00) # green elif events & Bluetooth.CLIENT_DISCONNECTED: #4 bt_o.disconnect_client() print("Client disconnected") pycom.rgbled(0x7f0000) # red time.sleep(3) bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) bluetooth.advertise(True) #set up BLE service srv1 = bluetooth.service(uuid=b'3333333333333333', isprimary=True) #set up service character chr1 = srv1.characteristic(uuid=b'3333333333333333', properties=Bluetooth.PROP_READ | Bluetooth.PROP_WRITE, value=5) #char1_read_counter = 0 def char1_cb_handler(chr): #global char1_read_counter #char1_read_counter += 1 global BLEConnectionCounter events = chr.events() print('events is ' + str(events)) if events & Bluetooth.CHAR_WRITE_EVENT: #16 print("Write request with value = {}".format(chr.value())) elif events & Bluetooth.CHAR_READ_EVENT: #8 #modify here to send its device_id to other clients BLEConnectionCounter += 1 return str(globalvar.device_id) + ' ' + str(BLEConnectionCounter) #using the callback to send the data to other clients chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)
def BlueToothFun(): global deviceID global connectionCounter #Also act as a server pycom.heartbeat(False) bluetooth = Bluetooth() #create a bluetooth object bluetooth.set_advertisement(name='LoPyServer1', service_uuid=b'1234567890123456') #using callback conn_cb to check client's connection ##Function: conn_cb(callback for bluetooth object events checking) ##Description:check there is any client connected to the service def conn_cb(bt_o): events = bt_o.events( ) #using events to check if there is any client connected to the service if events & Bluetooth.CLIENT_CONNECTED: print("Client connected") pycom.rgbled(0x007f00) # green elif events & Bluetooth.CLIENT_DISCONNECTED: print("Client disconnected") pycom.rgbled(0x7f0000) # red bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) # bluetooth.advertise(True) srv1 = bluetooth.service(uuid=b'1234567890123456', isprimary=True) chr1 = srv1.characteristic(uuid=b'ab34567890123456', value=5) #char1_read_counter = 0 def char1_cb_handler(chr): #global char1_read_counter #char1_read_counter += 1 global connectionCounter events = chr.events() if events & Bluetooth.CHAR_WRITE_EVENT: print("Write request with value = {}".format(chr.value())) else: #modify here to send message to other clients #connectionCounter += 1 return str(deviceID) #using the callback to send the data to other clients char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler) '''srv2 = bluetooth.service(uuid=1234, isprimary=True)
def startSending(dataList: list, callback, shouldClear): global data global call global bluetooth global clear clear = shouldClear call = callback pycom.rgbled(0x70000ff) data = dataList print("data" + str(data)) bluetooth = Bluetooth() bluetooth.set_advertisement(name='Loy', service_uuid=b'1234567890123456') bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) bluetooth.advertise(True) srv1 = bluetooth.service(uuid=b'serviceA90123456', isprimary=True) srv2 = bluetooth.service(uuid=b'serviceB90123456', isprimary=True) srv3 = bluetooth.service(uuid=b'serviceC90123456', isprimary=True) srv4 = bluetooth.service(uuid=b'serviceD90123456', isprimary=True) chr1 = srv1.characteristic(uuid=b'temp567890123456', value="") chr2 = srv2.characteristic(uuid=b'light67890123456', value="") chr3 = srv3.characteristic(uuid=b'time567890123456', value="") chr4 = srv4.characteristic(uuid=b'id34567890123456', value="") chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler) chr2.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char2_cb_handler) chr3.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char3_cb_handler) chr4.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char4_cb_handler)
def bt_check(self): av = AV() av.blue() bluetooth = Bluetooth() bluetooth.set_advertisement(name=BLE_DEVICE_NAME, manufacturer_data=BLE_DEVICE_NAME, service_uuid=0001) bluetooth.advertise(True) srv1 = bluetooth.service(uuid=0001, isprimary=True) chr1 = srv1.characteristic(uuid=0002, value=5) if self.config["state"] == 0: lock_state = 0 else: lock_state = 1 def char1_cb_handler(chr): if chr.value() == bytes(BLE_SECRET_KEY, "utf-8"): if self.config["state"] == 0: self.lock() av.locked() return 'good' else: self.unlock() av.unlocked() return 'good' srv2 = bluetooth.service(uuid=0003, isprimary=True) chr2 = srv2.characteristic(uuid=0004, value=lock_state) char1_cb = chr2.callback(trigger=Bluetooth.CHAR_WRITE_EVENT, handler=char1_cb_handler) time.sleep(20) av.off() bluetooth.advertise(False)
def connect(self): bluetooth = Bluetooth() bluetooth.set_advertisement(name='Blynk') def conn_cb(bt): events = bt.events() if events & Bluetooth.CLIENT_CONNECTED: self.bout = b'' print("Client connected") elif events & Bluetooth.CLIENT_DISCONNECTED: print("Client disconnected") BlynkProtocol.disconnect(self) bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) nus = bluetooth.service( uuid=unhex('6E400001-B5A3-F393-E0A9-E50E24DCCA9E'), isprimary=True, nbr_chars=2) self.rx = nus.characteristic( uuid=unhex('6E400002-B5A3-F393-E0A9-E50E24DCCA9E'), properties=Bluetooth.PROP_WRITE | Bluetooth.PROP_WRITE_NR, value='') self.tx = nus.characteristic( uuid=unhex('6E400003-B5A3-F393-E0A9-E50E24DCCA9E'), properties=Bluetooth.PROP_READ | Bluetooth.PROP_NOTIFY, value='') bluetooth.advertise(True) def rx_cb(chr): data = chr.value() print('>', data) self.process(bytes(data)) def tx_subsc(chr): print("Client subscribed", chr) BlynkProtocol.connect(self, login=False) self.tx.callback(trigger=Bluetooth.CHAR_SUBSCRIBE_EVENT, handler=tx_subsc) self.rx.callback(trigger=Bluetooth.CHAR_WRITE_EVENT, handler=rx_cb)
class Blues: # Initialization method. def __init__(self): print('Advertising Bluetooth...') self.mBluetooth = Bluetooth() self.mEvents = self.mBluetooth.events() # self.mBluetooth.init([id=0, mode=Bluetooth.BLE, antenna=Bluetooth.INT_ANT, modem_sleep=False, pin=None, privacy=True, secure_connections=True, mtu=200]) self.mBluetooth.set_advertisement( name='FiPy', service_uuid=2) self.mBluetooth.callback(trigger=self.mBluetooth.CLIENT_CONNECTED | self.mBluetooth.CLIENT_DISCONNECTED, handler=self.Connection) self.mBluetooth.advertise(True) # Create Service & Characteristic self.CreateServChar() self.mCharacteristic.callback( trigger=self.mBluetooth.CHAR_WRITE_EVENT | self.mBluetooth.CHAR_READ_EVENT, handler=self.CharCallback) # Connection method: Used as callback function to detect a BLE connection. def Connection(self, zResponse): self.mEvents = zResponse.events() if self.mEvents & self.mBluetooth.CLIENT_CONNECTED: print("Client connected") elif self.mEvents & self.mBluetooth.CLIENT_DISCONNECTED: print("Client disconnected") def CreateServChar(self): # Create service and characteristic self.mService = self.mBluetooth.service( uuid=3, isprimary=True) self.mCharacteristic = self.mService.characteristic( uuid=31, properties=self.mBluetooth.PROP_WRITE | self.mBluetooth.PROP_READ, value=6) def CharCallback(self, chr, zResponse): self.mEvents, value = zResponse if self.mEvents & self.mBluetooth.CHAR_READ_EVENT: print("Read from Char = {}".format(value)) if self.mEvents & self.mBluetooth.CHAR_WRITE_EVENT: print("Written to Char = {}".format(value))
- 3. It receives the bluetooth cmds to open/close back door throw proximity sensor """ # Broadcasting BLE and stablish connection from network import Bluetooth import time import machine bluetooth = Bluetooth() bluetooth.set_advertisement(name='PyWagenHub', manufacturer_data='manufacturer data', service_uuid=b'1234567890123456') service = bluetooth.service(b'1234567890123457', isprimary=True, nbr_chars=1, start=True) def conn_cb(bt_o): events = bt_o.events( ) # This method returns the flags and clears the internal registry print(events) if events & Bluetooth.CLIENT_CONNECTED: print("Client connected") elif events & Bluetooth.CLIENT_DISCONNECTED: print("Client disconnected") bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED,
print("Start State:{} ".format(start_state)) main_protocol_thread = _thread.start_new_thread(protocol_state_machine, (start_state, 1)) bluetooth = Bluetooth() service_uuid = util.uuid2bytes('00002A6E00001000800000805F9B34FB') bluetooth.set_advertisement(name='Comfort', service_uuid=service_uuid) bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) bluetooth.advertise(True) srv1 = bluetooth.service(uuid=service_uuid, isprimary=True, nbr_chars=2) heater_ch = srv1.characteristic( uuid=util.uuid2bytes('00001AEF00001000800000805F9B34FB'), properties=Bluetooth.PROP_NOTIFY, value=0) controller_ch = srv1.characteristic( uuid=util.uuid2bytes('00001CDE00001000800000805F9B34FB'), properties=Bluetooth.PROP_WRITE | Bluetooth.PROP_READ, value=0) heater_cb = heater_ch.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=heater_cb_handler) controller_cb = controller_ch.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT,
else: if True: #if char1_read_counter < 3: print('Read request on char 1') else: return 'ABC DEF' bluetooth = Bluetooth() bluetooth.set_advertisement(name='gpy4gcam', service_uuid=1) bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) bluetooth.advertise(True) srv1 = bluetooth.service(uuid=1, isprimary=True, start=False) cv = '2020-01-01 00:00:00' chr1 = srv1.characteristic(uuid=2, value=cv) char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler) srv1.start() def chr1_update(Now): # rtc = machine.RTC() # Now = rtc.now() strDateTime = '{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format( Now[0] + 1, Now[1], Now[2], Now[3], Now[4], Now[5]) # srv1.stop() # chr1 = srv1.characteristic(uuid=2, value=strDateTime)
print("init") bt = Bluetooth() bt.set_advertisement( name='Pycom BT test server5', service_uuid=666, manufacturer_data=b'deadbeef', service_data=b'cafebabe' ) # bytes([0xC0, 0x01, 0xD0, 0x0D, 0xC0, 0x01, 0xD0, 0x0D, 0xC0, 0x01, 0xD0, 0x0D, 0xC0, 0x01, 0xD0, 0x0D, ])) # b'1234567890123456') bt.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) print("service 1") srv1 = bt.service( isprimary=True, uuid=1 ) # bytes([0xCA, 0xFE, 0xBA, 0xBE, 0xCA, 0xFE, 0xBA, 0xBE, 0xCA, 0xFE, 0xBA, 0xBE, 0xCA, 0xFE, 0xBA, 0xBE, ])) chr11 = srv1.characteristic(uuid=11, value=87) chr11.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char11_cb_handler) chr12 = srv1.characteristic(uuid=12, value=99) # chr13 = srv1.characteristic(uuid=13, value=33) print("service 2") srv2 = bt.service( isprimary=True, uuid=2, ) chr21 = srv2.characteristic(uuid=4567, value=9) # 0x1234) chr21.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=char21_cb_handler) # chr22 = srv2.characteristic(uuid=89, value=2) # 0x1234)
# Function to caculate the pwm value for the angle given. def setServoPwn(angle): servo.duty_cycle( ((angle / 180) * 0.05) + 0.05) # calculate the right value for PWM based on the angle given bluetooth = Bluetooth() # Get a bluetooth instance bluetooth.set_advertisement(name='FyPi') # Set the name bluetooth.callback( trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) # set up the callbacks for connect and disconnect events bluetooth.advertise(True) # advertise the device print("Started BLE") srv1 = bluetooth.service( uuid=0x1815, isprimary=True ) # set up the service to display the current angle of the servo chr1 = srv1.characteristic( uuid=0x2763, value=currentAngle) # set up the characteristic to get the server angle char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT, handler=char1_cb_handler ) # set up the callback when writen to characteristic pwm = PWM(0, frequency=50) # make a pwm provider servo = pwm.channel(0, pin='P23', duty_cycle=0.0) # Setup the pwm for the servo setServoPwn(currentAngle) # Set the servo the the initial angle
class BLE(): # This class initializes the BLE mode in the fipy and also checks # if the BLE mode param is on or not def __init__(self, name, uuid): self.ble = Bluetooth() self.ble.set_advertisement(name=name, service_uuid=uuid) self.setup() def connect_cb(self, bt_o): # callback for connection print('enter connect_cb\n') events = bt_o.events() if events & Bluetooth.CLIENT_CONNECTED: print("Client connected") elif events & Bluetooth.CLIENT_DISCONNECTED: print("Client disconnected") self.string = "" def setup(self): # sets up all the services and characteristics print("Enter Setup\n") self.ble.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=self.connect_cb) self.ble.advertise(True) # main service switches config_service = self.ble.service(uuid=0xfff0, isprimary=True, nbr_chars=1) # main service self.message = config_service.characteristic(uuid=0xfff0, value=0) # nvram declarations self.string = "" pycom.nvs_set('id', 0) pycom.nvs_set('mode', 0) pycom.nvs_set('wifi', 0) pycom.nvs_set('mqtt', 0) pycom.nvs_set('lora', 0) pycom.nvs_set('ssid', 0) pycom.nvs_set('pass', 0) pycom.nvs_set('server', 0) pycom.nvs_set('port', 0) pycom.nvs_set('appSkey', 0) pycom.nvs_set('nwkSkey', 0) pycom.nvs_set('temp_sensor', 0) pycom.nvs_set('temp_f', 0) pycom.nvs_set('accl_sensor', 0) pycom.nvs_set('accl_f', 0) pycom.nvs_set('alt_sensor', 0) pycom.nvs_set('alt_f', 0) pycom.nvs_set('light_sensor', 0) pycom.nvs_set('light_f', 0) # callbacks to deploy_handler which will use the switch_dict to switch between chars msg_cb = self.message.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.msg_handler) # restart details callback) print("waiting for callback") def msg_handler(self,chr): print("Entered msg_handler\n") # handles write and read requests from the client print(chr.value()) events = chr.events() if events & Bluetooth.CHAR_WRITE_EVENT: msg = chr.value().decode('utf-8') self.string += msg print("string is" + self.string) if msg[len(msg)-2:] == '>e': self.list = [] self.list = self.string[:len(self.string)-2].split(";") msg_list = msg[1:len(msg)-2].split(";") print(self.list) self.execute(self.list) return # device should reset after this elif events & Bluetooth.CHAR_READ_EVENT: NvsStore('deveui', binascii.hexlify(LoRa().mac()).decode('utf-8')) return binascii.hexlify(LoRa().mac()).decode('utf-8') def execute(self, msg_list): for x in range(len(msg_list)): if msg_list[x] == '': msg_list[x] = '0' print(msg_list) print("count" + str(len(msg_list))) if len(msg_list) == 15: NvsStore('id', msg_list[0]) if msg_list[2] == "1": NvsStore('wifi', msg_list[2]) NvsStore('ssid', msg_list[5]) NvsStore('pass', msg_list[6]) else: pycom.nvs_set('wifi', 0) if msg_list[3] == "1": NvsStore('mqtt', msg_list[3]) NvsStore('server', msg_list[7]) NvsStore('port', msg_list[8]) else: pycom.nvs_set('mqtt', 0) if msg_list[4] == "1": NvsStore('lora', msg_list[4]) NvsStore('appskey', msg_list[9]) NvsStore('nwkskey', msg_list[10]) else: pycom.nvs_set('mqtt', 0) if msg_list[11] != 0: NvsStore('temp_sensor', '1') NvsStore('temp_f', msg_list[11]) else: pycom.nvs_set('temp_sensor', 0) if msg_list[12] != 0: NvsStore('alt_sensor', '1') NvsStore('alt_f', msg_list[12]) else: pycom.nvs_set('alt_sensor', 0) if msg_list[13] != 0: NvsStore('accl_sensor', '1') NvsStore('accl_f', msg_list[13]) else: pycom.nvs_set('accl_sensor', 0) if msg_list[14] != 0: NvsStore('light_sensor', '1') NvsStore('light_f', msg_list[14]) else: pycom.nvs_set('light_sensor', 0) if int(msg_list[2]) + int(msg_list[4]) == 0: print("YAS") NvsStore('mode', 0) NvsStore('mode', msg_list[1]) pycom.rgbled(0xffffff) time.sleep(2) pycom.rgbled(0x000000) print("ENTERING DEPLOY MODE IN 5 seconds") time.sleep(5) machine.reset() else: print("INCORRECT DATA STREAM") self.string = ""
from network import Bluetooth bluetooth = Bluetooth() bluetooth.set_advertisement(name='FiPy', service_uuid=0xffffff01234) def conn_cb (bt_o): events = bt_o.events() if events & Bluetooth.CLIENT_CONNECTED: print("Client connected") elif events & Bluetooth.CLIENT_DISCONNECTED: print("Client disconnected") bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) bluetooth.advertise(True) srv1 = bluetooth.service(uuid=0xfff0, isprimary=True) chr1 = srv1.characteristic(uuid=0x1000, value="ssid") def char1_cb_handler(chr): events = chr.events() if events & Bluetooth.CHAR_WRITE_EVENT: chr1.value(chr.value()) print("Write request with value = {}".format(chr.value())) else: return chr1.value() char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)
def BLEServer(): bluetooth = Bluetooth() #create a bluetooth object bluetooth.set_advertisement(name=settings['deviceid'], service_uuid=settings['filipsblue_service']) #using callback conn_cb to check client's connection ##Function: conn_cb(callback for bluetooth object events checking) ##Description:check there is any client connected to the service def conn_cb(bt_o): events = bt_o.events( ) #using events to check if there is any client connected to the service global isConnected if events & Bluetooth.CLIENT_CONNECTED: print("Client connected") isConnected = True pycom.rgbled(0x007f00) # green elif events & Bluetooth.CLIENT_DISCONNECTED: bt_o.disconnect_client( ) # in this way other client will have the chance to connect? print("Client disconnected") isConnected = False pycom.rgbled(0x7f0000) # red time.sleep(3) bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) bluetooth.advertise(True) print("Started BLE advertising") ########################### # BLE Ambient Sensor Service ########################### ambient_sensor_service = bluetooth.service( uuid=settings['ambient_sensor_service'], isprimary=True, nbr_chars=2 ) # nbr_chars indicates the number of characteristics of this service #set up service characteristic ambient_sensor_chr = ambient_sensor_service.characteristic( uuid=settings['ambient_sensor_chr'], properties=Bluetooth.PROP_NOTIFY, value='NA') ambient_sensor_sample_interval_chr = ambient_sensor_service.characteristic( uuid=settings['ambient_sensor_sample_interval_chr'], properties=Bluetooth.PROP_WRITE, value='NA') def ambient_sensor_sample_interval_cb_handler(chr): events = chr.events() if events & Bluetooth.CHAR_WRITE_EVENT: print("ambient_sensor_sample_interval_cb_handler : Raw value: {}". format(chr.value())) try: sample_interval = float(chr.value()) ambient_sensor.set_sample_interval(sample_interval) print("Updated ambient sampling rate: {}".format( sample_interval)) except: print( "ambient_sensor_sample_interval_cb_handler : Invalid value. Expected type float." ) pass ambient_sensor_sample_interval_chr.callback( trigger=Bluetooth.CHAR_WRITE_EVENT, handler=ambient_sensor_sample_interval_cb_handler) ambient_sensor_service.start() def ble_ambient_sensor_callback(lux_sample): print("Should we notify? : " + str(isConnected)) # Can take an integer, a string or a bytes object. Can only be called if there clients are connected? # Should trigger notification if a client has registered for notifications if isConnected: try: ambient_sensor_chr.value( str(lux_sample)) # Send the notification over BLE print("Emitted lux value: " + str(lux_sample)) except: # Some error happened during notification. # Most likely no client is connected to receive notifications. # Disconnect bluetooth communication. bluetooth.disconnect_client() ambient_sensor = AmbientSensor( callback_on_emit=ble_ambient_sensor_callback) _thread.start_new_thread(ambient_sensor.start_sampling, ()) # Start sampling the ambient sensor ########################### # BLE Light Source Service ########################### light_source_service = bluetooth.service( uuid=settings['light_source_service'], isprimary=True) #set up service characteristic light_source_chr = light_source_service.characteristic( uuid=settings['light_source_chr'], properties=Bluetooth.PROP_WRITE, value='\xff\xff\xff') def light_source_chr_cb_handler(chr): events = chr.events() if events & Bluetooth.CHAR_WRITE_EVENT: colorsArray = chr.value() try: print("light_source_chr_cb_handler : Raw value: {}".format( chr.value())) red = colorsArray[0] green = colorsArray[1] blue = colorsArray[2] print("Set LED with value = {} {} {}".format(red, green, blue)) set_LED_rgb(red, green, blue) except: print( "light_source_chr_cb_handler : Invalid value. Expected byte array of length 3." ) pass light_source_chr.callback(trigger=Bluetooth.CHAR_WRITE_EVENT, handler=light_source_chr_cb_handler) light_source_service.start()
to_send = [] rx_data = [] enable_send_to_lora = False poll_again = True message_sent_to_lora = False msg = b'' cpt = 0 length_totale = 0 ######################### Initialisation du bluetooth ########################## bluetooth = Bluetooth() bluetooth.set_advertisement(name=NAME, service_uuid=service_uuid) bluetooth.advertise(True) # Definition du service et des caracteristiques service = bluetooth.service(uuid=service_uuid, nbr_chars=4, isprimary=True) char_send = service.characteristic(uuid=char_send_uuid, properties=Bluetooth.PROP_WRITE) char_sent = service.characteristic(uuid=char_sent_uuid, properties=Bluetooth.PROP_NOTIFY) char_receive = service.characteristic(uuid=char_receive_uuid, properties=Bluetooth.PROP_NOTIFY) char_lora_ready = service.characteristic(uuid=char_lora_ready_uuid, properties=Bluetooth.PROP_NOTIFY | Bluetooth.PROP_READ, value=b'\x00') def test(*arg): msg = b'test' type = b'\x01'
from network import Bluetooth bluetooth = Bluetooth() bluetooth.set_advertisement(name='LoPy', service_uuid=0x180F) def conn_cb (bt_o): events = bt_o.events() if events & Bluetooth.CLIENT_CONNECTED: print("Client connected") elif events & Bluetooth.CLIENT_DISCONNECTED: print("Client disconnected") bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) srv1 = bluetooth.service(uuid=0x180F, isprimary=True) chr1 = srv1.characteristic(uuid=0x2A19, value='50') srv2 = bluetooth.service(uuid=0x180A, isprimary=True) chr2 = srv2.characteristic(uuid=0x2A29, value='name string') bluetooth.advertise(True) def char1_cb_handler(chr): events = chr.events() if events & Bluetooth.CHAR_WRITE_EVENT: print("Write request on char1 with value = {}".format(chr.value())) else: print("Read request on char1 value = {}".format(chr.value())) char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)
# # mkfifo ~/myfifo # while sleep 1; do socat /dev/ttyACM1,b115200,echo=0,raw ~/myfifo; done & # minicom -o -D ~/myfifo bluetooth = Bluetooth() bluetooth.set_advertisement( name='gpy4gcam', service_uuid=uuid2bytes('d9293538-3087-11ea-aec2-2e728ce88125')) bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) bluetooth.advertise(True) # srv1 = bluetooth.service(uuid=uuid2bytes('d9293538-3087-11ea-aec2-2e728ce88125'), isprimary=True, nbr_chars=2) srv1 = bluetooth.service(uuid=0xAA1, isprimary=True, nbr_chars=2) cv = '2020-01-01 00:00:00' chr1 = srv1.characteristic(uuid=0xaaa1, value=cv) chr2 = srv1.characteristic(uuid=0xaaa2, value=9) char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler) char2_cb = chr2.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char2_cb_handler) # srv1.start() def chr1_update(Now): # rtc = machine.RTC()
#alkutehtävät käynnistyksessä bluetooth = Bluetooth() bluetooth.set_advertisement(name='Kuormanohjausyksikkö', service_uuid=b'1234567890123456') def conn_cb (bt_o): global connected events = bt_o.events() # this method returns the flags and clears the internal registry if events & Bluetooth.CLIENT_CONNECTED: print("Client connected") connected = True elif events & Bluetooth.CLIENT_DISCONNECTED: print("Client disconnected") connected = False #Create service and characteristic wifiService = bluetooth.service(uuid='0000000000000000', isprimary=True) wifiChara = wifiService.characteristic(uuid='0000000000000002', properties=Bluetooth.PROP_BROADCAST | Bluetooth.PROP_WRITE) infoService = bluetooth.service(uuid='0000000000000001',isprimary=True) infoChara = infoService.characteristic(uuid='0000000000000003',properties=Bluetooth.PROP_BROADCAST | Bluetooth.PROP_READ,value=0) bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) wifiChara.callback(trigger=Bluetooth.CHAR_WRITE_EVENT, handler=receiveWifiInfo) infoChara.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=sendInfo) connected = False ssid = "aalto open" password = "" proceed = False [ssid,password]=openWifiInfo()
class BLE(): # This class initializes the BLE mode in the fipy and also checks # if the BLE mode param is on or not def __init__(self, name, uuid): self.ble = Bluetooth() self.ble.set_advertisement(name=name, service_uuid=uuid) self.setup() def connect_cb(self, bt_o): # callback for connection print('enter connect_cb\n') events = bt_o.events() if events & Bluetooth.CLIENT_CONNECTED: print("Client connected") elif events & Bluetooth.CLIENT_DISCONNECTED: print("Client disconnected") def setup(self): # sets up all the services and characteristics print("Enter Setup\n") self.ble.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=self.connect_cb) self.ble.advertise(True) # main service switches boot_service = self.ble.service(uuid=0xfff0, isprimary=True, nbr_chars=5) wifi_service = self.ble.service(uuid=0xfff1, isprimary=True, nbr_chars=2) mqtt_service = self.ble.service(uuid=0xfff2, isprimary=True, nbr_chars=2) lora_service = self.ble.service(uuid=0xfff3, isprimary=True, nbr_chars=3) restart_service = self.ble.service(uuid=0xfff4, isprimary=True, nbr_chars=1) #lte_service = self.ble.service(uuid=0xfff3, isprimary=True, nbr_chars=1) misc_service = self.ble.service(uuid=0xfff5, isprimary=True, nbr_chars=4) # characteristic / service declarations self.id_f = boot_service.characteristic(uuid=0x01, value=0) pycom.nvs_set('id', 0) self.mode_f = boot_service.characteristic(uuid=0x02, value=0) pycom.nvs_set('mode', 0) # generic services to be added to boot service self.wifi_f = boot_service.characteristic(uuid=0x03, value=0) pycom.nvs_set('wifi', 0) self.mqtt_f = boot_service.characteristic(uuid=0x04, value=0) pycom.nvs_set('mqtt', 0) self.lora_f = boot_service.characteristic(uuid=0x05, value=0) pycom.nvs_set('lora', 0) #self.lte_f = boot_service.characteristic(uuid=0x2002, value=0) #pycom.nvs_set('lte', 0) # restart declaration self.restart_f = restart_service.characteristic(uuid=0x01, value=0) # wifi_service chars self.wifi_ssid = wifi_service.characteristic(uuid=0x01, value=0) self.wifi_pass = wifi_service.characteristic(uuid=0x02, value=0) # mqtt_service chars self.mqtt_server = mqtt_service.characteristic(uuid=0x03, value=0) self.mqtt_port = mqtt_service.characteristic(uuid=0x04, value=0) # lora_service chars self.lora_deveui = lora_service.characteristic(uuid=0x01, value=0) self.lora_appSkey = lora_service.characteristic(uuid=0x02, value=0) self.lora_nwkSkey = lora_service.characteristic(uuid=0x03, value=0) # sensor_service chars -> light, pressure, humidity, temperature, altitude self.temp_sensor = misc_service.characteristic(uuid=0x01, value=0) self.alt_sensor = misc_service.characteristic(uuid=0x03, value=0) self.accl_sensor = misc_service.characteristic(uuid=0x05, value=0) self.light_sensor = misc_service.characteristic(uuid=0x07, value=0) # callbacks to deploy_handler which will use the switch_dict to switch between chars id_f_cb = self.id_f.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.deploy_handler) mode_f_cb = self.mode_f.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.deploy_handler) wifi_f_cb = self.wifi_f.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.deploy_handler) mqtt_f_cb = self.mqtt_f.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.deploy_handler) lora_f_cb = self.lora_f.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.deploy_handler) #lte_f_cb = self.lte_f.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.deploy_handler) # restart details callback restart_f_cb = self.restart_f.callback( trigger=Bluetooth.CHAR_WRITE_EVENT, handler=self.restart_handler) # wifi details callback wifi_cb1 = self.wifi_ssid.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.wifi_handler) wifi_cb2 = self.wifi_pass.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.wifi_handler) print("Enter callback mqtt\n") # mqtt details callback mqtt_cb1 = self.mqtt_server.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.mqtt_handler) mqtt_cb2 = self.mqtt_port.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.mqtt_handler) # lora details callback lora_cb1 = self.lora_deveui.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=self.lora_handler) lora_cb2 = self.lora_appSkey.callback( trigger=Bluetooth.CHAR_WRITE_EVENT, handler=self.lora_handler) lora_cb3 = self.lora_nwkSkey.callback( trigger=Bluetooth.CHAR_WRITE_EVENT, handler=self.lora_handler) # sensor details callback temp_sensor_cb = self.temp_sensor.callback( trigger=Bluetooth.CHAR_WRITE_EVENT, handler=self.sensor_handler) alt_sensor_cb = self.alt_sensor.callback( trigger=Bluetooth.CHAR_WRITE_EVENT, handler=self.sensor_handler) accl_sensor_cb = self.accl_sensor.callback( trigger=Bluetooth.CHAR_WRITE_EVENT, handler=self.sensor_handler) light_sensor_cb = self.light_sensor.callback( trigger=Bluetooth.CHAR_WRITE_EVENT, handler=self.sensor_handler) print("setup done/n") def deploy_handler(self, chr): print("Entered Deploy Handler\n") # handles write and read requests from the client events = chr.events() if events & Bluetooth.CHAR_WRITE_EVENT: val = chr.value().decode('utf-8') print(val[0]) print(val) if val[0] == '0': self.id_f.value(val[1:]) NvsStore('id', val[1:]) if val[0] == '1': self.mode_f.value(val[1]) NvsStore('mode', val[1]) elif val[0] == '2': self.wifi_f.value(val[1]) NvsStore('wifi', val[1]) elif val[0] == '3': self.mqtt_f.value(val[1]) NvsStore('mqtt', val[1]) elif val[0] == '4': self.lora_f.value(val[1]) NvsStore('lora', val[1]) else: pass elif events & Bluetooth.CHAR_READ_EVENT: return "REJECTED" def wifi_handler(self, chr): print("Wifi Handler\n") # handles writing the wifi ssid and password events = chr.events() if events & Bluetooth.CHAR_WRITE_EVENT: val = chr.value().decode("utf-8") print(val) if val[0] == '0': self.wifi_ssid.value(val[1:]) x = val[1:] NvsStore('ssid', x) print(NvsExtract('ssid').retval()) elif val[0] == '1': self.wifi_pass.value(val[1:]) NvsStore('pass', val[1:]) print(NvsExtract('pass').retval()) elif events & Bluetooth.CHAR_READ_EVENT: try: print("Connected to ssid: " + x) except NameError: print("not declared yet") print("exit") def lora_handler(self, chr): # handles writing the lora deveui, appSkey and nwkSkey events = chr.events() if events & Bluetooth.CHAR_WRITE_EVENT: val = chr.value().decode('utf-8') if val[0] == '0': NvsStore('deveui', binascii.hexlify(LoRa().mac()).decode('utf-8')) elif val[0] == '1': self.lora_appSkey.value(val[1:]) NvsStore('appSkey', val[1:]) elif val[0] == '2': self.lora_nwkSkey.value(val[1:]) NvsStore('nwkSkey', val[1:]) elif events & Bluetooth.CHAR_READ_EVENT: return binascii.hexlify(LoRa().mac()).decode('utf-8') def restart_handler(self, chr): events = chr.events() print("restart handler\n") if events & Bluetooth.CHAR_WRITE_EVENT: val = chr.value().decode("utf-8") if val == b'1': machine.reset( ) # hard reset resets hardware and software except firmware loaded else: print("Breach") def mqtt_handler(self, chr): print("MQTT handler") events = chr.events() val = chr.value().decode("utf-8") if events & Bluetooth.CHAR_WRITE_EVENT: if val[0] == '0': self.wifi_ssid.value(val[1:]) NvsStore('server', val[1:]) elif val[0] == '1': self.wifi_pass.value(val[1:]) NvsStore('port', val[1:]) elif events & Bluetooth.CHAR_READ_EVENT: print("Connected to server: " + str(self.mqtt_server.value())) def sensor_handler(self, chr): print("Sensor handler") events = chr.events() val = chr.value().decode("utf-8") if events & Bluetooth.CHAR_WRITE_EVENT: if val[0] == '0': self.temp_sensor.value(val[1]) NvsStore('tempsensor', val[1]) NvsStore('temp_f', val[2:]) elif val[0] == '1': self.alt_sensor.value(val[1]) NvsStore('altsensor', val[1]) NvsStore('alt_f', val[2:]) elif val[0] == '2': self.accl_sensor.value(val[1]) NvsStore('acclsensor', val[1]) NvsStore('accl_f', val[2:]) elif val[0] == '3': self.light_sensor.value(val[1]) NvsStore('lightsensor', val[1]) NvsStore('light_f', val[2:]) else: pass # for now
class BLEUart: def __init__(self): self.status = {'connected': False} self.rx_data = [] def begin(self, name): self._ble = Bluetooth() # Some apps require the service uuid to be part of the advertising packet for the device to show up # as Uart capable, like the Bluefruit Connect app self._ble.set_advertisement( name=name, service_uuid=unhex('6E400001-B5A3-F393-E0A9-E50E24DCCA9E')) #self._ble.set_advertisement(name=name) self._ble.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=self.conn_callback) self._ble.advertise(True) nus = self._ble.service( uuid=unhex('6E400001-B5A3-F393-E0A9-E50E24DCCA9E'), isprimary=True, nbr_chars=2) self.rx = nus.characteristic( uuid=unhex('6E400002-B5A3-F393-E0A9-E50E24DCCA9E'), #properties=Bluetooth.PROP_WRITE | Bluetooth.PROP_WRITE_NR, properties=Bluetooth.PROP_WRITE_NR, value='') self.tx = nus.characteristic( uuid=unhex('6E400003-B5A3-F393-E0A9-E50E24DCCA9E'), #properties=Bluetooth.PROP_READ | Bluetooth.PROP_NOTIFY, properties=Bluetooth.PROP_NOTIFY, value='') self.tx.callback(trigger=Bluetooth.CHAR_SUBSCRIBE_EVENT, handler=self.tx_subsc_callback) self.rx.callback(trigger=Bluetooth.CHAR_WRITE_EVENT, handler=self.rx_callback) def end(self): self.status['connected'] = False self.rx_data = [] self._ble.disconnect_client() self._ble.deinit() def conn_callback(self, bt): events = bt.events() if events & Bluetooth.CLIENT_CONNECTED: print("Client connected") self.status['connected'] = True elif events & Bluetooth.CLIENT_DISCONNECTED: print("Client disconnected") self.status['connected'] = False self.rx_data = [] def rx_callback(self, chr, msg): data = chr.value() #print('BLE data received') strdata = str(data, 'utf-8') #print('> ', strdata) self.rx_data.append(strdata) def tx_subsc_callback(self, chr, msg): print("Client subscribed", chr.value()) def write(self, msg): if self.status['connected'] == True: while len(msg): data = msg[:20] msg = msg[20:] print('BLE data send:') print('<', data) self.tx.value(data) self.tx.value('\r\n') def available_data(self): return (True if (len(self.rx_data) > 0) else False) def get_data(self): if self.available_data(): data = self.rx_data[0] del self.rx_data[0] return data else: return 0
pycom.heartbeat(False) #=============================================================================== bluetooth = Bluetooth() bluetooth.set_advertisement(name='LoPy',manufacturer_data='Pycom', service_data='Blutooth Low Energy') bluetooth.start_scan(-1) # scan forever pycom.rgbled(0x00007f) # Blue #=============================================================================== init_value = 0x20 char1_read_counter = 0 #=============================================================================== # UUID are READ LSB first (little endian) # i.e. # b'BLUTOOTH_SERVICE' = b'424c55544f4f54485f53455256494345' # will appear as 45434956-5245-535F-4854-4F4F54554C42 b'BLUTOOTH_SERVICE' srv1 = bluetooth.service(uuid=b'BLUTOOTH_SERVICE', isprimary=True) char1 = srv1.characteristic(uuid=b'CHARACTERISTIC!!', value=40) char1.value(init_value) #=============================================================================== def char1_cb_handler(char1): global char1_read_counter events = char1.events() if events & Bluetooth.CHAR_WRITE_EVENT: print("Write request with value = {}".format(char1.value())) if (char1_read_counter < 1): print('Read request on char 1') else: print('Read request on char 1') char1.value(init_value + char1_read_counter) # overwrite the characteristic value char1_read_counter += 1 #===============================================================================
# def char21_cb_handler(chr, data): # # The value is not used in this callback as the WRITE events are not processed. # events, value = data # if events & Bluetooth.CHAR_READ_EVENT: # print('Read request on char 21') bluetooth = Bluetooth() bluetooth.set_advertisement(name='LoPy', service_uuid=0x283B52D274704517BC39E852B24739EA) bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) bluetooth.advertise(True) srv1 = bluetooth.service(uuid=0x6CADF9A03F4B4A228CBD283788B9EE5C, nbr_chars=4, isprimary=True) # srv2 = bluetooth.service(uuid=0x2021, nbr_chars=3 ,isprimary=True) # chr1 = srv1.characteristic(uuid=0x283B52D274704517BC39E852B24739EA, properties=Bluetooth.PROP_READ, value=0x000000) char1_cb = chr1.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler) chr_x = srv1.characteristic(uuid=0x0CE418AAE6164546B5EE3EC89E5975D9, properties=Bluetooth.PROP_READ, value=0x000000) chr_y = srv1.characteristic(uuid=0x80725E09370E4BD0A0A7DFB7E40BD31A, properties=Bluetooth.PROP_READ, value=0x000000) chr_z = srv1.characteristic(uuid=0xF35C90EF359A4E83BF5D209F9C3F1E6D,
def conn_cb(bt_o): events = bt_o.events( ) # this method returns the flags and clears the internal registry if events & Bluetooth.CLIENT_CONNECTED: print("Client connected") elif events & Bluetooth.CLIENT_DISCONNECTED: print("Client disconnected") bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) bluetooth.advertise(True) srv1 = bluetooth.service(uuid=b'1234567890123456', isprimary=True) chr1 = srv1.characteristic(uuid=b'ab34567890123456', value=5) def char1_cb(chr): print("Write request with value = {}".format(chr.value())) char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT, handler=char1_cb) srv2 = bluetooth.service(uuid=1234, isprimary=True) chr2 = srv2.characteristic(uuid=4567)
class BluetoothApi: """Many examples integrated from https://microchipdeveloper.com/wireless:ble-gatt-data-organization https://development.pycom.io/firmwareapi/pycom/network/bluetooth/ https://github.com/sandeepmistry/arduino-BLEPeripheral https://github.com/kriswiner/nRF52832DevBoard/blob/master/BMP280_nRF52.ino https://forum.pycom.io/topic/2826/how-to-send-data-to-characteristic-over-ble/17 """ def __init__(self): self.bt = None def start(self): """ """ print('Starting Bluetooth') self.bt = Bluetooth() # Default #self.bt.init(id=0, mode=Bluetooth.BLE, antenna=Bluetooth.INT_ANT, modem_sleep=True) #self.bt.init(id=0, antenna=Bluetooth.INT_ANT, modem_sleep=False) self.bt.init(modem_sleep=False) return print('Entering main loop') while True: print('--- loop ---') adv = self.bt.get_adv() print('adv:', adv) # Give the system some breath. print('machine.idle()') machine.idle() utime.sleep(10.0) continue def scan(self, duration): """ :param duration: """ duration = int(duration) print('Starting Bluetooth scan for {} seconds'.format(duration)) self.bt.start_scan(duration) while self.bt.isscanning(): #print('is-scanning') adv = self.bt.get_adv() if adv: # try to get the complete name name_short = self.bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_SHORT) name_complete = self.bt.resolve_adv_data( adv.data, Bluetooth.ADV_NAME_CMPL) print('names:', name_short, name_complete) service_data = self.bt.resolve_adv_data( adv.data, Bluetooth.ADV_SERVICE_DATA) print('service_data:', service_data) mfg_data = self.bt.resolve_adv_data( adv.data, Bluetooth.ADV_MANUFACTURER_DATA) if mfg_data: # try to get the manufacturer data (Apple's iBeacon data is sent here) data = ubinascii.hexlify(mfg_data) print('data:', data) print('Bluetooth scanning stopped') def find_heart_rate(self): """ """ adv = self.bt.get_adv() if adv and self.bt.resolve_adv_data( adv.data, Bluetooth.ADV_NAME_CMPL) == 'Heart Rate': try: conn = self.bt.connect(adv.mac, timeout=5000) services = conn.services() for service in services: utime.sleep(0.050) if type(service.uuid()) == bytes: print('Reading chars from service = {}'.format( service.uuid())) else: print('Reading chars from service = %x' % service.uuid()) chars = service.characteristics() for char in chars: if (char.properties() & Bluetooth.PROP_READ): print('char {} value = {}'.format( char.uuid(), char.read())) conn.disconnect() except: print("Error while connecting or reading from the BLE device") else: utime.sleep(0.050) utime.sleep(10.0) def advertise(self): """https://development.pycom.io/firmwareapi/pycom/network/bluetooth/""" device_name = get_device_name() print('Advertising device {}'.format(device_name)) # TODO: Get from settings self.bt.set_advertisement(name=device_name, service_uuid=b'1234567890123456') def conn_cb(bt_o): """ :param bt_o: """ print("Callback happened") # Returns flags and clear internal registry events = bt_o.events() if events & Bluetooth.CLIENT_CONNECTED: print("Client connected:", events) elif events & Bluetooth.CLIENT_DISCONNECTED: print("Client disconnected") self.bt.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb) self.bt.advertise(True) def start_service(self): """https://development.pycom.io/firmwareapi/pycom/network/bluetooth/gattsservice/ https://development.pycom.io/firmwareapi/pycom/network/bluetooth/gattscharacteristic/ https://www.bluetooth.com/specifications/gatt/characteristics/ https://docs.python.org/3/library/struct.html The format field determines how a single value contained in the Characteristic Value is formatted. The information contained on this page is referenced in Bluetooth® Core Specification Volume 3, Part G, Section 3.3.3.5.2. https://www.bluetooth.com/specifications/assigned-numbers/format-types/ """ print('Starting service') # A. Battery Service # UUID: 180F # Abstract: The Battery Service exposes the state of a battery within a device # https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.service.battery_service.xml battery = self.bt.service(uuid=0x180F, isprimary=True, nbr_chars=1, start=True) # Battery Level # UUID: 2A19 # Abstract: The current charge level of a battery. # 100% represents fully charged while 0% represents fully discharged # Format: uint8 # https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.characteristic.battery_level.xml battery_level = battery.characteristic(uuid=0x2A19, value=78) # B. Environmental Sensing Service (ESS) # https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.service.environmental_sensing.xml # https://www.bluetooth.com/specifications/assigned-numbers/environmental-sensing-service-characteristics/ # https://github.com/sandeepmistry/arduino-BLEPeripheral/issues/215 # https://github.com/sandeepmistry/arduino-nRF5/issues/248 environment = self.bt.service(uuid=0x181A, isprimary=True, nbr_chars=5, start=True) # Temperature # UUID: 2A6E # InformativeText: Unit is in degrees Celsius with a resolution of 0.01 degrees Celsius # Format: sint16 # https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.characteristic.temperature.xml # https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.characteristic.temperature_measurement.xml # https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.characteristic.temperature_type.xml # https://github.com/ARMmbed/ble/blob/master/ble/services/HealthThermometerService.h # https://github.com/ARMmbed/ble/blob/master/ble/services/EnvironmentalService.h # Temperature Celsius # UUID: 2A1F # Format: sint16 # https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.temperature_celsius.xml self.temp = 42.42 temperature = environment.characteristic(uuid=0x2A1F) def temp_getter(chr): """ :param chr: """ print('Getting characteristic:', chr) value = sint16(self.temp * 10) print('Value:', value) self.temp += 1 return value print('Adding characteristic callback') temperature_callback = temperature.callback( trigger=Bluetooth.CHAR_READ_EVENT, handler=temp_getter) # Temperature Measurement # UUID: 2A1C # Format: Variable length structure # Value Format: FLOAT (IEEE-11073 32-bit FLOAT) # Abstract: The Temperature Measurement characteristic is a variable length structure # containing a Flags field, a Temperature Measurement Value field and, based upon the # contents of the Flags field, optionally a Time Stamp field and/or a Temperature Type field. # https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.temperature_measurement.xml print('Adding Temperature Measurement') temperature_measurement = environment.characteristic( uuid=0x2A1C, value=encode_temperature_2a1c(42.428423)) # Scientific Temperature # UUID: 2A3C # Format: float64 (IEEE-754 64-bit floating point) # https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.scientific_temperature_celsius.xml # http://forum.espruino.com/conversations/306536/ # https://stackoverflow.com/questions/36655172/how-to-read-a-ble-characteristic-float-in-swift # For the 'f', 'd' and 'e' conversion codes, the packed representation uses the IEEE 754 # binary32, binary64 or binary16 format (for 'f', 'd' or 'e' respectively), regardless of # the floating-point format used by the platform. # https://docs.python.org/3/library/struct.html #print('Adding Scientific Measurement') #temperature_sci = environment.characteristic(uuid=0x2A3C, value=float64(42.42)) # Humidity # UUID: 2A6F # InformativeText: Unit is in percent with a resolution of 0.01 percent # Format: uint16 # https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.characteristic.humidity.xml humidity1 = environment.characteristic(uuid=0x2A6F, value=uint16(86.86 * 100)) humidity2 = environment.characteristic(uuid=0x2A6F, value=uint16(50.55 * 100)) # Pressure # UUID: 2A6D # InformativeText: Unit is in pascals with a resolution of 0.1 Pa # Format: uint32 # https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.pressure.xml # Weight # UUID: 2A98 # InformativeText: Unit is in kilograms with a resolution of 0.005 # Format: uint16 # https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.weight.xml # https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.weight_measurement.xml # https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.weight_scale_feature.xml # https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.characteristic.weight.xml # https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.characteristic.weight_measurement.xml # https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.characteristic.weight_scale_feature.xml weight = environment.characteristic(uuid=0x2A98, value=uint16(25.44 * 2 * 100)) # GPS # https://forum.pycom.io/topic/5012/can-only-see-value-of-first-gatt-characteristic """ lan = bt.service(0x1819, nbr_chars=3) # 0.001 precision lan.characteristic(uuid=0x2AAE, value=int(gps.lat * 1000)) lan.characteristic(uuid=0x2AAF, value=int(gps.lon * 1000)) lan.characteristic(uuid=0x2AB3, value=int(gps.alt * 1000)) """ # Generic Access # UUID: 1800 # Abstract: The generic_access service contains generic information # about the device. All available Characteristics are readonly. # https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Services/org.bluetooth.service.generic_access.xml # Generic Attribute Service # UUID: 1801 # https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Services/org.bluetooth.service.generic_attribute.xml # String Characteristic # UUID: 2A3D # A generic UTF8 string which may be used in Services requiring strings. # https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.string.xml srv1 = self.bt.service(uuid=BluetoothUuid.to_bytes_le( '15ECCA29-0B6E-40B3-9181-BE9509B53200'), isprimary=True, nbr_chars=2, start=True) #generic_attribute = self.bt.service(uuid=0x1800, isprimary=True, nbr_chars=2, start=True) #wifi_ssid = generic_attribute.characteristic(uuid=0x2A3D, properties=Bluetooth.PROP_BROADCAST | Bluetooth.PROP_INDICATE | Bluetooth.PROP_NOTIFY | Bluetooth.PROP_READ | Bluetooth.PROP_WRITE, value="default") #wifi_ssid = generic_attribute.characteristic(uuid=0x2A3D, properties=Bluetooth.PROP_NOTIFY | Bluetooth.PROP_READ | Bluetooth.PROP_WRITE, value="default") wifi_ssid = srv1.characteristic(uuid=0x2A3D) #wifi_password = generic_attribute.characteristic(uuid=0x2A3D, properties=Bluetooth.PROP_READ | Bluetooth.PROP_WRITE) #print('wifi_ssid:', wifi_ssid, dir(wifi_ssid)) #cfg = wifi_ssid.config() #print('cfg:', cfg) #wifi_ssid.config(name='hallodri') print('wifi_ssid:', wifi_ssid) print('wifi_ssid:', dir(wifi_ssid)) def wifi_callback(characteristic): """ :param characteristic: """ print('Characteristic callback:', characteristic) #print('attr_obj:', characteristic.attr_obj) events = characteristic.events() if events & Bluetooth.CHAR_WRITE_EVENT: print("Write request with value = {}".format( characteristic.value())) else: pass value = characteristic.value() print('Reading characteristic:', value) print('Adding characteristic callback') wifi_ssid_callback = wifi_ssid.callback( trigger=Bluetooth.CHAR_READ_EVENT | Bluetooth.CHAR_WRITE_EVENT, handler=wifi_callback) #wifi_password_callback = wifi_password.callback(trigger=Bluetooth.CHAR_READ_EVENT | Bluetooth.CHAR_WRITE_EVENT, handler=wifi_callback) #print('instance:', wifi_ssid.instance()) # Characteristic User Description # UUID: 0x2901,0x2902 print(wifi_ssid_callback) print(dir(wifi_ssid_callback)) srv1.characteristic(uuid=0x2901, value="hello world 1")