Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] Questio paho mqtt <-> mosquitto Python

Hi,

on a quick look, I would bet on a too early exit of your script.

When calling publish it process the messages asynchronously, so you need
to wait to the corresponding on_publish callback to be sure the message
is delivered.

If your csv is not too big, the easiest way is probably to use
publish.multiple helper that publish messages to a broken and disconnect
cleanly.

Regards,

PierreF

Le 20/06/2017 à 11:24, Michael Post a écrit :
> Hello,
>
> i have a py script to read a csv file and send the content row-per-row
> to the mosquitto broker.
>
> Below you see the source (only for structure. The file as is is not
> working.)
>
>
> My effect is, that when i subscribe via
>
> 	moquitto_sub -t "XXXX/YYYYYY"
>
> i got no values. The connection for subscribing was established without
> errors.
>
> The publishing does not work and i did not have any clue why.
>
> Do you have any hint for me to get it working?
>
> Thanks a lot,
>
> Michael
>
>
> #!/usr/bin/env python3
>
> import paho.mqtt.client as mqtt
> import os
> import subprocess
> import glob
> import datetime
>
>
>
> # MQTT parameter
> MQTT_HOST="10.0.1.94"
> MQTT_PORT=1883
> MQTT_TOPIC="XXXX/YYYYYY"
> MQTT_CLIENT_ID="jsakdlfsjdaf_sadfsafas"
> MQTT_QOS=2
>
>
>
>
> # This is the MQTT Broker Subscriber
> def on_connect(client, userdata, flags, rc):
> 	print("Connected with result code " + str(rc))
> 	client.subscribe(MQTT_TOPIC)
> 		
> def on_publish(client,userdata,result):             #create function for
> callback
>     print("data published \n")
>
>
> # Establish connection to mqtt broker
> client = mqtt.Client(MQTT_CLIENT_ID, False)
> client.on_publish = on_publish
> client.on_connect = on_connect
> client.connect(MQTT_HOST,MQTT_PORT,60)
>
> client.loop_start()
>
> 		
> 		# Get tracker serial
> 		tracker="adbc"
> 		
> filename="abc.csv"
>
> 		
> 		f = open(filename + ".csv", "rU")
> 		
> 		for line in f:
> 			
> 			client.publish(MQTT_TOPIC, '"Hello World 123. Yes it works. A"' +
> str(datetime.datetime.now()), MQTT_QOS, False)
>
>
> 				i_data = i_data+1
> 				print(str(i_files) + ". " +str(i_data) + '.: ' + '"' +
> tracker.strip() + '";' + line.strip());
> 				
> 				print("Send Message to Broker")				
> 				
> 				rc = client.publish(MQTT_TOPIC, '"' + tracker.strip() + '";' +
> line.strip(), MQTT_QOS, False)
> 				
> 	
> 		f.close()
> _______________________________________________
> paho-dev mailing list
> paho-dev@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
> https://dev.eclipse.org/mailman/listinfo/paho-dev



Back to the top