Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据Arduino向串行端口发送的通知向gmail发送电子邮件_Python_Arduino_Serial Port_Gmail_Email Notifications - Fatal编程技术网

Python 根据Arduino向串行端口发送的通知向gmail发送电子邮件

Python 根据Arduino向串行端口发送的通知向gmail发送电子邮件,python,arduino,serial-port,gmail,email-notifications,Python,Arduino,Serial Port,Gmail,Email Notifications,因此,我正在尝试做一个工厂监控环境项目,我让arduino/嵌入式端工作。其想法是,当传感器中的数据低于某个阈值时,嵌入式端通过蓝牙模块向该python脚本正在侦听的串行端口发送一个字符串。如果脚本在端口上检测到一个字符串,它将向输入的电子邮件发送电子邮件消息。 然而,就目前情况而言,它似乎没有将电子邮件发送给用户输入的电子邮件,我不确定问题是否在于我的发送电子邮件方法或主循环本身,但在填写所有用户输入值后,它只是循环,永远不做任何事情,甚至不打印“未发送电子邮件”案例,我目前不确定原因。此外,

因此,我正在尝试做一个工厂监控环境项目,我让arduino/嵌入式端工作。其想法是,当传感器中的数据低于某个阈值时,嵌入式端通过蓝牙模块向该python脚本正在侦听的串行端口发送一个字符串。如果脚本在端口上检测到一个字符串,它将向输入的电子邮件发送电子邮件消息。 然而,就目前情况而言,它似乎没有将电子邮件发送给用户输入的电子邮件,我不确定问题是否在于我的发送电子邮件方法或主循环本身,但在填写所有用户输入值后,它只是循环,永远不做任何事情,甚至不打印“未发送电子邮件”案例,我目前不确定原因。此外,getpass3功能存在一些问题,并且在终端中对密码进行了置乱,因此目前我已经切换到原始输入,一旦我获得电子邮件发送功能,我计划切换回getpass3并修复它,就像它之前所做的一样,只需键入并显示输入密码的提示,然后挂起

这是我用python编写的第二个程序,所以我完全可能误解了其中一些方法是如何调用或传递的,但从我在文档中看到的情况来看,我没有发现这方面有任何错误。我之前已经让adruino通过串行端口发送信息,但是我注意到蓝牙设备配对可能存在问题,作为参考,该模块是HC-05,我之前已经让它将信息发送到TeraTerm

import time
import serial
import smtplib
import colorama
import getpass3

useremail = raw_input("Enter your email ")
# getpass3.set_echo = '#'
# userpass = getpass3.getpass("Enter your password: ")
userpass = raw_input("Enter your password ")
usercom = raw_input("Please Enter COMPORT: ").upper()
print usercom

TO = useremail
GMAIL_USER = useremail
GMAIL_PASS = userpass #plaintext need to get getpass3 working i hate this way

SUBJECT = 'Plant Environment Needs Attention'
TEXT1 = 'Low Light Levels Detected in the Enclosure'
TEXT2 = 'Low Mositure in the Soil Detected in the Enclosure'
TEXT3 = 'Temperature Outside of Safe Range in Enclosure'
TEXT4 = 'Humidity Outside of Safe Range in Enclosure'

ser = serial.Serial(usercom, 9600) #seems to always be COM9 on my PC can adjust as needed

def send_email(TEXT):
    print("Sending Email")
    #SMTP server setup
    smtpserver = smtplib.SMTP("smtp.gmail.com",587)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo()
    #login to gmail can potentially add diff email client if wanted
    smtpserver.login(GMAIL_USER, GMAIL_PASS)
    header = 'To:' + TO + '\n' + 'From: ' + GMAIL_USER
    header = header + '\n' + 'Subject:' + SUBJECT + '\n'
    # #form the diff cases here based off if its temp hum ect
    print header
    msg = header + '\n' + TEXT + ' \n\n'
    #send email and close server for security reasons
    smtpserver.sendmail(GMAIL_USER, TO, msg)
    smtpserver.close()

# just do it forever
while True:
    message = ser.readline()
    print(message)
    #big brained implamentation
    #L = light
    if message[0] == 'L' :
        send_email(TEXT1)
        print("Email Sent")
    #S = Soil Moisutre
    if message[0] == 'S' :
        send_email(TEXT2)
        print("Email Sent")
    #T = temp
    if message[0] == 'T' :
        send_email(TEXT3)
        print("Email Sent")
    #H = Humidity
    if message[0] == 'H' :
        print("Email Sent")
        send_email(TEXT4)
    else:
        print("No Email Sent")
    time.sleep(0.5)

只是猜测,但脚本可能不会发送电子邮件,原因是:
消息[0]='L'
,您可以尝试
if message.lower().startswith(“L”):…
只是猜测,但脚本可能不会发送电子邮件,原因是:
消息[0]='L'
,您可以尝试
if message.lower().startswith(“L”):…