文章标题

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

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

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

#!/bin/bash
if [ $# > 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
    fi
fi

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

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

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

实现代码如下

#!/usr/bin/env python 
# coding=utf-8

#import sys,exceptions,pifacedigitalio as p
import RPi.GPIO as GPIO
import time
import os
import urllib
import urllib2
import json
import traceback

# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 11
SPIMISO = 9
SPIMOSI = 10
SPICS = 8

# photoresistor connected to adc #0
photo_ch = 0

#port init
def 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 adcout

def 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)
                 continue

if __name__ == '__main__':
         try:
                  main()


         except KeyboardInterrupt:
                  pass
GPIO.cleanup()


实现视频如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值