Issue sending data to MQTT

Hello,
I have added a MQTT and I am testing it via a Python application on my MacMini which makes use paho library.
If I look at Live inspector it seems that I am able to connect but anything I publish does not appear.
I took a Python code similar to the one cited in 1 of the examples:import paho.mqtt.client as mqtt

import paho.mqtt.client as mqtt
import random
import json

# Definitions
# put here your device token
device_token = 'TeslaMate'

broker = "mqtt.tago.io"
broker_port = 8883
mqtt_keep_alive = 60

# MQTT publish topic must be tago/data/post
mqtt_publish_topic = "temperature"

# put any name here, TagoIO doesn't validate this username.
mqtt_username = 'tagoio'

# MQTT password must be the device token (TagoIO does validate this password)
mqtt_password =<my token>

# Callback - MQTT broker connection is on


def on_connect(client, userdata, flags, rc):
    print("[STATUS] Connected to MQTT broker. Result: " + str(rc))


def on_publish():
    print("Message published")
# Main program


print("[STATUS] Initializing MQTT...")
client = mqtt.Client()
client.username_pw_set(mqtt_username, mqtt_password)
client.tls_set_context()
client.on_connect = on_connect
x = client.connect(broker, broker_port, mqtt_keep_alive)

# Generate a random temperature to send to TagoIO (from 32 to 86F)
temperature = random.randint(32,86)
txt_temperature="Random temperature generated: {rand_temp}F"
print(txt_temperature.format(rand_temp=temperature) )

# Format data into JSON
temperature_json = {"variable": "temperature", "unit": "F", "value": temperature}
temperature_json_string = json.dumps(temperature_json)

# Send data to TagoIO using MQTT
y = client.publish(mqtt_publish_topic, temperature_json_string)
print(y.rc)
print(y.is_published())
print("Data sent to TagoIO platform")

What I see is on pYthon console is:[STATUS] Initializing MQTT…

Random temperature generated: 78F
0
True
Data sent to TagoIO platform

so the the method is_published() returns true. However the Live inspector returns :

Dear all,

I am facing the same issue that @maurizios63 has posted, any idea of what could solve this

Thanks in advance!@

Hi Guys,

Did you create the Action MQTT?
By default, the TagoIO accepts data automatically when use topic tago/data/post. If you are using another topic, like temperature, you’ll need to create an Action MQTT.

Thx,
Fabiano.

It does look like the data is not being published to TagoIO.

I’m not sure, but it can be an issue with MQTT version. Are you able to set the MQTT version with the library you’re using?

Hi everybody,
thanks for the suggestions. I tried to implement all the suggestions you provided and ow it seems to be working.
As indicated above I am testing the protocol with a simple Python script using MQTT Paho library. In fact the Client constructor allows to specify protocol version:
client = mqtt.Client(protocol=mqtt.MQTTv311)

It looks like TagoIo is working with v31, v31.1 nut not v5 (the ones supported by paho)
Also I have added an action as well (in fact once you specified an MQTT action there seems to be no need to specify a trigger as well. The action is triggered when something the specified is published.

Thanks for everybody; this allows me to go next step. In fact my plan is to use TagoIo to display data from Telsamate (the application that allows user to get data from a Tesla vehicle) and implements a MQTT client. Let’s see if it works!

Maurizio

1 Like

Dear all,

I have tried to publish using the paho on python using the default topic tago/data/post and even creating a topic as temperature, the data never arrives to the Device Inspector, I had changed the protocol version as @maurizios63 did, but no success.

So, in order to test using another tool, I have used the MQTTFX, and by there I could see data arriving to the inspector.

Would you suggest me another approach or test that I could perform in order to solve this?

Thanks in advance.

Gustavo

Hi @gustavo.engto,
Do you see your device connecting in the Live Inspector at least?

Hi @vitor

Yes, I can see the connection the same way in the original question on this topic.
image

Therefore, when I actualy publish the data the publish event does not happen, I already changed the MQTT protocol version, the topic, but no success.

Thanks a lot

Gustavo

I’ll do this integration here by myself and check if I do have success. I’ll get back you soon this week with the instructions.

1 Like

What I could notice is that the publishing function is running before the paho is actually connected to the broker. It seems you need to add a loop in the code in order to check if Paho is connected before sending your publish message.

What I found to be working is the following code:

import paho.mqtt.client as mqtt
import random
import json

# Definitions
# put here your device token
device_token = 'TeslaMate'

broker = "mqtt.tago.io"
broker_port = 1883
mqtt_keep_alive = 60

# MQTT publish topic must be tago/data/post
mqtt_publish_topic = "test"

# put any name here, TagoIO doesn't validate this username.
mqtt_username = 'tagoio'

# MQTT password must be the device token (TagoIO does validate this password)
mqtt_password ='device-token'

# Callback - MQTT broker connection is on


def on_connect(client, userdata, flags, rc):
    print("[STATUS] Connected to MQTT broker. Result: " + str(rc))
  # Generate a random temperature to send to TagoIO (from 32 to 86F)
    temperature = random.randint(32,86)
    txt_temperature="Random temperature generated: {rand_temp}F"
    print(txt_temperature.format(rand_temp=temperature) )

    # Format data into JSON
    temperature_json = {"variable": "temperature", "unit": "F", "value": temperature}
    temperature_json_string = json.dumps(temperature_json)

    # Send data to TagoIO using MQTT
    y = client.publish(mqtt_publish_topic, temperature_json_string)
    print(y.rc)
    print(y.is_published())
    print("Data sent to TagoIO platform")
    client.disconnect();
    client.loop_stop()

def on_publish(client, userdata, result):
    print("Message published")
# Main program

def on_message(client, userdata, message):
    print("Received message '" + str(message.payload) + "' on topic '"
        + message.topic + "' with QoS " + str(message.qos))

print("[STATUS] Initializing MQTT...")
client = mqtt.Client(protocol=mqtt.MQTTv31)
client.username_pw_set(mqtt_username, mqtt_password)
# client.tls_set_context()
client.on_connect = on_connect
client.on_publish = on_publish
client.on_message = on_message

x = client.connect(broker, broker_port)

try:
    client.loop_forever()
except KeyboardInterrupt:
    pass

client.disconnect()
client.loop_stop()
1 Like

Dear Vitor,

Thank you so much for your help, now it has worked! The problem really was the publish was running early the connection.

Best regards!

Gustavo

1 Like