Problem In connecting the sensor with losant

#1

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: https://gpiozero.readthedocs.io/en/stable/
from time import sleep
from losantmqtt import Device # Import Losant library: https://github.com/Losant/losant-mqtt-python
# 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])

        time.sleep(0.5)
        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
device.connect(blocking=True)

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

#2

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.

#3

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 losant1.py
Listening for device commands
Traceback (most recent call last):
File “losant1.py”, line 46, in
device.connect(blocking=True)
File “/usr/local/lib/python2.7/dist-packages/losantmqtt/device.py”, line 173, in connect
self._mqtt_client.loop_forever()
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 1481, in loop_forever
rc = self.loop(timeout, max_packets)
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 1003, in loop
rc = self.loop_read(max_packets)
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 1284, in loop_read
rc = self._packet_read()
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 1849, in _packet_read
rc = self._packet_handle()
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 2311, in _packet_handle
return self._handle_connack()
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 2372, in _handle_connack
self.on_connect(self, self._userdata, flags_dict, result)
File “/usr/local/lib/python2.7/dist-packages/losantmqtt/device.py”, 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