def blueGo(u0): # p = Peripheral() # p.advertise(device_name=name) def event_handler(id, handle, data): print("BLE event:", id, "handle:", handle) print(data) if id == constants.EVT_GAP_CONNECTED: Dragos.colorwheel() elif id == constants.EVT_GAP_DISCONNECTED: print("Disconnect") # disconnect #LED(2).off() #u0 = UUID("6e400001-b5a3-f393-e0a9-e50e24dcca9e") #u0 = UUID(encodeName(badge)) u1 = UUID("6e400002-b5a3-f393-e0a9-e50e24dcca9e") u2 = UUID("6e400003-b5a3-f393-e0a9-e50e24dcca9e") s = Service(u0) c0 = Characteristic(u1, props=Characteristic.PROP_WRITE | Characteristic.PROP_WRITE_WO_RESP) c1 = Characteristic(u2, props=Characteristic.PROP_NOTIFY, attrs=Characteristic.ATTR_CCCD) s.addCharacteristic(c0) s.addCharacteristic(c1) p = Peripheral() p.addService(s) p.setConnectionHandler(event_handler) p.advertise(device_name="Dragos", services=[s])
def event_handler(id, handle, data): global periph global service if id == constants.EVT_GAP_CONNECTED: pass elif id == constants.EVT_GAP_DISCONNECTED: # restart advertisment periph.advertise(device_name="Nano Blinky", services=[service]) elif id == constants.EVT_GATTS_WRITE: LED(1).on() if int(data[0]) else LED(1).off() # start off with LED(1) off LED(1).off() notif_enabled = False uuid_service = UUID("0x1523") uuid_led = UUID("0x1525") service = Service(uuid_service) char_led = Characteristic(uuid_led, props=Characteristic.PROP_WRITE) service.addCharacteristic(char_led) periph = Peripheral() periph.addService(service) periph.setConnectionHandler(event_handler) periph.advertise(device_name="Nano Blinky", services=[service]) while (True): time.sleep_ms(500)
# measure chip temperature temp = Temp.read() temp = temp * 100 char_temp.write(bytearray([temp & 0xFF, temp >> 8])) # start off with LED(1) off LED(1).off() # use RTC1 as RTC0 is used by bluetooth stack # set up RTC callback every 5 second rtc = RTC(1, period=5, mode=RTC.PERIODIC, callback=send_temp) notif_enabled = False uuid_env_sense = UUID("0x181A") # Environmental Sensing service uuid_temp = UUID("0x2A6E") # Temperature characteristic serv_env_sense = Service(uuid_env_sense) temp_props = Characteristic.PROP_NOTIFY | Characteristic.PROP_READ temp_attrs = Characteristic.ATTR_CCCD char_temp = Characteristic(uuid_temp, props=temp_props, attrs=temp_attrs) serv_env_sense.addCharacteristic(char_temp) periph = Peripheral() periph.addService(serv_env_sense) periph.setConnectionHandler(event_handler) periph.advertise(device_name="micr_temp", services=[serv_env_sense])
else: notif_enabled = False # start off with LED(1) off LED(1).off() notif_enabled = False uuid_service = UUID("0x181A") # Environmental Sensing service uuid_temp = UUID("0x2A6E") # Temperature characteristic service = Service(uuid_service) temp_props = Characteristic.PROP_READ | Characteristic.PROP_NOTIFY temp_attrs = Characteristic.ATTR_CCCD temp_char = Characteristic(uuid_temp, props=temp_props, attrs=temp_attrs) service.addCharacteristic(temp_char) periph = Peripheral() periph.addService(service) periph.setConnectionHandler(event_handler) periph.advertise(device_name="Temperature Sensor", services=[service]) bus = I2C(1, scl=Pin(15), sda=Pin(14)) hts = hts221.HTS221(bus) while (True): if notif_enabled: temp = int(hts.temperature() * 100) temp_char.write(bytearray([temp & 0xFF, temp >> 8])) time.sleep_ms(100)