Package: losant-mqtt
TheLosantMQTT 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 thePaho MQTT Clientunder the
covers for the actual MQTT communication.InstallationThe latest stable version is available in the Python Package Index (PyPi)
and can be installed usingpip install losant-mqttExampleBelow 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 DocumentationDeviceconstructorconnectis_connectedclosesend_stateloopadd_event_observerremove_event_observerDeviceA 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.constructorDevice(device_id, key, secret, secure=True, transport="tcp")TheDevice()constructor takes the following arguments:device_idThe device’s ID. Obtained by first registering a device using the Losant platform.keyThe Losant access key.secretThe Losant access secret.secureIf the client should connect to Losant over SSL - default is true.transportAllowed 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).Examplefrom losantmqtt import Device

device = Device("my-device-id", "my-app-access-key", "my-app-access-secret")connectconnect(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:blockingIf 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 theloopmethod periodically.is_connectedis_connected()Returns a boolean indicating whether or not the device is currently connected
to the Losant platform.closeclose()Closes the device’s connection to the Losant platform.send_statesend_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.stateThe state to send as a Dict.time_likeWhen the state occurred - if None or not set, will default to now.Exampledevice.send_state({ "voltage": read_analog_in() })looploop(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.timeoutMax time to block on the socket before continuing - defaults to 1 second.add_event_observeradd_event_observer(event_name, observer)Adds an observer to listen for an event on this device.event_nameThe 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).observerCallback 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.Exampledef 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_observerremove_event_observer(event_name, observer)Removes an observer from listening for an event on this device.event_nameThe event to stop listening for.  Same events asadd_event_observer.observerCallback method to remove.Copyright (c) 2019 Losant IoT, Inchttps://www.losant.com