-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.py
executable file
·73 lines (59 loc) · 1.44 KB
/
sensor.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
import Adafruit_DHT
from time import sleep
from datetime import datetime
def init():
global sensor, pin, hookdir, datadir
# Define sensor and pin data
sensor = Adafruit_DHT.DHT22
pin = 4
# Define picam hook director
hookdir = '/home/pi/picam/hooks/'
def readsensor():
global hum, temp
# Get Sensor data and format it
hum, temp = Adafruit_DHT.read_retry(sensor, pin)
def textcreator():
global hum, temp
# Convert readings to strings
try:
temp_t = 'Temperatur: {:.1f}C '.format(temp)
hum_t = 'Luftfeuchtigkeit: {:.1f}%'.format(hum)
except ValueError:
temp_t = 'no temp '
hum_t = 'no hum'
# Get time
time_t = datetime.now().strftime('%H:%M')
# Concatenate to one line
subtitle = 'text=' + time_t + ' Uhr ' + temp_t + hum_t
# Write data to subtitle file
f = open(hookdir + 'subtitle', 'w+')
f.write(subtitle)
f.write('\n')
f.write('duration=115')
f.close()
def datalogger():
import urllib.parse
import urllib.request
global hum, temp
# Convert reading to string
try:
temp_t = '{:.2f}'.format(temp)
hum_t = '{:.2f}'.format(hum)
except ValueError:
temp_t = '0'
hum_t ='0'
url = 'http://192.168.178.50/add_data.php'
values = {'temp': temp_t,
'hum': hum_t,
'name': 'sensor1'}
data = urllib.parse.urlencode(values)
#print(data)
full_url = url + '?' + data
with urllib.request.urlopen(full_url) as response:
s = response.read()
#print(s)
# Script execution
init()
readsensor()
#textcreator()
datalogger()