-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathambient_device.py
More file actions
67 lines (52 loc) · 1.66 KB
/
ambient_device.py
File metadata and controls
67 lines (52 loc) · 1.66 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
## @file ambient_device.py
## @brief Script to send BME280 data to Ambient
## @author Keitetsu
## @date 2021/10/12
## @copyright Copyright (c) 2021 Keitetsu
## @par License
## This software is released under the MIT License.
##
import json
import ambient
import smbus2
import bme280
import time
def post_BME280Data_to_Ambient(data_creation_time, data):
with open('ambient.json', 'r') as f:
dict_configs = json.load(f)
str_channelId = dict_configs['ambient']['channelId']
str_writeKey = dict_configs['ambient']['writeKey']
am = ambient.Ambient(str_channelId, str_writeKey)
dict_data = {
"created": time.strftime("%Y-%m-%d %H:%M:%S", data_creation_time),
"d1": data.temperature,
"d2": data.humidity,
"d3": data.pressure
}
print("begin request")
r = am.send(dict_data)
print("response status code: %d" % (r.status_code))
if r.status_code == 200:
print("response text: %s" % (r.text))
ret_val = True
else:
ret_val = False
print("end request")
return ret_val
if __name__ == '__main__':
port = 1
address = 0x76
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, address)
# the sample method will take a single reading and return a
# compensated_reading object
data = bme280.sample(bus, address, calibration_params)
data_creation_time = time.localtime()
for i in range(0, 5):
result = post_BME280Data_to_Ambient(data_creation_time, data)
if result == True:
break
time.sleep(2)