I use the following code to add data to the database from MQTT:
def sendToDB(topic, payload):
‘’‘This function will transmit the given payload to the InfluxDB server’‘’
receiveTime=datetime.datetime.utcnow()
jsonData = [
{
"measurement": str(topic),
#"time": receiveTime,
"fields": {
"value": float(payload)
}
}
]
#jsonData = json.dumps([jsonD])
print jsonData
try:
dbConn.write_points(jsonData)
logging.debug("Wrote " + jsonData + "to InfluxDB.")
except Exception as e:
try:
logging.critical("Couldn't write to InfluxDB: " + e.message)
except TypeError as e2:
logging.critical("Couldn't write to InfluxDB.")
And recive output likr this:
2018-03-04 17:57:18,963 http://localhost:8086 “POST /write?db=mqtt HTTP/1.1” 204 0
2018-03-04 17:57:18,964 Couldn’t write to InfluxDB: cannot concatenate ‘str’ and ‘list’ objects
2018-03-04 17:57:18,964 Received message: tp_dush_a16.8
[{‘fields’: {‘value’: 16.8}, ‘measurement’: ‘tp_dush_a’}]
2018-03-04 17:57:18,972 http://localhost:8086 “POST /write?db=mqtt HTTP/1.1” 204 0
2018-03-04 17:57:18,973 Couldn’t write to InfluxDB: cannot concatenate ‘str’ and ‘list’ objects
but all the data was inserted into the database!