Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Scrape serial port for text data and publish on MQTT
#!/usr/bin/python
#
#simple app to read string from serial port
#and publish via MQTT
#
#uses the Python MQTT client from the Mosquitto project
#http://mosquitto.org
#
#Andy Piper http://andypiper.co.uk
#2011/09/15
import serial
import mosquitto
import os
serialdev = '/dev/ttyUSB0'
broker = "127.0.0.1"
port = 1883
#MQTT callbacks
def on_connect(rc):
if rc == 0:
#rc 0 successful connect
print "Connected"
else:
raise Exception
def on_publish(val):
print "Published ", val
#called on exit
#close serial, disconnect MQTT
def cleanup():
print "Ending and cleaning up"
ser.close()
mqttc.disconnect()
try:
print "Connecting... ", serialdev
#connect to serial port
ser = serial.Serial(serialdev, 9600, timeout=20)
except:
print "Failed to connect serial"
#unable to continue with no serial input
raise SystemExit
try:
ser.flushInput()
#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)
pass
# handle list index error (i.e. assume no data received)
except (IndexError):
print "No data received within serial timeout period"
cleanup()
# handle app closure
except (KeyboardInterrupt):
print "Interrupt received"
cleanup()
except (RuntimeError):
print "uh-oh! time to die"
cleanup()
@sisomm

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: https://github.com/sisomm/iot-concepts

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

@andypiper
Owner

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.

@antex22

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 "./serilScrape.py", line 71, in
while mqttc.loop() == 0:
File "/usr/lib/python2.7/dist-packages/mosquitto.py", line 720, in loop
rc = self.loop_read(max_packets)
File "/usr/lib/python2.7/dist-packages/mosquitto.py", line 961, in loop_read
rc = self._packet_read()
File "/usr/lib/python2.7/dist-packages/mosquitto.py", line 1360, in _packet_read
rc = self._packet_handle()
File "/usr/lib/python2.7/dist-packages/mosquitto.py", line 1781, in _packet_handle
return self._handle_connack()
File "/usr/lib/python2.7/dist-packages/mosquitto.py", 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...
23.24

23.24

23.24

23.24

23.24

23.24

23.24
23.24
23.24
23.24

23.24

22.75

23.24

23.24

23.24

22.75

22.75

23.24

Thanks for your help.

@26labs

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
Something went wrong with that request. Please try again.