-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_fish_tank_data.py
112 lines (93 loc) · 3.56 KB
/
update_fish_tank_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python3
__author__ = 'Richard J. Sears'
VERSION = "V0.0.4 (2020-02-02)"
# richardjsears@gmail.com
# Reads fish tank data utilizing Seneye API and writes data to InfluxDB
# February 2nd, 2020
# More information about the Seneye API may be found here:
# http://answers.seneye.com/en/Seneye_Products/Seneye_hobbyist_developer_information
# and here:
# https://api.seneye.com/
# pip3 install influxdb if you have not already done so.
from influxdb import InfluxDBClient, exceptions
import requests
import json
import argparse
# Setup Argparser for Debug Mode. Run this script from the command line with "--DEBUG"
# to output data to the screen. When in DEBUG mode NO DATA will be written to the
# database.
parser = argparse.ArgumentParser()
parser.add_argument("--DEBUG", action="store_true", help="Add '--DEBUG' to run script in DUBUG mode")
args = parser.parse_args()
DEBUG = args.DEBUG
# Here you can set DEBUG2 to True or False to override the argparse default. This is so you can set
# DEBUG mode on permanently for repeated testing without having to pass "--DEBUG" on the command line.
DEBUG2 = False
# InfluxDB connections settings
influx_host = 'my_influxdb_host'
influx_port = 8086
influx_user = 'aquaman'
influx_password = 'breathewater'
influx_dbname = 'fish_tank'
# This is useful if you have multiple tanks you want to keep track of...
tank_name = "75planted"
# Seneye API Settings
user_email = "your_email_address@gmail.com"
user_passwd = "super_secret_password"
user_device = "12345"
# If you are like me and want to see your tank water temperature in Fahrenheit, set to True
Temp_in_F = True
def write_data(measurement, value):
client = InfluxDBClient(influx_host, influx_port, influx_user, influx_password, influx_dbname, timeout=2)
json_body = [
{
"measurement": measurement,
"tags": {
"tank": tank_name
},
"fields": {
"value": value
}
}
]
try:
client.write_points(json_body)
client.close()
except (exceptions.InfluxDBClientError, exceptions.InfluxDBServerError) as e:
print(e)
def get_fish_tank_data():
url = "https://api.seneye.com/v1/devices/{}?IncludeState=1&user={}&pwd={}".format(user_device, user_email, user_passwd)
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}
api_request = requests.get(url, headers=headers)
response = api_request.json()
to_json = json.dumps(response)
dict_json = json.loads(to_json)
if Temp_in_F:
keys = ['ph', 'nh3', 'nh4', 'o2', 'lux', 'par', 'kelvin']
for key in dict_json['exps']:
if key in keys:
value = dict_json['exps'][key]['curr']
if DEBUG or DEBUG2:
print(key, value)
else:
write_data(key, float(value))
# Convert temperature from C to F
temp_in_c = float(dict_json['exps']['temperature']['curr'])
temp_in_f = (9.0 / 5.0 * temp_in_c) + 32
if DEBUG or DEBUG2:
print("temperature", temp_in_f)
else:
write_data('temperature', temp_in_f)
else:
keys = ['temperature', 'ph', 'nh3', 'nh4', 'o2', 'lux', 'par', 'kelvin']
for key in dict_json['exps']:
if key in keys:
value = dict_json['exps'][key]['curr']
if DEBUG or DEBUG2:
print(key, value)
else:
write_data(key, float(value))
def main():
get_fish_tank_data()
if __name__ == '__main__':
main()