What started out as a simple Python program to read and power a Hall Effect Flowmeter for my water filter ballooned into a major project. My original request was to write a Python program to record waterflow using a Digiten flowmeter. I envisioned a log file that I could view using SSH to the RPi. Each step of the way, ChatGPT suggested an improvement. I followed these improvements that included a web interface, weekly reports and https:// access with secure certificates. Wow!
I’ve recorded each of these improvements and plan to step through each iteration.
Basic Measurement with Log
I added the first version of the python program, installed the prerequisite libraries and ran the script hoping to see the log file it generated. Since the flowmeter sensor was not connected, it failed. I’ll connect it and try again.
The Gredia GR-208B registers 2,200 pulses per liter of water. The first program works!
Add Web Interface with Flask
ChatGPT suggested visual graphs and a web dashboard. The python 3 package manager, pip3, failed to install flask because created a conflict between pip3 and the linux package manager, apt. The best solution was to run flask inside a virtual environment. There is a new section in “Raspberry PI Water Flow Meter Installation”. This involved setting flask up as systemd service so that it starts automatically at boot.
The web version had an error that caused a “GPIO busy” error. Using the argument “debug=True” makes all GPIO’s busy. Removing it resolves the problem. The second program works!
Activate_this.py missing. Use:
activator = 'some/path/to/activate_this.py' # Looted from virtualenv; should not require modification, since it's defined relatively
with open(activator) as f:
exec(f.read(), {'__file__': activator})
What is Flask?
After a few fumbles, I corrected some errors and got a functioning web page. However, I didn’t understand the code, especially the Flask part. I found an excellent video with 2 million hits but it was five years old. Almost half way through, the examples didn’t work. I may try this video. This video from Udemy helped a lot. As much as ChatGPT and Gemini have helped, I need a better foundation. It’s probably time to go back to basics with the excellent 100 Days of Code: The Complete Python Pro Bootcamp with Dr. Angela Yu.
Sending Email from Python (Gemini)
The absolute simplest way to send a message from Python, without relying on external services or complex setups, is to send an email. While not an SMS, it’s often a suitable alternative for simple notifications. Here’s how:
Using Python’s smtplib and email modules:
Python
import smtplib
from email.mime.text import MIMEText
def send_email(sender_email, sender_password, recipient_email, subject, message):
"""Sends an email message."""
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = recipient_email
try:
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server: #Or your email server
server.login(sender_email, sender_password)
server.send_message(msg)
print("Email sent successfully!")
except Exception as e:
print(f"Error sending email: {e}")
# Example usage (replace with your details)
sender_email = "your_email@gmail.com" # Or other email provider
sender_password = "your_email_password" # Or app password.
recipient_email = "recipient@example.com"
subject = "Python Message"
message = "This is a simple message from Python."
send_email(sender_email, sender_password, recipient_email, subject, message)
Explanation and Important Notes:
- Import Modules:
smtplib: For sending emails using the Simple Mail Transfer Protocol (SMTP).email.mime.text.MIMEText: For creating email messages.
send_emailFunction:- Takes sender, recipient, subject, and message as input.
- Creates a
MIMETextobject to represent the email. - Sets the subject, sender, and recipient headers.
- Uses
smtplib.SMTP_SSLto connect to an SMTP server (in this example, Gmail’s). - Logs in using the sender’s email and password.
- Sends the message using
server.send_message(). - Includes error handling using a
try...exceptblock.
- Example Usage:
- Replace the placeholder email addresses and password with your actual credentials.
- Change the subject and message as needed.
Key Considerations:
- Email Provider:
- You’ll need to use the correct SMTP server and port for your email provider (e.g.,
smtp.outlook.comfor Outlook,smtp.mail.yahoo.comfor Yahoo). - Port 465 is commonly used for SMTP over SSL.
- You’ll need to use the correct SMTP server and port for your email provider (e.g.,
- Security:
- Never hardcode your actual email password directly into your scripts. This is a major security risk.
- For Gmail, you’ll likely need to generate an “app password” in your Google account settings. This is a password specifically for applications.
- Consider using environment variables or a configuration file to store your credentials more securely.
- Two-Factor Authentication (2FA):
- If you have 2FA enabled on your email account, you’ll need to use an app password.
- Spam Filters:
- Emails sent from scripts might sometimes end up in spam folders.
Advantages:
- Simplicity: No external services or complex configurations are required.
- Built-in Modules: Python’s standard library provides the necessary tools.
Disadvantages:
Security: Requires care to protect your email credentials.
Not SMS: This sends emails, not SMS messages.
Configuration: Requires some configuration related to your email provider.