Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import MypcDuinoIO
- import time
- from fractions import Fraction
- class dht22:
- def read(self,pin,retries):
- mypin=pin
- mytries=retries
- if mytries==0:
- x=self.getdht22(mypin)
- elif mytries<0:
- x=1
- while x>0:
- x=self.getdht22(mypin)
- elif mytries>0:
- for i in range(0,mytries):
- x=self.getdht22(mypin)
- if x==0:
- break
- else:
- time.sleep(0.1)
- return x
- def getdht22(self,pin):
- myerror=0
- testpin=pin
- Mybits=''
- Mylist=[]
- count=1
- MyPin = MypcDuinoIO.pin(testpin,1)
- MyPin.low()
- time.sleep(0.0006)
- MyPin.setmode(0)
- for i in range(0,41):
- while MyPin.state()==0:
- pass
- while (MyPin.state()==1 and count<20):
- count += 1
- if count>=20:
- myerror=1
- break
- if count>2:
- Mylist.append('1')
- else:
- Mylist.append('0')
- count=1
- Mybits=''.join(Mylist)
- if len(Mybits)<41:
- self.tempc=0
- self.humid=0
- return 3
- temp=int(Mybits[1:17],2)+int(Mybits[18:33],2)
- if temp>255:
- temp=temp-256
- crc=int(Mybits[33:41],2)
- if temp==crc:
- myerror=0
- else:
- myerror=2
- MyTemp=int(Mybits[18:33],2)
- if MyTemp>32767:
- MyTemp=32768-MyTemp
- self.tempc=MyTemp/10.0
- self.humid=int(Mybits[1:17],2)/10.0
- return myerror
- def temperature(self):
- return self.tempc
- def humidity(self):
- return self.humid
- def dewpoint(self):
- expo=Fraction('1/8')
- temp=(self.humid/100)**expo
- temp2=temp*(112+(0.9*self.tempc))+(0.1*self.tempc)-112
- return "%.2f"%temp2
- MypcDuinoIO.py
- import os
- class pin:
- GPIO_MODE_PATH= os.path.normpath('/sys/devices/virtual/misc/gpio/mode/')
- GPIO_PIN_PATH=os.path.normpath('/sys/devices/virtual/misc/gpio/pin/')
- GPIO_FILENAME="gpio"
- thepin=''
- def __init__(self,mypin,mode):
- self.gpio=mypin
- self.mode=mode
- self.pinMode=(os.path.join(self.GPIO_MODE_PATH, 'gpio'+str(self.gpio)))
- self.pinData=(os.path.join(self.GPIO_PIN_PATH, 'gpio'+str(self.gpio)))
- file=open(self.pinMode,'r+')
- file.write(str(self.mode))
- file.close
- self.file=open(self.pinData,'r+',0)
- def high(self):
- self.file.write("1")
- self.thepin=1
- def low(self):
- self.file.write("0")
- self.thepin=0
- def toggle(self):
- if self.thepin==0:
- self.high()
- else:
- self.low()
- def state(self):
- self.file.seek(0)
- return int(self.file.read(1))
- def setmode(self,x):
- self.mode=x
- file2=open(self.pinMode,'r+')
- file2.write(str(self.mode))
- file2.close
- def __del__(self):
- self.file.close
- class adc:
- ADC_PATH=os.path.normpath('/proc/')
- raw=0
- def __init__(self,pin):
- self.adc=pin
- if self.adc==0 or self.adc==1:
- self.adcMult=2/62
- else:
- self.adcMult=3.3/4095
- self.adcData=(os.path.join(self.ADC_PATH, 'adc'+str(self.adc)))
- self.file=open(self.adcData,'r')
- def raw(self):
- self.file.seek(0)
- value=self.file.read(9)
- value=int(value[5:len(value)])
- return value
- def volts(self):
- self.file.seek(0)
- value=self.file.read(9)
- value=int(value[5:len(value)])
- if value==0:
- return 0
- else:
- return '%.4f'%(value*self.adcMult)
- def __del__(self):
- self.file.close
- test22.py
- import dht22
- MyDHT22=dht22.dht22()
- ## read(pin number, retries)
- ## 0 retries means it tries only once
- ## greater than 0 and it will retry that number of times
- ## less than 0 ad it will retry forever until it gets a good CRC check
- test=MyDHT22.read(2,0)
- ## The return = 0 if its good anything else is an error
- print test
- ## Temperature in degress celcius
- print MyDHT22.temperature()
- ## Humidity in %
- print MyDHT22.humidity()
- ## Calculated dew point in degress celcius
- print MyDHT22.dewpoint()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement