smartgadget.service module

Base class for a Smart Gadget Service.

class smartgadget.service.SmartGadgetService(cls, interface=None)[source]

Bases: Service

Base class for a Smart Gadget Service.

Parameters
  • cls – A SHT3XService or a SHTC1Service class type.

  • interface (int, optional) – The Bluetooth interface to use for the connection. For example, 0 or None means /dev/hci0, 1 means /dev/hci1.

battery(mac_address) int[source]

Returns the battery level for the specified MAC address.

Parameters

mac_address (str) – The MAC address of the Smart Gadget.

Returns

int – The battery level [%].

connect_gadget(mac_address, strict=True) bool[source]

Connect to the specified Smart Gadget.

It is not necessary to call this method to connect to a Smart Gadget via Bluetooth before fetching data from it. The Bluetooth connection will automatically be created and destroyed when requesting information from the Smart Gadget if the Bluetooth connection does not already exist.

Establishing a Bluetooth connection to a Smart Gadget takes approximately 7 seconds. If you are only requesting data from a couple of Smart Gadgets then connecting to each Smart Gadget at the beginning of your script and then fetching data in a loop would be more efficient if you want to fetch data as quickly as possible. However, there are hardware limits to how many Smart Gadgets can simultaneously have a Bluetooth connection with the Raspberry Pi. So, there is a compromise between how quickly your program can fetch data and how many Smart Gadgets you want to fetch data from.

Parameters
  • mac_address (str) – The MAC address of the Smart Gadget to connect to.

  • strict (bool, optional) – Whether to raise an error if the Smart Gadget could not be connected to.

Returns

bool – Whether the connection was successful.

connect_gadgets(mac_addresses, strict=True) Tuple[list, list][source]

Connect to the specified Smart Gadgets.

See connect_gadget() for more details.

Parameters
  • mac_addresses (list of str) – A list of MAC addresses of the Smart Gadgets to connect to.

  • strict (bool, optional) – Whether to raise an error if a Smart Gadget could not be connected to.

Returns

tuple of list – A list of MAC addresses of the Smart Gadgets that were successfully connected to and the MAC addresses of the Smart Gadgets that could not be connected to.

connected_gadgets() List[str][source]

Returns the MAC addresses of the Smart Gadgets that are currently connected.

Returns

list of str – The MAC addresses of the currently-connected Smart Gadgets.

dewpoint(mac_address, temperature=None, humidity=None) float[source]

Returns the dew point for the specified MAC address.

Parameters
  • mac_address (str) – The MAC address of the Smart Gadget.

  • temperature (float, optional) – The temperature [degree C]. If None then reads the current temperature value from the Smart Gadget.

  • humidity (float, optional) – The humidity [%RH]. If None then reads the current humidity value from the Smart Gadget.

Returns

float – The dew point [degree C].

disconnect_gadget(mac_address)[source]

Disconnect the Smart Gadget with the specified MAC address.

Parameters

mac_address (str) – The MAC address of the Smart Gadget to disconnect from.

disconnect_gadgets()[source]

Disconnect from all Smart Gadgets.

humidity(mac_address) float[source]

Returns the current humidity for the specified MAC address.

Parameters

mac_address (str) – The MAC address of the Smart Gadget.

Returns

float – The humidity [%RH].

info(mac_address) dict[source]

Returns all available information from the Smart Gadget.

Parameters

mac_address (str) – The MAC address of the Smart Gadget.

Returns

dict – Includes information such as the firmware, hardware and software version numbers, the battery level, the temperature, humidity and dew point values and the timing information about the data logger (if the Smart Gadgets supports logging).

max_attempts() int[source]

Returns the maximum number of times to try to connect or read/write data from/to a Smart Gadget.

Returns

int – The maximum number of times to retry.

restart_bluetooth()[source]

Restart the Bluetooth driver on the Raspberry Pi.

This can fix scanning issues or connection timeouts.

Attention

Calling this method will disconnect all Smart Gadgets that are currently connected to the Raspberry Pi.

static rpi_date() str[source]

Returns the current date of the Raspberry Pi.

Returns

str – The current date of the Raspberry Pi in the ISO-8601 format.

rssi(mac_address) int[source]

Returns the Received Signal Strength Indication (RSSI) for the last received broadcast from the device.

This is an integer value measured in dB, where 0 dB is the maximum (theoretical) signal strength, and more negative numbers indicate a weaker signal.

Parameters

mac_address (str) – The MAC address of the Smart Gadget.

Returns

int or None – The RSSI value if the SmartGadget was initialized with a ScanEntry object. Otherwise returns None.

scan(timeout=10, passive=False) List[str][source]

Scan for Smart Gadgets that are within Bluetooth range.

Parameters
  • timeout (float, optional) – The number of seconds to scan for Smart Gadgets.

  • passive (bool, optional) – Use active (to obtain more information when connecting) or passive scanning.

Returns

list of str – A list of MAC addresses of the Smart Gadgets that are available for this particular SHTxx class.

set_max_attempts(max_attempts)[source]

Set the maximum number of times to try to connect or read/write data from/to a Smart Gadget.

Since a Bluetooth connection can drop unexpectedly, this provides the opportunity to automatically re-connect or re-send a request to a Smart Gadget.

Parameters

max_attempts (int) – The maximum number of times to try to connect or read/write data from/to a Smart Gadget. Increasing the number of attempts will decrease the occurrence of getting a BTLEDisconnectError or a BrokenPipeError when sending requests, but may make sending a request take a long time while the connection automatically tries to be re-established.

static set_rpi_date(date)[source]

Set the date of the Raspberry Pi.

This is useful if the Raspberry Pi does not have internet access on startup to sync with an online NTP server. Does not set the time zone.

Parameters

date – Can be a datetime object, an ISO-8601 formatted str, a float in seconds, or an int in milliseconds.

shutdown_service()[source]

Shutdown the Smart Gadget Service and the Network Manager.

temperature(mac_address) float[source]

Returns the current temperature for the specified MAC address.

Parameters

mac_address (str) – The MAC address of the Smart Gadget.

Returns

float – The temperature [degree C].

temperature_humidity(mac_address) Tuple[float, float][source]

Returns the current temperature and humidity for the specified MAC address.

Parameters

mac_address (str) – The MAC address of the Smart Gadget.

Returns

  • float – The temperature [degree C].

  • float – The humidity [%RH].

temperature_humidity_dewpoint(mac_address) Tuple[float, float, float][source]

Returns the current temperature, humidity and dew point for the specified MAC address.

Parameters

mac_address (str) – The MAC address of the Smart Gadget.

Returns

  • float – The temperature [degree C].

  • float – The humidity [%RH].

  • float – The dew point [degree C].