-
Notifications
You must be signed in to change notification settings - Fork 0
/
senddata.py
66 lines (52 loc) · 2.08 KB
/
senddata.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
#!/usr/bin/python
import sys
import time
import Adafruit_DHT
import threading
import json
import urllib3
import base64
import pprint
from datetime import datetime
import http.client
import math
interval = 30 # set interval in seconds
deviceId = '' # same as in register.py
url = '' # https://CLOUD.MYCLOUD.XYZ/index.php/apps/sensorlogger/api/v1/createlog/ -> NOTE: SLASH AT THE END is necessary
# Which sensor do you want to use?
sensor = Adafruit_DHT.DHT22
pin = 4 # Sensor data pin
def sendData():
threading.Timer(interval, sensorData).start()
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
cdatetime = datetime.now()
currentDate = cdatetime.strftime('%Y-%m-%d %H:%M:%S')
# calculate dew point:
A = 17.68
B = 243.12
tmp = ((A * temperature) / (B + temperature)) + math.log(humidity / 100.0)
dew_point = (B * tmp) / (A - tmp)
if humidity is not None and temperature is not None:
# specify the data to be sent (index 1,2,3 refers to the response in register.py)
payload = {
'deviceId': deviceId,
'date': currentDate,
'data': [{'dataTypeId':1,
'value' : temperature},
{'dataTypeId':2,
'value' : humidity},
{'dataTypeId':3,
'value' : dew_point}]
}
encoded_body = json.dumps(payload)
http = urllib3.PoolManager()
credentials = '%s:%s' % ('USERNAME', 'DEVICE PASSWORD')
encoded_credentials = base64.b64encode(credentials.encode('ascii'))
r = http.request('POST', url, headers={'Content-Type': 'application/json', "Content-Security-Policy" : "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'", "Authorization" : "Basic %s" % encoded_credentials.decode('ascii')},
body=encoded_body)
# uncomment to print response (debug)
# print(r.data)
else:
print('Something went wrong with your sensor!')
sys.exit(1)
sendData()