找到
2
篇与
邮件
相关的结果
-
树莓派访客拍照C语言版 前引 不知道因为什么原因Picamera2包调用摄像头拍照,在一个程序中达到一定次数之后就会导致系统的内存泄漏,除了重启软件没有什么好的方法来解决,但作为一个监控性质的软件,经常发生这种错误还是比较无法接受的事情。 因此决定看看能不能直接使用C语言来完成监控人员到访和录像这一系列动作 代码 代码部分十分简单 #include <pigpio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/wait.h> #include <time.h> int pinNumber = 18; int getVideo(){ time_t timep; struct tm *p; char filePath[28]; time (&timep); p=gmtime(&timep); int hour = 8+p->tm_hour; sprintf(filePath,"%s%d%02d%02d%02d%02d%02d%s","/Disk/MyCloud/Stream/Safe/",1900+p->tm_year,1+p->tm_mon,p->tm_mday,(hour>=24)?(hour-24):hour,p->tm_min,p->tm_sec,".mp4"); char command[1000]; sprintf(command,"%s%s%s%s", "libcamera-vid -t 5000 --width 1280 --height 720 --codec libav -o ",filePath," && sudo chown www-data ",filePath); return system(command); } int main() { int value = 0; if (gpioInitialise() < 0) { printf("pigpio initialization failed\n"); return 1; } gpioSetMode(pinNumber, PI_INPUT); while(1){ value = gpioRead(pinNumber); if(value==1){ getVideo(); } time_sleep(1.0); } gpioTerminate(); return 0; }由于在之前 使用的GPIO库wiringPi.h已经暂停维护,在新版的树莓派系统中不在默认安装,这里使用了pigpio.h库 如果没有安装 sudo apt update sudo apt install pigpio 编译 gcc monitor.c -o monitor -lpigpio -lrt -lpthread 引脚编号 默认为BCM编号 主要拍照代码 system(command)实际是为了调用系统的libcamera-vid 即 sprintf(command,"%s%s%s%s", "libcamera-vid -t 5000 --codec libav -o ",filePath," && sudo chown www-data ",filePath);这部分代码为录一个5秒的视频并保存到filePath目录下,同时修改视频权限方便给Nextcloud修改查看 人体红外代码 int value = 0; if (gpioInitialise() < 0) { printf("pigpio initialization failed\n"); return 1; } gpioSetMode(pinNumber, PI_INPUT); while(1){ value = gpioRead(pinNumber); if(value==1){ getVideo(); } time_sleep(1.0); } gpioTerminate();当有人到访时输出高电平,检测到高电平时触发录像 -
树莓派开机自动发送邮件 在平时使用树莓派的过程中,大多数人的习惯都是直接使用终端去连接树莓派,很少使用屏幕去直接操作;而在连接终端的时候又必须要知道树莓派的IP地址;这时候如果树莓派能够通过邮件的方式直接将自己的IP地址直接发送给我们,在连接时就会省去很多不必要的麻烦。 安装软件 安装mutt:$sudo apt-get install mutt 安装msmtp:$sudo apt-get install msmtp 设置软件 设置mutt 编辑/etc/Muttrc 系统全局设置 编辑~/.muttrc 为某个系统用户设置 这里对全局进行设置 $sudo vim /etc/Muttrc 在文件最后面添加如下代码 set sendmail="/usr/bin/msmtp" set use_from=yes set realname="Raspberry" #邮箱发送人昵称 set from=xxxxx@xxx.com #自己的发件箱邮箱地址 set envelope_from=yes set crypt_use_gpgme=no 登录邮箱,进入邮箱设置界面,开启邮箱的IMAP/SMTP服务 下图为163邮箱 NHVGLK3YN1G68YJBOYT图片 设置msmtp 创建 ~/.msmtprc 和 ~/.msmtp.log 两个文件分别作为msmtp的配置文件和日志文件 编辑.msmtprc vim ~/.msmtprc account default host smtp.163.com #自己邮箱的smtp地址,我的是163邮箱,所以是这个 from mstz1130@163.com #自己的邮箱地址 auth plain user mstz1130 #自己的邮箱用户名,就是@前的 password xxxxxxxx #这个并不是邮箱密码,是邮箱授权码 logfile ~/.msmtp.log #日志文件地址由于password是明码,所以我们要修改文件的权限 $sudo chmod 600 .msmtprc 163邮箱授权码的开启位置,其他邮箱的具体位置请自行查询。 2FUYGZLJWT4DM@RES图片 测试邮件发送 $ echo "正文" | mutt -s ”主题“ xxx@xx.com -q 附件 获取IP地址脚本 可以在任意可执行位置创建 $vim sendip.sh #!/bin/bash # check network availability SITE_TO_CHECK="www.163.com" while true do TIMEOUT=5 RET_CODE=`ping -s 1 -c 1 $SITE_TO_CHECK` if [ "$?" != "0" ] then echo "Network not ready, wait..." sleep 1s else echo "Network OK, will send mail..." break fi done # get the IP address of eth0, e.g. "192.168.x.x" WLAN0_IP_ADDR=`ifconfig wlan0 | sed -n "2,2p" | awk '{print substr($2,1)}'` ETH0_IP_ADDR=`ifconfig eth0 | sed -n "2,2p" | awk '{print substr($2,1)}'` ETH1_IP_ADDR=`ifconfig eth1 | sed -n "2,2p" | awk '{print substr($2,1)}'` ETH2_IP_ADDR=`ifconfig eth2 | sed -n "2,2p" | awk '{print substr($2,1)}'` # send the Email echo "Current time: `date '+%F %T'` WLAN0 IP Address of Raspberry Pi: $WLAN0_IP_ADDR ETH0 IP Address of Raspberry Pi: $ETH0_IP_ADDR IP address loading completed ! ! !" | mutt -s "IP Address of Raspberry Pi" xxx@xxx.com写入开机启动项 $sudo vim /etc/rc.local 在exit 0之前加入以下命令 su pi -c /home/pi/sh/sentIP.sh &具体路径由自己创建的脚本地址位置来决定,&表示并发执行,在脚本运行时不影响其他脚本