-
Notifications
You must be signed in to change notification settings - Fork 10
/
sensordata_extended.py
76 lines (67 loc) · 2.71 KB
/
sensordata_extended.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
#!/usr/bin/python
import sys
import time
import Adafruit_DHT
import threading
import json
import urllib2
import base64
import pprint
from datetime import datetime
from random import randint
from random import uniform
start_time = time.time()
interval = 10 # set interval in seconds
deviceId = 'SOMEUNIQUE-DEVICE-ID' # for example: a uuid like this f7645058-fe8c-11e6-bc64-92361f002671
url = 'https://SOME.URL/TO/POST/TO' # for example: https://YOUR-OWNCLOUD-INSTANCE/index.php/apps/sensorlogger/api/v1/createlog/ if you are using SensorLogger for owncloud
username = 'SOMEUSERNAME'
password = 'YOUR PWD OR APP TOKEN'
headers = {'content-type': 'application/json'}
sensor_args = { '11': Adafruit_DHT.DHT11,
'22': Adafruit_DHT.DHT22,
'2302': Adafruit_DHT.AM2302 }
if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
sensor = sensor_args[sys.argv[1]]
pin = sys.argv[2]
else:
print('usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#')
print('example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4')
sys.exit(1)
def sensorData():
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')
fakeData1 = randint(-100,100)
fakeData2 = randint(0,1000)
fakeData3 = randint(0,50)
fakeData4 = uniform(0,1)
if humidity is not None and temperature is not None:
#Example of payload for extended data see : https://github.com/alexstocker/sensorlogger/blob/master/tests/curl/post_extend_0.php
payload = {
'deviceId': deviceId,
'date': currentDate,
'data': [{'dataTypeId':1,
'value' : temperature},
{'dataTypeId':2,
'value' : humidity},
{'dataTypeId':3,
'value' : fakeData1},
{'dataTypeId':3,
'value' : fakeData2}
{'dataTypeId':3,
'value' : fakeData3}
{'dataTypeId':3,
'value' : fakeData4}]
}
req = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
req.add_header("Authorization", "Basic %s" % base64string)
req.add_header("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'")
req.add_header('Content-Type','application/json')
data = json.dumps(payload)
response = urllib2.urlopen(req,data)
else:
print('Failed to get reading. Try again!')
sys.exit(1)
sensorData()