Last active
January 19, 2021 08:41
-
-
Save Tombo1001/1f096ca224f0e61ac66548e2abd36a25 to your computer and use it in GitHub Desktop.
Writing to an InfluxDB server with Python3
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 | |
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be sure to update your influxDB server IP and port accordingly on line 6 👍