Problem In connecting the sensor with losant


Hey I have been working with mentioned below code for temperature and humidity monitoring and using Chip name SHT31 taking help from library

import json
import smbus
import time
from gpiozero import LED, Button # Import GPIO library:
from time import sleep
from losantmqtt import Device # Import Losant library:
# Get I2C bus
bus = smbus.SMBus(1)

# Construct Losant device
device = Device(" DEVICE_ID", "ACCESS_KEY", "ACCESS_SECRET")

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

    # Listen for the gpioControl. This name configured in Losant
    if command["name"] == "toggle":
       # SHT31 address, 0x44(68)
       # Send measurement command, 0x2C(44)
       # 0x06(06)	High repeatability measurement
       bus.write_i2c_block_data(0x44, 0x2C, [0x06])

        data = bus.read_i2c_block_data(0x44, 0x00, 6)

    # Convert the data
    temp = data[0] * 256 + data[1]
    cTemp = -45 + (175 * temp / 65535.0)
    fTemp = -49 + (315 * temp / 65535.0)
    humidity = 100 * (data[3] * 256 + data[4]) / 65535.0

def sendDeviceState():
    print("Temperature in Celsius is : %.2f C" %cTemp)
    print ("Temperature in Fahrenheit is : %.2f F" %fTemp)
    print ("Relative Humidity is : %.2f %%RH" %humidity)
    device.send_state({"button": True})

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

  print("Listening for device commands")

# Connect to Losant and leave the connection open

But I am new to python code so need help here so that I will be able to connect the my sensor interfacing with raspberry will also connect with phone through MQTT service

Your help will be much appreciated


First things first - please do not post sensitive information such as your access key and secret on our public forum. I edited your original post to remove them, but since they were exposed for a couple hours, you should discard that key / secret and generate a new one as anybody who may have seen this post before the edit could connect to our broker as your device using those credentials.

Second, where exactly are you getting stuck now? Are you unable to connect the device to the broker? Is it connecting but not sending state? Are commands not being processed?

I did notice that your device ID had a space at the beginning of the string; that could be the cause of a failure to connect if that is the issue.


Hey I really apologize for that,
And Yes the I am not able to connect device to broker, During working on my sensor and getting some idea according to losant sample code for raspberry pi, I feel that there are some mistakes which I am doing while modifying the code, I am facing the quoted errors while executing the program

pi@raspberrypi:~/Desktop $ python
Listening for device commands
Traceback (most recent call last):
File “”, line 46, in
File “/usr/local/lib/python2.7/dist-packages/losantmqtt/”, line 173, in connect
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/”, line 1481, in loop_forever
rc = self.loop(timeout, max_packets)
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/”, line 1003, in loop
rc = self.loop_read(max_packets)
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/”, line 1284, in loop_read
rc = self._packet_read()
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/”, line 1849, in _packet_read
rc = self._packet_handle()
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/”, line 2311, in _packet_handle
return self._handle_connack()
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/”, line 2372, in _handle_connack
self.on_connect(self, self._userdata, flags_dict, result)
File “/usr/local/lib/python2.7/dist-packages/losantmqtt/”, line 249, in _cb_client_connect
raise Exception(“Invalid Losant credentials - error code {0}”.format(response_code))
Exception: Invalid Losant credentials - error code 5"

This Is my first ever try to program the library with internet cloud and I in apologize advance if somethings are not upto the mark