Simple Weather notification project built with python in mobile.
Tags: beginners, programming, python, webdev
Published: 2023-09-02 07:54:13

```python
import requests
import smtplib
from email.message import EmailMessage
import schedule
import time
# Configuration
EMAIL_USERNAME =  "your@email.com"
EMAIL_PASSWORD =   "yourpassword"  # or your actual email password if using less secure apps
RECIPIENT_EMAIL = "recipient@gmail.com"



def send_weather_notification():
    city = "city"
    url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid=yourapikey&units=metric"
    
    # Rest of your code for fetching weather data and creating email content
    response = requests.get(url)
    weather_data = response.json()

    temperature = weather_data["main"]["temp"]
    description = weather_data["weather"][0]["description"]

    notification_title = "Weather Forecast"
    notification_text = f"Temperature: {temperature}°C\nDescription: {description}"

    # Set up email notification
    msg = EmailMessage()
    msg.set_content(notification_text)
    msg["Subject"] = notification_title
    msg["From"] = EMAIL_USERNAME
    msg["To"] = RECIPIENT_EMAIL

    # Send the email
    with smtplib.SMTP_SSL("smtp.gmail.com") as smtp:
        smtp.login(EMAIL_USERNAME, EMAIL_PASSWORD)
        smtp.send_message(msg)

# Schedule the email to be sent every day at 16:00 (4:00 PM)
schedule.every().day.at("8:00").do(send_weather_notification)

while True:
    schedule.run_pending()
    time.sleep(1)
```


**This is a simple weather forecast project built with termux in mobile which sends you the weather via email.**

## 1. Introduction:

You can change the `youremail@.com`, `yourpassword`, `recipient@gmail.com`, `city` in the code to your respective email address, password, recipient email and city.

## 2. Installation:

To run the script, you'll need Python 3 installed on your system. You can install it according to your specific operating system or desktop environment.

**For example I use Linux mint in my pc and termux in my mobile so the command to install python3 in them is:**

### Debian/Ubuntu-based systems:
```
sudo apt update
sudo apt install python3

```

### Termux:

If you are using termux in android:

```
pkg install python3
```

After installing python3 if you don't have the package manager 'pip' you can install it using this command in the terminal:

```
sudo apt-get install python3-pip

```
### Libraries

Install these libraries:

```
pip3 install requests
pip3 install smtplib
pip3 install schedule

```

**These libraries may already be present in the 'pip' make sure if its there using the above commands.**

```
pip3 install smtplib                                                                     
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement smtplib (from versions: none)
ERROR: No matching distribution found for smtplib

```

If you get it this means that smtplib is not an external package to be downloaded using pip it's actually a part of python's standard library.

## 3. Getting the API key:

I used an api key from this site [https://openweathermap.org/].

You can also get your key by following these steps:

 

- Visit the OpenWeatherMap website at [https://openweathermap.org/].
- Sign up for a free account by clicking on the "Sign Up" or "Join" button on the top right corner of the homepage.
- After signing up, log in to your OpenWeatherMap account.
- Once logged in, navigate to the "API keys" section, usually found in your account settings or profile.
- Generate a new API key by clicking on the appropriate button or link. You may be asked to specify the type of API key you need, such as a "Free" or "Basic" plan key.
- The generated API key will be displayed on the screen. Make sure to copy and securely store this key as you will need it to access OpenWeatherMap's API services.

## 4. Execution:

### Run this in the terminal:

```
python3 weather_notification.py

```
If this command prints to the next line at the scheduled time you'll get the email.

### Running in the background:
If you want to the kill the terminal but want to receive the email use this command:


```
nohup python3 weather_notification.py &

```
The `nohup` command stands for "no hang up." It's used in Unix-like operating systems (including Linux) to run a command or a script in such a way that it can continue running even after you log out or close the terminal session.

`nohup` also redirects the standard output (stdout) and standard error (stderr) streams of the command or script to a file called `nohup.out` in the current directory. This allows you to capture any output or error messages generated by the process.

When you run a command with `nohup`, it can be started in the background (using the `&` symbol) so that you can continue using the terminal for other tasks.