Skip to main content

travis-badge pypi-badge

The Losant MQTT client provides a simple way for custom things to communicate with the Losant platform over MQTT. You can authenticate as a device, publish device state, and listen for device commands.

This client works with both Python 2.7 and 3. It uses the Paho MQTT Client under the covers for the actual MQTT communication.

Installation

The latest stable version is available in the Python Package Index (PyPi) and can be installed using

pip install losant-mqtt

Example

Below is a high-level example of using the Losant Python MQTT client to send the value of a temperature sensor to the Losant platform.

import time
from losantmqtt import Device

# Construct device
device = Device("my-device-id", "my-app-access-key", "my-app-access-secret")

def on_command(device, command):
    print("Command received.")
    print(command["name"])
    print(command["payload"])

# Listen for commands.
device.add_event_observer("command", on_command)

# Connect to Losant.
device.connect(blocking=False)

# Send temperature once every second.
while True:
    device.loop()
    if device.is_connected():
        temp = call_out_to_your_sensor_here()
        device.send_state({"temperature": temp})
    time.sleep(1)

API Documentation

Device

A device represents a single thing or widget that you’d like to connect to the Losant platform. A single device can contain many different sensors or other attached peripherals. Devices can either report state or respond to commands.

A device’s state represents a snapshot of the device at some point in time. If the device has a temperature sensor, it might report state every few seconds with the temperature. If a device has a button, it might only report state when the button is pressed. Devices can report state as often as needed by your specific application.

Commands instruct a device to take a specific action. Commands are defined as a name and an optional payload. For example, if the device is a scrolling marquee, the command might be “update text” and the payload would include the text to update.

constructor

Device(device_id, key, secret, secure=True, transport="tcp")

The Device() constructor takes the following arguments:

device_id

The device’s ID. Obtained by first registering a device using the Losant platform.

key

The Losant access key.

secret

The Losant access secret.

secure

If the client should connect to Losant over SSL - default is true.

transport

Allowed values are “tcp” and “websockets”. Defaults to “tcp”, which is a raw TCP connection over ports 1883 (insecure) or 8883 (secure). When “websockets” is passed in, connects using MQTT over WebSockets, which uses either port 80 (insecure) or port 443 (secure).

Example
from losantmqtt import Device

device = Device("my-device-id", "my-app-access-key", "my-app-access-secret")

connect

connect(blocking=True)

Connects the device to the Losant platform. Hook the connect event to know when a connection has been successfully established. Connect takes the following arguments:

blocking

If the connect method should block or not. True is the default, which means that the connect call will be a blocking call that will not return until the connection is closed or an error occurs - all interaction has to be done through the various event callbacks. If blocking is set to False, the function will only block until the connection is kicked off - after that point you must run the network loop yourself, by calling the loop method periodically.

is_connected

is_connected()

Returns a boolean indicating whether or not the device is currently connected to the Losant platform.

close

close()

Closes the device’s connection to the Losant platform.

send_state

send_state(state, time_like=None)

Sends a device state to the Losant platform. In many scenarios, device states will change rapidly. For example a GPS device will report GPS coordinates once a second or more. Because of this, sendState is typically the most invoked function. Any state data sent to Losant is stored and made available in data visualization tools and workflow triggers.

state

The state to send as a Dict.

time_like

When the state occurred - if None or not set, will default to now.

Example
device.send_state({ "voltage": read_analog_in() })

loop

loop(timeout=1)

Loops the network stack for the connection. Only valid to call when connected in non-blocking mode. Be sure to call this reasonably frequently when in that model to make sure the underlying MQTT connection does not get timed out.

timeout

Max time to block on the socket before continuing - defaults to 1 second.

add_event_observer

add_event_observer(event_name, observer)

Adds an observer to listen for an event on this device.

event_name

The event to listen for. Possible events are: “connect” (the device has connected), “reconnect” (the device lost its connection and reconnected), “close” (the device’s connection was closed), and “command” (the device has received a command from Losant).

observer

Callback method to call when the given event fires. The first argument for all callbacks will be the device instance. Command callbacks have a second argument - the command received.

Example
def on_command(device, cmd):
    print(cmd["time"]) # time of the command
    print(cmd["name"]) # name of the command
    print(cmd["payload"]) # payload of the command

device.add_event_observer("command", on_command)

remove_event_observer

remove_event_observer(event_name, observer)

Removes an observer from listening for an event on this device.

event_name

The event to stop listening for. Same events as add_event_observer.

observer

Callback method to remove.

Copyright (c) 2024 Losant IoT, Inc

https://www.losant.com

Release files for losant-mqtt 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for losant-mqtt 1.2.1
File Size Uploaded
losant-mqtt-1.2.1.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for losant-mqtt 1.2.1
File Interpreter ABI Platform
losant_mqtt-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.1 kB

Release files / losant-mqtt-1.2.1.tar.gz

Download URL losant-mqtt-1.2.1.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e73c93f75aa5b3944a8d8e97196bec5fce873ceb79a28cb5857274e642902a8f
BLAKE2b-256 checksum
How to use checksums
bb3dc56c4898e5fdcf0a838b545f8a8705ae22a02aa11bb97a80178dd6a798c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release files / losant_mqtt-1.2.1-py3-none-any.whl

Download URL losant_mqtt-1.2.1-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a824bb66f43d4b3b8ec36323492e1c07f9bd6db4ba81e024d16ebbb19a0b67e6
BLAKE2b-256 checksum
How to use checksums
ee85dd9d3c177f5604b5e8b0808bac57fe058121ddfc19d9d79307da18a93f91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

4 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page