-
Notifications
You must be signed in to change notification settings - Fork 1
/
thingspeak_temp.py
59 lines (44 loc) · 1.04 KB
/
thingspeak_temp.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
#!/usr/bin/python
import sys
import os
from time import sleep
try:
import Adafruit_DHT
except RuntimeError:
print("Feil ved importering av Adafruit_DHT")
try:
import urllib2
except RuntimeError:
print("Feil ved importering av urllib2")
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Feil ved importering av RPi.GPIO! Har du husket å kjøre scriptet som sudo?")
DEBUG = 1
# GPIO pinne som mottar data
DHTpin = 4
# API nøkkel
myAPI = "API nøkkel"
GPIO.setmode(GPIO.BCM)
# Henter sensor data
def getSensorData():
RHW, TW = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHTpin)
# returnerer data
return (str(RHW), str(TW))
# main() funksjonen
def main():
print 'starter...'
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
print baseURL
try:
RHW, TW = getSensorData()
f = urllib2.urlopen(baseURL +
"&field1=%s&field2=%s" % (TW, RHW))
print f.read()
print TW + " " + RHW
f.close()
except:
print 'Avslutter.'
# Kaller main
if __name__ == '__main__':
main()