The Automated Weather Data Logging System is a Python-based project designed to collect, process, and store real-time weather data using the OpenWeatherMap API.
The goal is to automate weather monitoring by fetching current conditions for multiple cities and storing them in a MySQL database for further analysis, reporting, or visualization.
This project demonstrates how to build an end-to-end data automation pipeline β from data extraction (API) to transformation (Pandas) and finally to loading (MySQL).
It can be extended to build weather dashboards, forecasting models, or daily summary reports.
β
Fetch real-time weather data automatically using API
β
Transform raw JSON into a clean tabular format using Pandas
β
Store weather information securely in a MySQL database
β
Schedule automatic data collection at fixed intervals
β
Enable historical weather trend analysis and insights
By completing this project, you will learn:
- How to use REST APIs in Python (
requestslibrary) - How to manage API keys securely using
.envfiles - How to connect Python with MySQL databases
- How to build ETL pipelines (Extract β Transform β Load)
- How to automate data collection with
time.sleep()or schedulers - How to prepare weather data for analytics and visualization
Automated Weather Data Logging into MySQL Database/ β βββ Automated Weather Data Logging into MySQL Database.ipynb # Jupyter notebook (core script) βββ .env # API and DB credentials (not uploaded) βββ weather_data.csv # saved dataset export βββ README.md # Project documentation βββ requirements.txt # Python dependencies βββ /sql/ βββ create_weather_table.sql # SQL table structure
| Tool / Library | Purpose |
|---|---|
| Python 3.x | Main programming language |
| OpenWeatherMap API | Source of real-time weather data |
| Requests | Fetching API data |
| Pandas | Data cleaning and transformation |
| SQLAlchemy | MySQL database connection |
| PyMySQL | MySQL connector driver |
| Dotenv | Managing API and DB credentials securely |
| Time / Datetime | Scheduling and timestamping records |
| MySQL Server | Data storage for long-term analysis |
git clone https://github.com/yourusername/Automated Weather Data Logging into MySQL Database-automation.git
cd Automated Weather Data Logging into MySQL Database-automationMake sure Python and pip are installed.
pip install requests pandas sqlalchemy pymysql python-dotenvCreate a .env file in your project root:
OPENWEATHER_API_KEY=your_api_key_here
DB_USER=root
DB_PASS=your_password
DB_HOST=localhost
DB_NAME=weather_dbOpen your MySQL shell or Workbench:
CREATE DATABASE weather_db;import requests
import pandas as pd
import os
import time
from datetime import datetime
from sqlalchemy import create_engine
from dotenv import load_dotenvload_dotenv()
API_KEY = os.getenv("OPENWEATHER_API_KEY")
DB_CONFIG = {
'user': os.getenv("DB_USER"),
'password': os.getenv("DB_PASS"),
'host': os.getenv("DB_HOST"),
'database': os.getenv("DB_NAME")
}CITIES = ["Chennai", "Delhi", "Mumbai", "Kolkata", "Bangalore"]def fetch_weather(city):
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
response = requests.get(url)
data = response.json()
return {
"city": city,
"temperature": data["main"]["temp"],
"humidity": data["main"]["humidity"],
"weather": data["weather"][0]["description"],
"wind_speed": data["wind"]["speed"],
"timestamp": datetime.now()
}def store_to_mysql(df):
engine = create_engine(
f"mysql+pymysql://{DB_CONFIG['user']}:{DB_CONFIG['password']}@{DB_CONFIG['host']}/{DB_CONFIG['database']}"
)
df.to_sql("weather_data", con=engine, if_exists="append", index=False)def automate_weather():
all_data = []
for city in CITIES:
record = fetch_weather(city)
all_data.append(record)
time.sleep(1) # Avoid rate limit
df = pd.DataFrame(all_data)
store_to_mysql(df)
print("β
Data logged successfully at", datetime.now())
automate_weather()| Column | Type | Description |
|---|---|---|
city |
VARCHAR(50) | City name |
temperature |
FLOAT | Temperature in Celsius |
humidity |
INT | Humidity percentage |
weather |
VARCHAR(100) | Description of weather (e.g., clear sky) |
wind_speed |
FLOAT | Wind speed in m/s |
timestamp |
DATETIME | Date and time of data collection |
| city | temperature | humidity | weather | wind_speed | timestamp |
|---|---|---|---|---|---|
| Chennai | 32.5 | 66 | scattered clouds | 2.3 | 2025-10-28 10:00:00 |
| Delhi | 28.4 | 54 | clear sky | 1.9 | 2025-10-28 10:00:00 |
flowchart TD
A[Start Script] --> B[Load Environment Variables]
B --> C[Fetch Data from OpenWeather API]
C --> D[Transform JSON to DataFrame]
D --> E[Store Data into MySQL]
E --> F[Automate with Scheduler]
- Build Power BI or Tableau dashboards with weather data
- Compare temperature and humidity trends across cities
- Use for machine learning models (e.g., rainfall prediction)
- Integrate with IoT devices or home weather stations
- Add forecast API for next 5 or 7 days
- Enable email or Telegram alerts for extreme conditions
- Store data in cloud databases (AWS RDS, Google Cloud SQL)
- Add real-time dashboards using Power BI or Streamlit