我们正在为学校开展一个项目,我们有2个PIR运动传感器在Arduino微控制器上运行.我们可以在Ardunio IDE和Python IDLE中查看串口的输出.
我们接下来要做的是,在检测到大约30秒的运动后,发出电子邮件警报,看到我们此时没有以太网功能,我们认为最简单的方法是通过Python获取电子邮件.
怎么做到这一点?
此时我们可以发送来自Python的电子邮件,我们可以在Python中阅读Arduino串口,但是我们只是把它放在一起就有问题了.
这就是我们的Python代码看起来的样子,而1:是混乱发生的地方:
import smtplib,serial
ser = serial.Serial(port=2, baudrate=9200)
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
gmail_user = "usr@gmail.com"
gmail_pwd = "pw"
def mail(to, subject, text, attach):
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
while 1: **// CONFUSION HAPPENS HERE //** <----------------------
ser.readline()
if ser.readline() = "motion"
do this mail sequence?
mail("usr2@gmail.com",
"Alarm Alert!",
"Both Motion Sensor A & B have been active for over # seconds",
"stor_fight.jpg")
Run Code Online (Sandbox Code Playgroud)
任何提示将非常感激.
我不确定您在这个过程的哪一部分遇到了问题,但这里是解决方案的草图:
当 Arduino 通过 USB 插入计算机时,您可以使用pyserial库从 python 与 Arduino 进行通信。
在 python 方面,您的代码将如下所示:
serial = serial.Serial("/dev/tty.usbserial-A6007btF", 38400) # the serial name you can see in the Arduino GUI - you might just need to say "COM1" on Windows
result = serial.readline(); # blocks until you get something from the Arduino
if result == "motion":
# send email here
Run Code Online (Sandbox Code Playgroud)
在 Arduino 方面,您只需执行以下操作:
void loop()
{
if(30 seconds have passed with motion)
Serial.println("motion");
}
Run Code Online (Sandbox Code Playgroud)
合理?
| 归档时间: |
|
| 查看次数: |
3966 次 |
| 最近记录: |