Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. import MypcDuinoIO
  2. import time
  3. from fractions import Fraction
  4.  
  5. class dht22:
  6.  
  7. def read(self,pin,retries):
  8. mypin=pin
  9. mytries=retries
  10. if mytries==0:
  11. x=self.getdht22(mypin)
  12. elif mytries<0:
  13. x=1
  14. while x>0:
  15. x=self.getdht22(mypin)
  16. elif mytries>0:
  17. for i in range(0,mytries):
  18. x=self.getdht22(mypin)
  19. if x==0:
  20. break
  21. else:
  22. time.sleep(0.1)
  23. return x
  24.  
  25. def getdht22(self,pin):
  26. myerror=0
  27. testpin=pin
  28. Mybits=''
  29. Mylist=[]
  30. count=1
  31. MyPin = MypcDuinoIO.pin(testpin,1)
  32. MyPin.low()
  33. time.sleep(0.0006)
  34. MyPin.setmode(0)
  35. for i in range(0,41):
  36. while MyPin.state()==0:
  37. pass
  38. while (MyPin.state()==1 and count<20):
  39. count += 1
  40. if count>=20:
  41. myerror=1
  42. break
  43. if count>2:
  44. Mylist.append('1')
  45. else:
  46. Mylist.append('0')
  47. count=1
  48. Mybits=''.join(Mylist)
  49. if len(Mybits)<41:
  50. self.tempc=0
  51. self.humid=0
  52. return 3
  53. temp=int(Mybits[1:17],2)+int(Mybits[18:33],2)
  54. if temp>255:
  55. temp=temp-256
  56. crc=int(Mybits[33:41],2)
  57. if temp==crc:
  58. myerror=0
  59. else:
  60. myerror=2
  61. MyTemp=int(Mybits[18:33],2)
  62. if MyTemp>32767:
  63. MyTemp=32768-MyTemp
  64. self.tempc=MyTemp/10.0
  65. self.humid=int(Mybits[1:17],2)/10.0
  66. return myerror
  67.  
  68. def temperature(self):
  69. return self.tempc
  70.  
  71. def humidity(self):
  72. return self.humid
  73.  
  74. def dewpoint(self):
  75. expo=Fraction('1/8')
  76. temp=(self.humid/100)**expo
  77. temp2=temp*(112+(0.9*self.tempc))+(0.1*self.tempc)-112
  78. return "%.2f"%temp2
  79. MypcDuinoIO.py
  80. import os
  81.  
  82. class pin:
  83. GPIO_MODE_PATH= os.path.normpath('/sys/devices/virtual/misc/gpio/mode/')
  84. GPIO_PIN_PATH=os.path.normpath('/sys/devices/virtual/misc/gpio/pin/')
  85. GPIO_FILENAME="gpio"
  86. thepin=''
  87.  
  88. def __init__(self,mypin,mode):
  89. self.gpio=mypin
  90. self.mode=mode
  91. self.pinMode=(os.path.join(self.GPIO_MODE_PATH, 'gpio'+str(self.gpio)))
  92. self.pinData=(os.path.join(self.GPIO_PIN_PATH, 'gpio'+str(self.gpio)))
  93. file=open(self.pinMode,'r+')
  94. file.write(str(self.mode))
  95. file.close
  96. self.file=open(self.pinData,'r+',0)
  97.  
  98. def high(self):
  99. self.file.write("1")
  100. self.thepin=1
  101.  
  102. def low(self):
  103. self.file.write("0")
  104. self.thepin=0
  105.  
  106. def toggle(self):
  107. if self.thepin==0:
  108. self.high()
  109. else:
  110. self.low()
  111.  
  112. def state(self):
  113. self.file.seek(0)
  114. return int(self.file.read(1))
  115.  
  116. def setmode(self,x):
  117. self.mode=x
  118. file2=open(self.pinMode,'r+')
  119. file2.write(str(self.mode))
  120. file2.close
  121.  
  122. def __del__(self):
  123. self.file.close
  124.  
  125. class adc:
  126. ADC_PATH=os.path.normpath('/proc/')
  127. raw=0
  128.  
  129. def __init__(self,pin):
  130. self.adc=pin
  131. if self.adc==0 or self.adc==1:
  132. self.adcMult=2/62
  133. else:
  134. self.adcMult=3.3/4095
  135. self.adcData=(os.path.join(self.ADC_PATH, 'adc'+str(self.adc)))
  136. self.file=open(self.adcData,'r')
  137.  
  138. def raw(self):
  139. self.file.seek(0)
  140. value=self.file.read(9)
  141. value=int(value[5:len(value)])
  142. return value
  143.  
  144. def volts(self):
  145. self.file.seek(0)
  146. value=self.file.read(9)
  147. value=int(value[5:len(value)])
  148. if value==0:
  149. return 0
  150. else:
  151. return '%.4f'%(value*self.adcMult)
  152.  
  153. def __del__(self):
  154. self.file.close
  155. test22.py
  156. import dht22
  157.  
  158. MyDHT22=dht22.dht22()
  159.  
  160. ## read(pin number, retries)
  161. ## 0 retries means it tries only once
  162. ## greater than 0 and it will retry that number of times
  163. ## less than 0 ad it will retry forever until it gets a good CRC check
  164. test=MyDHT22.read(2,0)
  165. ## The return = 0 if its good anything else is an error
  166. print test
  167. ## Temperature in degress celcius
  168. print MyDHT22.temperature()
  169. ## Humidity in %
  170. print MyDHT22.humidity()
  171. ## Calculated dew point in degress celcius
  172. print MyDHT22.dewpoint()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement