-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathiot_thingsboard.py
More file actions
71 lines (53 loc) · 1.72 KB
/
iot_thingsboard.py
File metadata and controls
71 lines (53 loc) · 1.72 KB
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
import time
import requests
class ThingsBoard():
"""
Ref:
https://thingsboard.io/docs/reference/http-api/
"""
def __init__(self,
token,
hostname='thingsboard.cloud',
protocol='https',
):
self._telemetry_url = protocol + '://' + hostname + '/api/v1/' + token + '/telemetry'
def post_telemetry(self, values : list[dict]):
"""
Send multiple telemetry values.
The 'time' key should be in Unix milliseconds.
"""
def encode_one(d):
o = { 'values': {} }
if 'time' in d:
o['ts'] = d['time']
for k, v in d.items():
if k == 'time':
continue
o['values'][k] = v
return o
payload = [ encode_one(v) for v in values ]
r = requests.post(self._telemetry_url, json=payload)
assert r.status_code == 200, (r.status_code, r.content)
def unix_time_seconds():
timestamp = time.time()
epoch_year = time.gmtime(0)[0]
if epoch_year == 2020:
# seconds between 2000 (MicroPython epoch) and 1970 (Unix epoch)
epoch_difference = 946684800
timestamp = timestamp + epoch_difference
elif epoch_year == 1970:
pass
else:
raise ValueError('Unknown epoch year')
return float(timestamp)
# bc3ab311-4e92-11ef-b45a-8f71ad378839
ACCESS_TOKEN = '7AiV0dXRPWKrxrLcI4wO'
api = ThingsBoard(token=ACCESS_TOKEN)
t = int(unix_time_seconds() * 1000)
values = []
for s in range(0, 60, 10):
v = {'time': t-(s*1000), 'db2': 78.0+s, 'hex': 'ABCEDFE123122312452231DFABCEDF'}
values.append(v)
api.post_telemetry(values)
print(values)
print('Posted telemetry')