how to send sensor data to the user using email
我目前正在使用树莓派并使用 DHT11 每秒读取温度和湿度值。我必须通过电子邮件将通知发送给用户。这是我每秒显示传感器数据的代码,我不知道如何向用户发送电子邮件。我只需要在湿度低于 40 时发送通知。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import RPi.GPIO as GPIO
import dht11 import time import datetime import os
# initialize GPIO instance = dht11.DHT11(pin=dht11_pin) while True: cnt += 1 result = instance.read() if previous_temperature != result.temperature or previous_humidity != result.humidity: previous_temperature = result.temperature counter += 1 if result.humidity>=40: else: else: time.sleep(sleep_time) |
您可以这样发送电子邮件:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 |
import smtplib
sender = ‘my@address.de’ # e.g. gmail account |
- 不客气,如果它回答了您的问题,请给它一个绿色复选标记
- 实际上,我想发送湿度小于 40 的结果。我想发送日期、时间、温度和湿度结果,当时传感器检测到湿度小于 40。
- 那么只少了一个简单的步骤:将发送邮件的代码放入else语句中,并在msg[‘Subject’]中包含result.humidity
- 抱歉,我不明白
- 如果我知道你的误解在哪里…… ;) 上帝保佑你!
- 我不明白这一点:将发送电子邮件的代码放入 else 语句中,并在 msg[\\’Subject\\’] 中包含 result.humidity
- 好的。谢谢你帮助我。
来源:https://www.codenong.com/51577694/