Skip to content

Instantly share code, notes, and snippets.

@Tombo1001
Last active January 19, 2021 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tombo1001/1f096ca224f0e61ac66548e2abd36a25 to your computer and use it in GitHub Desktop.
Save Tombo1001/1f096ca224f0e61ac66548e2abd36a25 to your computer and use it in GitHub Desktop.
Writing to an InfluxDB server with Python3
#!/usr/bin/python
from datetime import datetime
from influxdb import InfluxDBClient
def func_log(unit, kind, value):
client = InfluxDBClient(host='127.0.0.1', port=8086)
client.create_database('DBname')
client.switch_database('DBname')
json_body = [
{
"measurement": "MeasureName",
"tags": {
"unit": unit,
"kind": kind
},
"fields": {
"value": value
},
"time": f'{datetime.utcnow().isoformat()}Z'
}
]
try:
if client.write_points(json_body):
success = True
else:
print("Error writing to InfluxDB")
except:
print("Error writing to InfluxDB")
if __name__ == '__main__':
unit = "Celsius"
kind = "temp"
value = 15
func_log(unit, kind, value)
@Tombo1001
Copy link
Author

Tombo1001 commented Jan 19, 2021

Be sure to update your influxDB server IP and port accordingly on line 6 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment