查看: 2770|回复: 0

[大赛作品提交] 树莓派DIY之六------系统联调,远程监控及无人值守报警

[复制链接]
  • TA的每日心情
    无聊
    2021-6-6 21:56
  • 签到天数: 225 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2017-12-14 23:58:03 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 ky123 于 2018-1-31 14:17 编辑

    感谢e络盟提供的助赛基金。
    1.1 树莓派gpio定义和布局。
    lcd1602连接.png
    1.2  驱动GPIO
    目前有两种方式可以通过 RPi.GPIO 对 Raspberry Pi 上的 IO 针脚进行编号。
    第一种方式是使用 BOARD 编号系统。该方式参考 RaspberryPi 主板上 P1 接线柱的针脚编号。使用该方式的优点是无需考虑主板的修订版本,您硬件始终都是可用的状态。您将无需从新连接线路和更改您的代码。
    第二种方式是使用 BCM 编号。这是一种较低层的工作方式 – 该方式参考 BroadcomSOC 的通道编号。使用过程中,您始终要保证主板上的针脚与图表上标注的通道编号相对应。您的脚本可能在 Raspberry Pi 主板进行修订版本更新时无法工作。
    指定您所使用的方式(必须指定):
    GPIO.setmode(GPIO.BOARD)
    或者

    GPIO.setmode(GPIO.BCM)
    #!usr/bin/python
    # -*- coding: utf-8 -*-
    import RPi.GPIO as GPIO
    import time
    #BOARD编号方式,基于插座引脚编号
    #GPIO.setmode(GPIO.BOARD)
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    # 输出模式
    GPIO.setup(21, GPIO.OUT)
    GPIO.setup(21, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(20,GPIO.IN)
    count=0
       while True:
           GPIO.output(21, GPIO.HIGH)
           time.sleep(1)
           GPIO.output(21, GPIO.LOW)
           time.sleep(1)
    1.3  驱动LCD1602
    之前使用c编写过LCD1602的驱动程序,相对比较简单,移植性还可以。只要是满足lcd1602的接口时序,都可以正常点亮显示。只是显示内容相对于比较简单,只有16x2个字符。为了减少连线,采用4线式来驱动lcd1602,下图为lcd1602和树莓派的连接示意图,途中的红线表示电源5V,黑色表示电源地。
    lcd1602连接1.png
    下面是LCD1602的python程序,运行下面程序之前需要首先安装rpi.gpio驱动。

    class Adafruit_CharLCD(object):

        # commands
        LCD_CLEARDISPLAY        = 0x01
        LCD_RETURNHOME          = 0x02
        LCD_ENTRYMODESET        = 0x04
        LCD_DISPLAYCONTROL      = 0x08
        LCD_CURSORSHIFT         = 0x10
        LCD_FUNCTIONSET         = 0x20
        LCD_SETCGRAMADDR        = 0x40
        LCD_SETDDRAMADDR        = 0x80

        # flags for display entrymode
        LCD_ENTRYRIGHT          = 0x00
        LCD_ENTRYLEFT           = 0x02
        LCD_ENTRYSHIFTINCREMENT =0x01
        LCD_ENTRYSHIFTDECREMENT =0x00

        # flags for display on/offcontrol
        LCD_DISPLAYON           = 0x04
        LCD_DISPLAYOFF          = 0x00
        LCD_CURSORON            = 0x02
        LCD_CURSOROFF           = 0x00
        LCD_BLINKON             = 0x01
        LCD_BLINKOFF            = 0x00

        # flags for display/cursorshift
        LCD_DISPLAYMOVE         = 0x08
        LCD_CURSORMOVE          = 0x00

        # flags for display/cursorshift
        LCD_DISPLAYMOVE         = 0x08
        LCD_CURSORMOVE          = 0x00
        LCD_MOVERIGHT           = 0x04
        LCD_MOVELEFT            = 0x00

        # flags for function set
        LCD_8BITMODE            = 0x10
        LCD_4BITMODE            = 0x00
        LCD_2LINE               = 0x08
        LCD_1LINE               = 0x00
        LCD_5x10DOTS            = 0x04
        LCD_5x8DOTS             = 0x00

        def __init__(self,pin_rs=26, pin_e=19, pins_db=[13, 6, 5, 11], GPIO=None):
            # Emulate the oldbehavior of using RPi.GPIO if we haven't been given
            # an explicit GPIOinterface to use
            if not GPIO:
                import RPi.GPIO asGPIO
               GPIO.setwarnings(False)
            self.GPIO = GPIO
            self.pin_rs = pin_rs
            self.pin_e = pin_e
            self.pins_db = pins_db

            self.GPIO.setmode(GPIO.BCM)
           self.GPIO.setup(self.pin_e, GPIO.OUT)
           self.GPIO.setup(self.pin_rs, GPIO.OUT)

            for pin inself.pins_db:
               self.GPIO.setup(pin, GPIO.OUT)

           self.write4bits(0x33)  #initialization
           self.write4bits(0x32)  #initialization
           self.write4bits(0x28)  # 2 line5x7 matrix
           self.write4bits(0x0C)  # turncursor off 0x0E to enable cursor
           self.write4bits(0x06)  # shiftcursor right

            self.displaycontrol =self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF

            self.displayfunction =self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS
            self.displayfunction|= self.LCD_2LINE

            # Initialize todefault text direction (for romance languages)
            self.displaymode =self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
           self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)  # set the entry mode

            self.clear()

        def begin(self, cols,lines):
            if (lines > 1):
                self.numlines = lines
               self.displayfunction |= self.LCD_2LINE

        def home(self):
           self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
           self.delayMicroseconds(3000)  #this command takes a long time!

        def clear(self):
           self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
           self.delayMicroseconds(3000)  #3000 microsecond sleep, clearing the display takes a long time

        def setCursor(self, col,row):
            self.row_offsets =[0x00, 0x40, 0x14, 0x54]
            if row >self.numlines:
                row =self.numlines - 1  # we count rowsstarting w/0
           self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))

        def noDisplay(self):
            """Turn the display off (quickly) """
            self.displaycontrol&= ~self.LCD_DISPLAYON
           self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)

        def display(self):
            """Turn the display on (quickly) """
            self.displaycontrol |=self.LCD_DISPLAYON
           self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)

        def noCursor(self):
            """Turns the underline cursor off """
            self.displaycontrol&= ~self.LCD_CURSORON
           self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)

        def cursor(self):
            """Turns the underline cursor on """
            self.displaycontrol |=self.LCD_CURSORON
           self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)

        def noBlink(self):
            """Turn the blinking cursor off """
            self.displaycontrol&= ~self.LCD_BLINKON
           self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)

        def blink(self):
            """Turn the blinking cursor on """
            self.displaycontrol |=self.LCD_BLINKON
           self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)

        def DisplayLeft(self):
            """These commands scroll the display without changing the RAM """
           self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE |self.LCD_MOVELEFT)

        defscrollDisplayRight(self):
            """These commands scroll the display without changing the RAM """
           self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE |self.LCD_MOVERIGHT)

        def leftToRight(self):
            """This is for text that flows Left to Right """
            self.displaymode |=self.LCD_ENTRYLEFT
           self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)

        def rightToLeft(self):
            """This is for text that flows Right to Left """
            self.displaymode&= ~self.LCD_ENTRYLEFT
           self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)

        def autoscroll(self):
            """This will 'right justify' text from the cursor """
            self.displaymode |=self.LCD_ENTRYSHIFTINCREMENT
           self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)

        def noAutoscroll(self):
            """This will 'left justify' text from the cursor """
            self.displaymode&= ~self.LCD_ENTRYSHIFTINCREMENT
           self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)

        def write4bits(self, bits,char_mode=False):
            """Send command to LCD """
           self.delayMicroseconds(1000)  #1000 microsecond sleep
            bits = bin(bits)[2:].zfill(8)
           self.GPIO.output(self.pin_rs, char_mode)
            for pin inself.pins_db:
               self.GPIO.output(pin, False)
            for i in range(4):
                if bits =="1":
                   self.GPIO.output(self.pins_db[::-1], True)
            self.pulseEnable()
            for pin inself.pins_db:
               self.GPIO.output(pin, False)
            for i in range(4, 8):
                if bits =="1":
                   self.GPIO.output(self.pins_db[::-1][i-4], True)
            self.pulseEnable()

        def delayMicroseconds(self, microseconds):
            seconds = microseconds/ float(1000000)  # divide microsecondsby 1 million for seconds
            sleep(seconds)

        def pulseEnable(self):
           self.GPIO.output(self.pin_e, False)
           self.delayMicroseconds(1)       #1 microsecond pause - enable pulse must be > 450ns
           self.GPIO.output(self.pin_e, True)
           self.delayMicroseconds(1)       #1 microsecond pause - enable pulse must be > 450ns
            self.GPIO.output(self.pin_e,False)
           self.delayMicroseconds(1)       #commands need > 37us to settle

        def message(self, text):
            """Send string to LCD. Newline wraps to second line"""
            for char in text:
                if char == '\n':
                    self.write4bits(0xC0)  # next line
                else:
                   self.write4bits(ord(char), True)


    if __name__ == '__main__':
        lcd = Adafruit_CharLCD()
        lcd.clear()
        lcd.message("  Adafruit 16x2\n  Standard LCD")
    IMG_20171214_235536.jpg

    1.4  配置邮件发送
    当红外采集到有人入侵时,此时会打开摄像头进行拍照,同时打开LED灯辅助照片,增加拍照的质量。

    1.5  上传到百度网盘
    首先需要安装两个库,在树莓派终端界面输入:sudo pip install requests 和sudo pip install bypy
    安装成功以后输入:sudo bypy.py info
    出现界面复制“ PleaseVisit:”下的网址,登陆百度账号复制授权码,这样就完成了安装和授权。授权成功后会看到授权网盘的总容量和已使用容量等信息。
    把当前目录同步到云盘,输入命令:bypy.py upload
    把云盘内容同步到本地来,输入命令:bypy.py downdir或bypy.py syncdown1
    有一点需要注意的是,由于百度API的限制,上传和下载路径都在  /我的应用数据/bypy

    1.6   手机和电脑都可以登录网盘远程查看。 我的电脑端网盘拍照图片.PNG

    Screenshot_20171214-233839.png


    1.7 整体照片
    IMG_20171210_200502.jpg

    image1.jpg

    image2.jpg 1.8 程序运行截图
    抓取图像截图.PNG

    lcd1602显示.PNG
    19   优酷视频




    我的其它帖子:

    1树莓派DIY之一------晒晒我的树莓派靓照https://www.cirmall.com/bbs/forum ... 85&fromuid=8155
    2 树莓派DIY之二------开启开发环境之远程桌面连接,配置vnc
    https://www.cirmall.com/bbs/forum ... 97&fromuid=8155
    3树莓派DIY之三------配置树莓派发送邮件https://www.cirmall.com/bbs/forum ... 18&fromuid=8155
    4树莓派DIY之四------连接摄像头拍摄照片https://www.cirmall.com/bbs/forum ... =98073&fromuid=8155
    5树莓派DIY之五------连接红外传感器和控制继电器、LCD1602https://www.cirmall.com/bbs/forum ... =98074&fromuid=8155

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则



    手机版|小黑屋|与非网

    GMT+8, 2024-4-24 01:49 , Processed in 0.139759 second(s), 18 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.