Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Scrape serial port for text data and publish on MQTT

View readserial.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#!/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()

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

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.

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.

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.