文章标题

来源:互联网 发布:跳转 打开淘宝客户端 编辑:程序博客网 时间:2024/06/12 06:05

树莓派多传感器工作系列(一)

树莓派和水位传感器,继电器一起工作
树莓派和数位传感器工作时候,注意需要数模转化器

首先编辑脚本,利用电平高低控制继电器,实现通断

#!/bin/bashif [ $# > 1 ]then/usr/local/bin/gpio mode 4 out    if [[ "$1" = "on" ]]    then/usr/local/bin/gpio write 4 on    fi    if [[ "$1" = "off" ]]    then/usr/local/bin/gpio write 4 off    fifi

注意这里的4对应的是树莓派的board编码。

理想中水位传感器根据水位高低来控制继电器,当水位过高、过低都打开继电器工作。

水位传感器水位变化的公式,需要自己去修改,计算。

实现代码如下

#!/usr/bin/env python # coding=utf-8#import sys,exceptions,pifacedigitalio as pimport RPi.GPIO as GPIOimport timeimport osimport urllibimport urllib2import jsonimport traceback# change these as desired - they're the pins connected from the# SPI port on the ADC to the CobblerSPICLK = 11SPIMISO = 9SPIMOSI = 10SPICS = 8# photoresistor connected to adc #0photo_ch = 0#port initdef init():         GPIO.setwarnings(False)         GPIO.cleanup()         #clean up at the end of your script         GPIO.setmode(GPIO.BCM)     #to specify whilch pin numbering system         # set up the SPI interface pins         GPIO.setup(SPIMOSI, GPIO.OUT)         GPIO.setup(SPIMISO, GPIO.IN)         GPIO.setup(SPICLK, GPIO.OUT)         GPIO.setup(SPICS, GPIO.OUT)         #GPIO.setup(7, GPIO.OUT)         #-----------start--------------init-------         url = "http://112.74.161.95/task/init"         req = urllib2.Request(url)         res_data = urllib2.urlopen(req)         print res_data         #--------------end-----------------#read SPI data from MCP3008(or MCP3204) chip,8 possible adc's (0 thru 7)def readadc(adcnum, clockpin, mosipin, misopin, cspin):        if ((adcnum > 7) or (adcnum < 0)):                return -1        GPIO.output(cspin, True)            GPIO.output(clockpin, False)  # start clock low        GPIO.output(cspin, False)     # bring CS low        commandout = adcnum        commandout |= 0x18  # start bit + single-ended bit        commandout <<= 3    # we only need to send 5 bits here        for i in range(5):                if (commandout & 0x80):                        GPIO.output(mosipin, True)                else:                        GPIO.output(mosipin, False)                commandout <<= 1                GPIO.output(clockpin, True)                GPIO.output(clockpin, False)        adcout = 0        # read in one empty bit, one null bit and 10 ADC bits        for i in range(12):                GPIO.output(clockpin, True)                GPIO.output(clockpin, False)                adcout <<= 1                if (GPIO.input(misopin)):                        adcout |= 0x1        GPIO.output(cspin, True)        adcout >>= 1       # first bit is 'null' so drop it        return adcoutdef main():         init()         time.sleep(2)         print"will start detec water level\n"         while True:             try:                  needsleep=1                  adc_value=readadc(photo_ch, SPICLK, SPIMOSI, SPIMISO, SPICS)                  val= (adc_value-300)/3                  print val                  text=""                  #isNo=GPIO.input(7);                  if (val <= 0):                           text = "没水了,正在为您充水"                           print text                           #-----------start---------------------                           print res_data                           #--------------end-----------------                           #-----------start---------------------                           try:                                resStr = res_data.read()                                print resStr                                obj=json.loads(resStr)                                waterOn=obj['res']                           except BaseException:                                continue                           print waterOn                           #--------------end-----------------                           if waterOn==1:                               os.system('cd/home/pi/scripts&&./jidianqi on')                           needsleep=0                  elif (val > 70):                           text = "水满了,正在为您关闭水龙头"                           print text                           #-----------start---------------------                           print res_data                           #--------------end-----------------                           #-----------start---------------------                           try:                                resStr = res_data.read()                                print resStr                                obj=json.loads(resStr)                                waterOn=obj['res']                           except BaseException:                                continue                           print waterOn                           #--------------end-----------------                           if waterOn==0:                               os.system('cd /home/pi/scripts&&./jidianqi off')                           needsleep=0                  print "success....."                  time.sleep(1)             except BaseException:                 print traceback.format_exc()                 if needsleep==1:                     time.sleep(2)                 continueif __name__ == '__main__':         try:                  main()         except KeyboardInterrupt:                  passGPIO.cleanup()

实现视频如下:

原创粉丝点击