【问题标题】:Pibrella + RPI Threading stopPibrella + RPI 穿线停止
【发布时间】:2014-10-07 15:05:45
【问题描述】:

我正在编写一个脚本,一旦按下 Pibrella 上的按钮,就会每 10 秒发送一封电子邮件。我希望它在再次按下按钮时停止。

例如,第一次按下它时,它每 10 秒发送一封电子邮件。然后第二次,它停止线程。

这是我当前的代码,谁能告诉我哪里做错了?

import pibrella, signal
import threading
import time
import smtplib

server = smtplib.SMTP("smtp.gmail.com",587)
server.ehlo()
server.starttls()
server.ehlo()

server.login("email@gmail.com", "EmailPassword")

isPressed = 0

def pressed(pin):
    global isPressed
    if isPressed == 0:
        isPressed = 1
        sendMail()
    else:
        pibrella.light.off()
        t.cancel()
        isPressed = 0

def sendMail():
    pibrella.light.pulse()
    server.sendmail("email@gmail.com", "receiver@gmail.com", "Hello World")
    t.start()

t = threading.Timer(10, sendMail).start()

pibrella.button.pressed(pressed)
pibrella.pause()

server.close()

我现在遇到的错误发布在下面。请注意,电子邮件确实正在发送中。

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/pibrella.py", line 262, in handle_callback
    callback(self)
  File "sendText.py", line 27, in pressed
    sendMail()
  File "sendText.py", line 36, in sendMail
    t.start()
AttributeError: 'NoneType' object has no attribute 'start'
Exception in thread Thread-14:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 760, in run
    self.function(*self.args, **self.kwargs)
  File "sendText.py", line 36, in sendMail
    t.start()
AttributeError: 'NoneType' object has no attribute 'start'

【问题讨论】:

  • 您能告诉我们您当前的代码做了什么以及您认为它失败的原因吗?
  • 用错误信息更新它。

标签: python multithreading loops raspberry-pi email-threading


【解决方案1】:

发送电子邮件的代码(来自我的 Github)

import subprocess
import smtplib
import socket
from email.mime.text import MIMEText
import datetime
#account info
to = 'example@gmail.com'
gmail_user = 'example@gmail.com'
gmail_password = 'your_password'
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.login(gmail_user, gmail_password)
today = datetime.date.today()
arg='ip route list'
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
data=p.communicate()
split_data=data[0].split()
ipaddr=split_data[split_data.index('src')+1]
my_ip='your_message'
msg=MIMEText(my_ip)
msg['Subject']= 'the_subject'
msg['From']= gmail_user
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    相关资源
    最近更新 更多