Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Scrape serial port for text data and publish on MQTT
#simple app to read string from serial port
#and publish via MQTT
#uses the Python MQTT client from the Mosquitto project
#Andy Piper
import serial
import mosquitto
import os
serialdev = '/dev/ttyUSB0'
broker = ""
port = 1883
#MQTT callbacks
def on_connect(rc):
if rc == 0:
#rc 0 successful connect
print "Connected"
raise Exception
def on_publish(val):
print "Published ", val
#called on exit
#close serial, disconnect MQTT
def cleanup():
print "Ending and cleaning up"
print "Connecting... ", serialdev
#connect to serial port
ser = serial.Serial(serialdev, 9600, timeout=20)
print "Failed to connect serial"
#unable to continue with no serial input
raise SystemExit
#create an mqtt client
mypid = os.getpid()
client_uniq = "arduino_pub_"+str(mypid)
mqttc = mosquitto.Mosquitto(client_uniq)
#attach MQTT callbacks
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
#connect to broker
mqttc.connect(broker, port, 60, True)
#remain connected to broker
#read data from serial and publish
while mqttc.loop() == 0:
line = ser.readline()
#split line as it contains V,temp
list = line.split(",")
#second list element is temp
temp = list[1].rstrip()
mqttc.publish("arduino/temp", temp)
# handle list index error (i.e. assume no data received)
except (IndexError):
print "No data received within serial timeout period"
# handle app closure
except (KeyboardInterrupt):
print "Interrupt received"
except (RuntimeError):
print "uh-oh! time to die"

This comment has been minimized.

Copy link

@sisomm sisomm commented Feb 1, 2014

Hi Andy, I have made a littel demo of an integration between minecraft and arduino using mqtt. I use a similar approac to you, (indeed I copied some of the loop code just now!)

As you can see in this youtube video: Video This approach makes the whole thing sluggish, as the loop sits waiting before it checks arduino. Is there a way to make the loop snappier?

If you're interested my repo is here:

Be ware that this is work in progress, and I use the repo to keep the raspberries and the mac in sync.

The skull movement should be much more smooth, and lots of the code is hastily written. This is just for fun


This comment has been minimized.

Copy link
Owner Author

@andypiper andypiper commented Feb 1, 2014

This is a little bit old now! I'm not sure whether using the Paho Python client - a newer version of the mosquitto Python client - will improve things.


This comment has been minimized.

Copy link

@antex22 antex22 commented Jul 3, 2014

Hi Andy,
I used your script to scrape a temp reading from a serial interface, and get the following error. Can you help why this is not working?
Here is the error I get
TypeError: on_connect() takes exactly 1 argument (3 given)

Connecting... /dev/ttyACM0
Traceback (most recent call last):
File "./", line 71, in
while mqttc.loop() == 0:
File "/usr/lib/python2.7/dist-packages/", line 720, in loop
rc = self.loop_read(max_packets)
File "/usr/lib/python2.7/dist-packages/", line 961, in loop_read
rc = self._packet_read()
File "/usr/lib/python2.7/dist-packages/", line 1360, in _packet_read
rc = self._packet_handle()
File "/usr/lib/python2.7/dist-packages/", line 1781, in _packet_handle
return self._handle_connack()
File "/usr/lib/python2.7/dist-packages/", line 1822, in _handle_connack
self.on_connect(self, self._userdata, result)
TypeError: on_connect() takes exactly 1 argument (3 given)

The temp reading from sensor console looks like this...

Thanks for your help.


This comment has been minimized.

Copy link

@26labs 26labs commented Dec 14, 2015

antexx22 - on_connect is a callback for a mqtt client. So it is event and its only argument is the mqtt client for whicjh the callback will be raised. Its likely you are trying to get the topic and payload in the same line of code. If you are subscribing then you need a callback for subscribe:
mqtt.message = on_message

and then that of course means that somewhere previously in your code you should have function on_message to handle each message found when you subscribe such as:

def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment