Python新手求助:实现HX711称重10839-10850范围邮件告警
Hey Jon, let's work through this problem to get your email alert working reliably!
The core issues you're facing are:
- HX711's occasional spikes triggering false alerts when using a simple upper threshold
- Missing the exact value
10840because of your 5-second sampling interval
Here's a robust solution that combines range checking with a debounce mechanism to filter out spikes, plus a flag to avoid duplicate emails:
Step-by-Step Solution
- Use a range condition instead of exact value matching to catch all valid readings in your target window.
- Add a debounce counter to ignore single-sample spikes (since HX711 spikes are usually short-lived).
- Keep a state flag to ensure you only send one email per valid "weight in range" event (instead of spamming emails while the weight stays in the window).
Modified Code Example
import smtplib # Assume your HX711 and LCD setup code is above this # Initialize global state variables (put these outside your main loop) email_sent_flag = 0 # 0 = not sent, 1 = already sent for this event stable_reading_count = 0 # Require 3 consecutive valid readings to trigger (adjust based on your spike frequency) required_stable_readings = 3 # Inside your main sampling loop: w = hx711.get_weight() # Get current weight reading hx711.power_down() hx711.power_up() # Check if weight is in your target range if 10839 <= w <= 10850: stable_reading_count += 1 # Only trigger if we have multiple consecutive valid readings AND haven't sent the email yet if stable_reading_count >= required_stable_readings and email_sent_flag == 0: # Update LCD lcdclear() lcdprint("Fuel Cell Has Been Replenished") setCursor(0,1) lcdprint("Sending Email") # Send email with error handling try: server = smtplib.SMTP('mail.jon2.com', 587) server.starttls() server.login("jon2@jon2.com", "aaaa1111") # Properly format the email with a subject line msg = "Subject: Fuel Cell Replenished Alert\n\nFuel Cell Has Been Replenished" # Correct sendmail parameters: sender, recipient, message server.sendmail("jon2@jon2.com", "jon1@jon2.co.uk", msg) server.quit() email_sent_flag = 1 # Mark email as sent to avoid duplicates print("Alert email sent successfully!") except Exception as e: print(f"Failed to send email: {str(e)}") # Reset flag if sending failed so we can try again email_sent_flag = 0 else: # Reset counters if weight is outside the range stable_reading_count = 0 # Optional: Reset the flag if you want to re-trigger the email if weight re-enters the range later # email_sent_flag = 0
Key Improvements Explained
- Range Check:
10839 <= w <= 10850ensures you don't miss valid readings because of sampling timing. - Debounce Counter:
stable_reading_countfilters out single-sample spikes—only consecutive valid readings trigger the alert. Adjustrequired_stable_readings(e.g., to 5) if you still see false triggers. - State Flag:
email_sent_flagprevents multiple emails from being sent while the weight stays in the range. If you want to re-alert if the weight leaves and re-enters the range, uncomment theemail_sent_flag = 0line in theelseblock. - Proper Email Format: Added a subject line and corrected the
sendmailparameters (your original code was missing the recipient argument in the right place). - Error Handling: Wrapped the email logic in a
try-exceptblock to catch issues like network errors, and reset the flag if sending fails so you can retry.
Extra Tips
- Calibrate your HX711 module regularly to reduce baseline noise and spikes.
- If you notice frequent spikes, consider adding a low-pass filter to your weight readings (e.g., average the last 5 readings instead of using a single sample).
内容的提问来源于stack exchange,提问作者Jon
相关产品推荐
相关产品推荐

