Skip to main content

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

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()


Back to the top