def send_temp(count=50): """Sends out a fake temperature twice a second.""" with nrf as ble: ble.name = b"nRF24L01" print("available bytes in next payload:", ble.available(chunk(temperature_service.buffer))) for i in range(count): if ble.available(chunk(temperature_service.buffer)) >= 0: _prompt(count, i) # broadcast a temperature measurement; 0x16 means service data ble.advertise(temperature_service.buffer, data_type=0x16) ble.hop_channel() time.sleep(0.2)
def send_url(count=50): """Sends out a URL twice a second.""" with nrf as ble: print("available bytes in next payload:", ble.available(chunk(url_service.buffer))) # NOTE we did NOT set a device name in this with block for i in range(count): # URLs easily exceed the nRF24L01's max payload length if ble.available(chunk(url_service.buffer)) >= 0: _prompt(count, i) ble.advertise(url_service.buffer, 0x16) ble.hop_channel() time.sleep(0.2)
def master(count=50): """Sends out the device information twice a second.""" # using the "with" statement is highly recommended if the nRF24L01 is # to be used for more than a BLE configuration with nrf as ble: ble.name = b"nRF24L01" # include the radio's pa_level attribute in the payload ble.show_pa_level = True print("available bytes in next payload:", ble.available(chunk(battery_service.buffer)) ) # using chunk() gives an accurate estimate of available bytes for i in range(count): # advertise data this many times if ble.available(chunk(battery_service.buffer)) >= 0: _prompt(count, i) # something to show that it isn't frozen # broadcast the device name, MAC address, & # battery charge info; 0x16 means service data ble.advertise(battery_service.buffer, data_type=0x16) # channel hoping is recommended per BLE specs ble.hop_channel() time.sleep(0.5) # wait till next broadcast