-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensors.py
66 lines (59 loc) · 2.24 KB
/
sensors.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
import serial, time
import csv
import sys
import traceback
import os
from adc import readadc
import RPi.GPIO as GPIO
import spidev
import time
import dht11
import datetime
#pin[0] analog pin[1] digital
def setup(name, pin):
if name == "dust":
GPIO.setmode(GPIO.BCM) # choose BCM or BOARD
GPIO.setup(pin[1], GPIO.OUT) # set GPIO24 as an output $pinMode(iled, OUTPUT);
GPIO.output(pin[1],0) # to set port/pin to High, use => 1/GPIO.HIGH/True #digitalWrite(iled, LOW); //iled default closed
def read(name, pin, verbose=True):
if name == "mq135" or name == "mq4" or name == "mq6":
val = readadc(pin)
print (name + "read is " + str(val))
return val, val
if name == "dust":
samplingTime = .280 / 1000
deltaTime = 40 / 1000000.0
sleepTime = 9680 / 1000000.0
GPIO.setmode(GPIO.BCM) # choose BCM or BOARD
GPIO.setup(pin[1], GPIO.OUT) # set GPIO24 as an output $pinMode(iled, OUTPUT);
GPIO.output(pin[1],0) # to set port/pin to High, use => 1/GPIO.HIGH/True #digitalWrite(iled, LOW); //iled default closed
time.sleep(0.1)
GPIO.output(pin[1], 1)
time.sleep(samplingTime)
adcvalue = readadc(pin[0])
GPIO.output(pin[1], 0)
return adcvalue, adcvalue
if name=="dht11":
GPIO.setmode(GPIO.BCM)
instance = dht11.DHT11(pin=pin)
lastknown = (23, 35)
result1, result2 = lastknown
try:
result = instance.read()
if (result.is_valid):
result1 = result.temperature
result2 = result.humidity
if(result2==0):
result1, result2 = lastknown
if(verbose):
print "<{}> :: Temperature ::".format(name), result1
print "<{}> :: Humidity ::".format(name), result2
else:
result1, result2 = lastknown
if (verbose):
print "<{}> :: Temperature :: NOT VALID".format(name)
print "<{}> :: Humidity :: NOT VALID".format(name)
lastknown = result1, result2
return result1, result2
except:
print("ERROR in READ...read_temp")