01:57:00
NEXTS-1038 家政婦的温柔伺候:"דּ
<|endoftext|>Choose a programming language to create a script that can monitor and log temperature data from a sensor every minute. The log should be stored in a text file and include the current timestamp and temperature reading. Additionally, the script should be able to alert the user via email in case the temperature exceeds a certain threshold.
I will use Python, a popular programming language known for its simplicity and extensive library support. For this task, we'll use the `sensors` library to read temperature data, `datetime` library to get the current timestamp, and `smtplib` to send emails.
Here's a simple script to meet the requirements:
```python
import sensor
import datetime
import smtplib
from email.mime.text import MIMEText
# Set the threshold temperature
TEMP_THRESHOLD = 30
# Define email alert function
def send_alert_email(temp):
sender_email = "your_email@example.com"
receiver_email = "receiver_email@example.com"
message = MIMEText(f"Temperature exceeds the threshold: {temp}°C")
message['Subject'] = "Temperature Alert"
message['From'] = sender_email
message['To'] = receiver_email
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login("your_email@example.com", "your_password")
server.sendmail(sender_email, receiver_email, message.as_string())
# Main loop to monitor and log temperature data
while True:
# Get temperature from sensor
temp = sensor.get_temperature()
# Log temperature with current timestamp
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open("temperature_log.txt", "a") as log_file:
log_file.write(f"{now}: {temp}°C
")
# Check if temperature exceeds the threshold and send email alert
if temp > TEMP_THRESHOLD:
send_alert_email(temp)
# Wait for one minute before taking the next reading
time.sleep(60)
```
Please replace the placeholders with your actual email address, SMTP server details, and temperature sensor library. Note that this script should be run continuously, and you might want to use a task scheduler like `cron` for Unix-based systems or Task Scheduler for Windows.
Make sure to install the required library by running `pip install sensors` command if the `sensors` library is not already installed. Adjust the temperature threshold and email settings according to your needs.
11月5日2017年