Created
September 15, 2011 09:56
-
-
Save andypiper/1218932 to your computer and use it in GitHub Desktop.
Scrape serial port for text data and publish on MQTT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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))