宜蘭聰仔
2016年3月23日 星期三
raspberry pi 用 python 取得dht11之溫濕度資料以php呼叫並顯示於網頁中
感謝開發者
dht11.py
import time import RPi.GPIO as GPIO class DHT11Result: 'DHT11 sensor result returned by DHT11.read() method' ERR_NO_ERROR = 0 ERR_MISSING_DATA = 1 ERR_CRC = 2 error_code = ERR_NO_ERROR temperature = -1 humidity = -1 def __init__(self, error_code, temperature, humidity): self.error_code = error_code self.temperature = temperature self.humidity = humidity def is_valid(self): return self.error_code == DHT11Result.ERR_NO_ERROR class DHT11: 'DHT11 sensor reader class for Raspberry' __pin = 0 def __init__(self, pin): self.__pin = pin def read(self): GPIO.setup(self.__pin, GPIO.OUT) # send initial high self.__send_and_sleep(GPIO.HIGH, 0.05) # pull down to low self.__send_and_sleep(GPIO.LOW, 0.02) # change to input using pull up GPIO.setup(self.__pin, GPIO.IN, GPIO.PUD_UP) # collect data into an array data = self.__collect_input() # parse lengths of all data pull up periods pull_up_lengths = self.__parse_data_pull_up_lengths(data) # if bit count mismatch, return error (4 byte data + 1 byte checksum) if len(pull_up_lengths) != 40: return DHT11Result(DHT11Result.ERR_MISSING_DATA, 0, 0) # calculate bits from lengths of the pull up periods bits = self.__calculate_bits(pull_up_lengths) # we have the bits, calculate bytes the_bytes = self.__bits_to_bytes(bits) # calculate checksum and check checksum = self.__calculate_checksum(the_bytes) if the_bytes[4] != checksum: return DHT11Result(DHT11Result.ERR_CRC, 0, 0) # ok, we have valid data, return it return DHT11Result(DHT11Result.ERR_NO_ERROR, the_bytes[2], the_bytes[0]) def __send_and_sleep(self, output, sleep): GPIO.output(self.__pin, output) time.sleep(sleep) def __collect_input(self): # collect the data while unchanged found unchanged_count = 0 # this is used to determine where is the end of the data max_unchanged_count = 100 last = -1 data = [] while True: current = GPIO.input(self.__pin) data.append(current) if last != current: unchanged_count = 0 last = current else: unchanged_count += 1 if unchanged_count > max_unchanged_count: break return data def __parse_data_pull_up_lengths(self, data): STATE_INIT_PULL_DOWN = 1 STATE_INIT_PULL_UP = 2 STATE_DATA_FIRST_PULL_DOWN = 3 STATE_DATA_PULL_UP = 4 STATE_DATA_PULL_DOWN = 5 state = STATE_INIT_PULL_DOWN lengths = [] # will contain the lengths of data pull up periods current_length = 0 # will contain the length of the previous period for i in range(len(data)): current = data[i] current_length += 1 if state == STATE_INIT_PULL_DOWN: if current == GPIO.LOW: # ok, we got the initial pull down state = STATE_INIT_PULL_UP continue else: continue if state == STATE_INIT_PULL_UP: if current == GPIO.HIGH: # ok, we got the initial pull up state = STATE_DATA_FIRST_PULL_DOWN continue else: continue if state == STATE_DATA_FIRST_PULL_DOWN: if current == GPIO.LOW: # we have the initial pull down, the next will be the data pull up state = STATE_DATA_PULL_UP continue else: continue if state == STATE_DATA_PULL_UP: if current == GPIO.HIGH: # data pulled up, the length of this pull up will determine whether it is 0 or 1 current_length = 0 state = STATE_DATA_PULL_DOWN continue else: continue if state == STATE_DATA_PULL_DOWN: if current == GPIO.LOW: # pulled down, we store the length of the previous pull up period lengths.append(current_length) state = STATE_DATA_PULL_UP continue else: continue return lengths def __calculate_bits(self, pull_up_lengths): # find shortest and longest period shortest_pull_up = 1000 longest_pull_up = 0 for i in range(0, len(pull_up_lengths)): length = pull_up_lengths[i] if length < shortest_pull_up: shortest_pull_up = length if length > longest_pull_up: longest_pull_up = length # use the halfway to determine whether the period it is long or short halfway = shortest_pull_up + (longest_pull_up - shortest_pull_up) / 2 bits = [] for i in range(0, len(pull_up_lengths)): bit = False if pull_up_lengths[i] > halfway: bit = True bits.append(bit) return bits def __bits_to_bytes(self, bits): the_bytes = [] byte = 0 for i in range(0, len(bits)): byte = byte << 1 if (bits[i]): byte = byte | 1 else: byte = byte | 0 if ((i + 1) % 8 == 0): the_bytes.append(byte) byte = 0 return the_bytes def __calculate_checksum(self, the_bytes): return the_bytes[0] + the_bytes[1] + the_bytes[2] + the_bytes[3] & 255
gettc.py (取得溫度濕度)
import RPi.GPIO as GPIO import dht11 import time import datetime import os # initialize GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.cleanup() def gettc(): # read data using pin gpio4 instance = dht11.DHT11(pin = 4) i=0 while i<1000: result = instance.read() if result.is_valid(): print datetime.datetime.now() print("%d" % result.temperature) print("%d" % result.humidity) #return result.temperature + "C" res=os.popen('vcgencmd measure_temp').readline() print res.replace("temp=","").replace("'C\n","") i=1005 time.sleep(1) i += 1 gettc()
php
<html> <head> <?php if (isset($_POST['printon'])) { exec('sudo python /var/www/html/openprintpower.py'); } if (isset($_POST['printoff'])) { exec('sudo python /var/www/html/closeprintpower.py'); } if (isset($_POST['tvon'])) { exec('sudo python /var/www/html/opentvpower.py'); } if (isset($_POST['tvoff'])) { exec('sudo python /var/www/html/closetvpower.py'); } if (isset($_POST['modon'])) { exec('sudo python /var/www/html/openmodpower.py'); } if (isset($_POST['modoff'])) { exec('sudo python /var/www/html/closemodpower.py'); } if (isset($_POST['adslon'])) { exec('sudo python /var/www/html/openadslpower.py'); } if (isset($_POST['adsloff'])) { exec('sudo python /var/www/html/closeadslpower.py'); } if (isset($_POST['routeron'])) { exec('sudo python /var/www/html/openrouterpower.py'); } if (isset($_POST['routeroff'])) { exec('sudo python /var/www/html/closerouterpower.py'); } ?> <title>K REMOTE SCREEN</title> <style type="text/css"> @import url(http://fonts.googleapis.com/css?family=Dosis:200,400,500,600); html, body { height: 100%; } body { background: #f2f2f2; } .container { width: 300px; margin: 10px auto 0; } .de .den, .de .dene, .de .denem, .de .deneme { position: absolute; left: 50%; top: 50%; } .de { position: relative; width: 240px; height: 240px; border-radius: 100%; box-shadow: 0 0 10px rgba(0, 0, 0, .1); background-color: transparent; } .den { position: relative; width: 210px; height: 210px; margin: -105px 0 0 -105px; border-radius: 100%; box-shadow: inset 0 2px 10px rgba(0, 0, 0, .5), 0 2px 20px rgba(255, 255, 255, 1); background: #df3341; background: -moz-linear-gradient(left, #df3341 0%, #d4f355 50%, #61c0ec 100%); background: -webkit-gradient(linear, left top, right top, color-stop(0%,#df3341), color-stop(50%,#d4f355), color-stop(100%,#61c0ec)); background: -webkit-linear-gradient(left, #df3341 0%,#d4f355 50%,#61c0ec 100%); background: linear-gradient(to right, #df3341 0%,#d4f355 50%,#61c0ec 100%); position:relative; } .dene { width: 180px; height: 180px; margin: -90px 0 0 -90px; border-radius: 100%; box-shadow: inset 0 2px 2px rgba(255, 255, 255, .4), 0 3px 13px rgba(0, 0, 0, .85); background: #f2f6f5; background: -moz-linear-gradient(top, #f2f6f5 0%, #cbd5d6 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #f2f6f5), color-stop(100%, #cbd5d6)); background: -webkit-linear-gradient(top, #f2f6f5 0%, #cbd5d6 100%); background: -o-linear-gradient(top, #f2f6f5 0%, #cbd5d6 100%); } .denem { width: 160px; height: 160px; margin: -80px 0 0 -80px; border-radius: 100%; background: #cbd5d6; background: -moz-linear-gradient(top, #cbd5d6 0%, #f2f6f5 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #cbd5d6), color-stop(100%, #f2f6f5)); background: -webkit-linear-gradient(top, #cbd5d6 0%, #f2f6f5 100%); } .deneme { padding: 3px 10px 0 10px; width: 120px; height: 137px; display: inline-block; margin: -70px 0 0 -70px; color: #555; text-shadow: 1px 1px 1px white; font-family: 'Dosis'; font-size: 100px; font-weight: 400; text-align: center; } .deneme1 { padding: 3px 10px 0 10px; width: 100px; height: 137px; display: inline-block; margin: -70px 0 0 -70px; color: #555; text-shadow: 1px 1px 1px white; font-family: 'Dosis'; font-size: 100px; font-weight: 400; text-align: center; } .deneme span { font-size: 30px; font-weight: 200; } .deneme span1 { font-size: 50px; font-weight: 200; } .deneme span2 { font-size: 20px; font-weight: 200; } .deneme strong { position: absolute; right: 10px; top: 25px; font-size: 34px; } .deneme strong1 {position: absolute; right: 10px; top: 25px; font-size: 20px;} .deneme strong2 {position: absolute; right: 15px; top: 75px; font-size: 20px;} </style> </head> <body> <center> Raspberry pi 2 python control GPIO via Web Server <br> 192.168.1.252 </center> <center> <?php exec('sudo python /var/www/html/gettc.py',$m_tc); //print_r($m_tc); ?> 取得DHT11溫度濕度時間:<font style="font-size:30px;" > <?php echo $m_tc[0];?> </font> <!-- echo $m_tc[1] . "度 "; echo $m_tc[2] ."%<br>" ; --> <!-- 加入css溫度顯示 --> <table> <tr> <td><div class="container"> <div class="de"> <div class="den"> <div class="dene"> <div class="denem"> <div class="deneme"> <?php echo $m_tc[1];?><span></span><strong>°</strong> </div> </div> </div> </div> </div> </div> </td> <td><div class="container"> <div class="de"> <div class="den"> <div class="dene"> <div class="denem"> <div class="deneme"> <?php echo $m_tc[2];?><span></span><strong1>%</strong1> </div> </div> </div> </div> </div> </div> </td> <!-- raspberry pi cpu temp --> <td><div class="container"> <div class="de"> <div class="den"> <div class="dene"> <div class="denem"> <div class="deneme"> <span1><?php echo "CPU:\n" . $m_tc[3];?></span1><strong2>°</strong2> </div> </div> </div> </div> </div> </div> </td> </tr> </table> <!-- css 溫度顯示結尾 --> </center> <form method="post"> <table style="width: 100%; text-align: left; margin-left: auto; margin-right: auto;" border="0" > <tbody> <tr> <td style="text-align: center; font-size:60px;" >Turn Relay on</td> <td style="text-align: center; font-size:20px; color:blue;" >Status</td> <td style="text-align: center; font-size:60px;">Turn Relay off</td> </tr> <tr> <td style="text-align: center;" ><br> </td> <td style="text-align: center;" ><br> </td> <td style="text-align: center;" ><br> </td> </tr> <tr> <td style="text-align: left;"><button name="printon" style="font-size:60px;" >Printer ON</button></td> <td style="text-align: center;font-size:40px;"><?php $pinstatus=exec("gpio -g read 5"); if ($pinstatus) {echo "ON";} else {echo "OFF";} ?></td> <td style="text-align: right;"><button name="printoff" style="font-size:60px;" >Printer Off</button></td> </tr> <tr> <td><br><br></td> <td><br><br></td> <td><br><br></td></tr> <tr> <td style="text-align: left;"><button name="tvon" style="font-size:60px;">TV On</button></td> <td style="text-align: center;font-size:40px; "><?php $pinstatus=exec("gpio -g read 6"); if ($pinstatus) {echo "ON";} else {echo "OFF";} ?></td> <td style="text-align: right;"><button name="tvoff" style="font-size:60px;" >TV Off</button></td> </tr> <tr><td><br><br></td> <td><br><br></td> <td><br><br></td></tr> <tr> <td style="text-align: left;"><button name="modon" style="font-size:60px;">MOD On</button></td> <td style="text-align: center;font-size:40px; "><?php $pinstatus=exec("gpio -g read 13"); if ($pinstatus) {echo "ON";} else {echo "OFF";} ?></td> <td style="text-align: right;"><button name="modoff" style="font-size:60px;">MOD Off</button></td> </tr> <tr><td><br><br></td> <td><br><br></td> <td><br><br></td></tr> <tr> <td style="text-align: left;"><button name="adslon" style="font-size:60px;" >ATUR On</button></td> <td style="text-align: center;font-size:40px; "><?php $pinstatus=exec("gpio -g read 26"); if ($pinstatus) {echo "ON";} else {echo "OFF";} ?></td> <td style="text-align: right;"><button name="adsloff" style="font-size:60px;" >ATUR Off</button></td> </tr> <tr><td><br><br></td> <td><br><br></td> <td><br><br></td></tr> <tr> <td style="text-align: left;"><button name="routeron" style="font-size:60px;" >Router On</button></td> <td style="text-align: center;font-size:40px; "><?php $pinstatus=exec("gpio -g read 19"); if ($pinstatus) {echo "ON";} else {echo "OFF";} ?></td> <td style="text-align: right;"><button name="routeroff" style="font-size:60px;" >Router Off</button></td> </tr> </tbody> </table> </form> </body> </html>
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
網誌
▼
2016
(11)
►
10/02 - 10/09
(1)
►
04/17 - 04/24
(1)
►
04/03 - 04/10
(2)
▼
03/20 - 03/27
(3)
raspberry pi 用 python 取得dht11之溫濕度資料以php呼叫並顯示於網頁中
Raspberry pi 用php and python 控制GPIO 並取得GPIO狀態
Raspberry pi 用 python 控制GPIO
►
01/24 - 01/31
(1)
►
01/17 - 01/24
(1)
►
01/10 - 01/17
(1)
►
01/03 - 01/10
(1)
►
2015
(1)
►
05/03 - 05/10
(1)
►
2012
(2)
►
12/09 - 12/16
(1)
►
08/12 - 08/19
(1)
►
2011
(12)
►
10/16 - 10/23
(1)
►
10/02 - 10/09
(2)
►
09/25 - 10/02
(1)
►
09/18 - 09/25
(3)
►
08/28 - 09/04
(1)
►
06/05 - 06/12
(2)
►
02/06 - 02/13
(1)
►
01/16 - 01/23
(1)
►
2010
(25)
►
11/28 - 12/05
(4)
►
09/12 - 09/19
(4)
►
08/22 - 08/29
(1)
►
05/23 - 05/30
(4)
►
03/21 - 03/28
(5)
►
03/14 - 03/21
(2)
►
03/07 - 03/14
(5)
►
2009
(3)
►
06/21 - 06/28
(2)
►
04/19 - 04/26
(1)
►
2008
(10)
►
04/06 - 04/13
(1)
►
03/02 - 03/09
(3)
►
02/24 - 03/02
(1)
►
02/17 - 02/24
(5)
►
2007
(11)
►
04/08 - 04/15
(1)
►
03/18 - 03/25
(5)
►
03/11 - 03/18
(5)
我的最愛
試飛員
往山裡去的小孩
Histats.com
Open Flash Chart
訪客計數器
追蹤者
沒有留言:
張貼留言