树梅派学习 14. 雨滴检测上报系统

版权声明:(谢厂节的博客)博主文章绝大部分非原创,转载望留链接。 https://blog.csdn.net/xundh/article/details/81989335

这一次搭建一个雨滴检测并发送微信消息到手机上。

Created with Raphaël 2.1.2 雨滴检测 发送微信消息

硬件端:雨滴检测

雨滴检测传感器原理图:
这里写图片描述

接线图:
这里写图片描述

程序
PCF8591.py

#!/usr/bin/env python
#------------------------------------------------------
#
#       This is a program for PCF8591 Module.
#
#       Warnng! The Analog input MUST NOT be over 3.3V!
#    
#       In this script, we use a poteniometer for analog
#   input, and a LED on AO for analog output.
#
#       you can import this script to another by:
#   import PCF8591 as ADC
#   
#   ADC.Setup(Address)  # Check it by sudo i2cdetect -y -1
#   ADC.read(channal)   # Channal range from 0 to 3
#   ADC.write(Value)    # Value range from 0 to 255     
#
#------------------------------------------------------
import smbus
import time

# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)

#check your PCF8591 address by type in 'sudo i2cdetect -y -1' in terminal.
def setup(Addr):
    global address
    address = Addr

def read(chn): #channel
    if chn == 0:
        bus.write_byte(address,0x40)
    if chn == 1:
        bus.write_byte(address,0x41)
    if chn == 2:
        bus.write_byte(address,0x42)
    if chn == 3:
        bus.write_byte(address,0x43)
    bus.read_byte(address) # dummy read to start conversion
    return bus.read_byte(address)

def write(val):
    temp = val # move string value to temp
    temp = int(temp) # change string to integer
    # print temp to see on terminal else comment out
    bus.write_byte_data(address, 0x40, temp)

if __name__ == "__main__":
    setup(0x48)
    while True:
        print 'AIN0 = ', read(0)
        print 'AIN1 = ', read(1)
        tmp = read(0)
        tmp = tmp*(255-125)/255+125 # LED won't light up below 125, so convert '0-255' to '125-255'
        write(tmp)
#       time.sleep(0.3)

rain_detection.py

#!/usr/bin/env python
import PCF8591 as ADC
import RPi.GPIO as GPIO
import time
import math
import itchat

DO = 17
LAST_X = 0

GPIO.setmode(GPIO.BCM)

def setup():
    ADC.setup(0x48)
    GPIO.setup(DO, GPIO.IN)

def Print(x): 
    global LAST_X
    if LAST_X == x:
        return
    LAST_X=x
    if x == 1:
        LAST_X=1
        print ''
        print '   ***************'
        print '   * Not raining *'
        print '   ***************'
        print ''
    if x == 0:
        LAST_X=0
        itchat.send('its rainning', toUserName='filehelper')
        print ''
        print '   *************'
        print '   * Raining!! *'
        print '   *************'
        print ''

def loop():
    status = 1
    while True:
        print ADC.read(0)

        tmp = GPIO.input(DO);
        if tmp != status:
            Print(tmp)
            status = tmp

        time.sleep(0.2)

if __name__ == '__main__':
    itchat.auto_login(True)
    try:
        setup()
        loop()
    except KeyboardInterrupt: 
        pass    

运行:

sudo pip install itchat
python rain_detection.py

实体接线图:

这里写图片描述

手机端:实时显示

运行程序。在雨滴传感器上滴水,会发送消息到微信的文件传输助手。

程序运行需要在可视化界面进行,否则生成的qr.png显示想办法显示出来让手机微信扫描并登陆。

猜你喜欢

转载自blog.csdn.net/xundh/article/details/81989335