bledev.environmental

Environmental Sensing Service (0x181A): temperature, humidity, pressure and UV index.

Serve the readings your board has and update them as they change:

from bledev.environmental import EnvironmentalSensing

weather = EnvironmentalSensing(temperature=True, humidity=True, pressure=True)
ble.register_services(weather.service)
connection = await ble.advertise(name="Weather", services=[weather.SERVICE])
weather.update(temperature=21.5, humidity=40.2, pressure=101325.0)

Each reading is its own characteristic, which a central can read or subscribe to. Units are degrees Celsius, percent relative humidity, pascals and the UV index. The service converts them to the specification’s fixed-point formats: temperature in 0.01 degrees, humidity in 0.01 percent and pressure in 0.1 pascal. A reading you haven’t set yet reads “not known”, as the specification defines it. update(temperature=None) leaves that reading alone.

ble.register_services() replaces whatever was registered before, so pass this service together with the others. Any peripheral backend serves it: MicroPython (bledev.mpble), CircuitPython (bledev.cpble) and the fake one for tests.

bledev.environmental.encode_temperature(celsius)[source]

Temperature as sint16 in 0.01 degrees Celsius.

bledev.environmental.encode_humidity(percent)[source]

Relative humidity as uint16 in 0.01 percent.

bledev.environmental.encode_pressure(pascals)[source]

Pressure as uint32 in 0.1 pascal.

bledev.environmental.encode_uv_index(index)[source]

The UV index as uint8.

class bledev.environmental.EnvironmentalSensing(temperature=False, humidity=False, pressure=False, uv_index=False)[source]

Bases: object

An Environmental Sensing Service.

Parameters:
  • temperature – True to serve Temperature.

  • humidity – True to serve Humidity.

  • pressure – True to serve Pressure.

  • uv_index – True to serve UV Index.

characteristics

The served readings’ bledev.Characteristic, by name.

update(temperature=None, humidity=None, pressure=None, uv_index=None)[source]

Set readings and notify the centrals subscribed to them.

Parameters:
  • temperature – Degrees Celsius.

  • humidity – Percent relative humidity.

  • pressure – Pascals (101325 is a standard atmosphere).

  • uv_index – The UV index.