Reputation: 77
Before anyone links another Question that is similar I have gone through the questions on here and none of the results have managed to work on my code or im correcting it incorrectly.
Below code is having an issue decoding my json string not sure if the string is incorrectly configured or if the code is written incorrectly full code provided below if there is anything else needed for trouble shooting let me know.
String Recieved from broker
test 0 $GPRP,7F82854A6A62,C3D7EDB7EF28,-65,0201061AFF4C0002158A174208F78D495E8FDA176A3FC0F34D00160019BD
python code
# Open database connection
db = MySQLdb.connect(mysql_server, mysql_username, mysql_passwd, mysql_db)
# prepare a cursor object using cursor() method
cursor = db.cursor()
def on_connect(self,mosq, obj, rc):
print("rc: "+str(rc))
def on_message(mosq, obj, msg):
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
vars_to_sql = []
keys_to_sql = []
list = []
list = json.loads(msg.payload)
for key,value in list.iteritems():
print ("")
print key, value
if key == 'tst':
print "time found"
print value
value = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(value)))
print value
value_type = type(value)
if value_type is not dict:
print "value_type is not dict"
if value_type is unicode:
print "value_type is unicode"
vars_to_sql.append(value.encode('ascii', 'ignore'))
keys_to_sql.append(key.encode('ascii', 'ignore'))
else:
print "value_type is not unicode"
vars_to_sql.append(value)
keys_to_sql.append(key)
#add the msg.topic to the list as well
print "topic", msg.topic
addtopic = 'topic'
vars_to_sql.append(msg.topic.encode('ascii', 'ignore'))
keys_to_sql.append(addtopic.encode('ascii', 'ignore'))
keys_to_sql = ', '.join(keys_to_sql)
try:
# Execute the SQL command
# change locations to the table you are using
queryText = "INSERT INTO messages(%s) VALUES %r"
queryArgs = (keys_to_sql, tuple(vars_to_sql))
cursor.execute(queryText % queryArgs)
print('Successfully Added record to mysql')
db.commit()
except MySQLdb.Error, e:
try:
print "MySQL Error [%d]: %s" % (e.args[0], e.args[1])
except IndexError:
print "MySQL Error: %s" % str(e)
# Rollback in case there is any error
db.rollback()
print('ERROR adding record to MYSQL')
def on_publish(mosq, obj, mid):
print("mid: "+str(mid))
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def on_log(mosq, obj, level, string):
print(string)
mqttc = paho.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
# Uncomment to enable debug messages
mqttc.on_log = on_log
mqttc.connect(broker, broker_port, 60)
mqttc.subscribe(broker_topic, 0)
rc = 0
while rc == 0:
rc = mqttc.loop()
print("rc: "+str(rc))
Upvotes: 0
Views: 186
Reputation: 59741
Assuming that the problem is with the line:
list = json.loads(msg.payload)
in the on_message()
callback and the msg.payload
is:
$GPRP,7F82854A6A62,C3D7EDB7EF28,-65,0201061AFF4C0002158A174208F78D495E8FDA176A3FC0F34D00160019BD
Then is will fail because the payload is not a JSON object. json.loads()
will have not idea what do with it.
It's a basic CSV string, you should be able to use the split()
function to break it up into it's components and then you'll have to interpret each component according to what ever the spec is.
Upvotes: 1