[Solved] Python Test and is not working

Application ID:
5aa6a02d0d919d00076d8254

Device ID:
5aa6a055f45d3f00075f9a17

With the following test program I am see nothing in the Application Log…

import time
import random
from losantmqtt import Device

device = Device("5aa6a055f45d3f00075f9a17", "access-key", "access-secret")

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

device.add_event_observer("command", on_command)

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

#Send Voltage reading every second.
while True:
  device.loop()
  if device.is_connected():
    temp = random.random() * 100
    #print("temp")
    device.send_state({"temp": temp})
  time.sleep(1)

If I add device.connect(blocking=True) to the end of the program the Device , then Application log show “Authentication succeeded” and “Connection succeeded” and the device is connected but nothing else.

I am using Python3.6.4

Please help me troubleshoot this.

Bala,

I actually took your exact code above, and using my own deviceId, access key, and secret, the code connected and worked as expected. I’m using python 3.6.4, with losant-mqtt version 1.0.1 and paho-mqtt version 1.3.1. So I’m not sure why you are running into issues - could you give some more details?

Thanks Michael for your prompt response.

Pardon my ignorance as this is my first attempt with Losant.

While using a simulator, things are populated on the graph… but if I run this python program as is , nothing shows up in the application log ( all empty ) and no values whatsoever. Not even connection details.

Where do I see the logs,

All I have is a device ( with one Attribute Temp as Number) , Application and History Block dashboard. No workflows. Am I missing something here ?

Thanks again for your help and is much appreciated.

So this is a long shot - but when I did copy paste your code, I fixed some formatting errors that I thought were just a result of the code being pasted into the forums (I edited the code sample in your post to fix them). Everything was indented an extra few spaces from the add_event_observer line on down - which meant they were all ‘part’ of the on_command function (since python is whitespace sensitive). Could you double check your code and make sure that is not the case with your original code? Because that would explain why the device never connects - the device.connect line wouldn’t actually run in that case.

If that isn’t the issue, I’ll work up a code sample with some logging added that you can try, and we can see what the output is.

Thanks Michael for your tips. It was indeed white spaces and it is working now. Thanks again.

Great! Glad I could help.