Send Weather Data to Blynk & Save Locally with Python

Do you want to send live weather predictions like “Rainy” or “Windy” to Blynk and also save them locally on your device for later analysis? This Python tutorial shows you exactly how, step by step.


🔹 What You’ll Need

  1. Python installed on your Raspberry Pi, ESP32 with MicroPython, or PC.
  2. Blynk account & project (get your auth token).
  3. Requests library for HTTP calls (pip install requests).


🔹 Updated Code for Weather Predictions

Here’s the modified Python code. Instead of predicting birds, we now predict weather conditions randomly (“Rainy” or “Windy”) and send them to Blynk datastreams. We also log all data in a CSV file locally.

🔹 What You’ll Need

  1. Python installed on your Raspberry Pi, ESP32 with MicroPython, or PC.
  2. Blynk account & project (get your auth token).
  3. Requests library for HTTP calls (pip install requests).

🔹 Updated Code for Weather Predictions

Here’s the modified Python code. Instead of predicting birds, we now predict weather conditions randomly (“Rainy” or “Windy”) and send them to Blynk datastreams. We also log all data in a CSV file locally.

import csv

import time

import requests

import random

# Your Blynk Auth Token

BLYNK_AUTH = "yvvfCzSxViPCN0QTgTdGqfLS4zgJZnu3"

# Function to write value to Blynk virtual pin

def blynk_write(pin, value):

url = f"https://blynk.cloud/external/api/update?token={BLYNK_AUTH}&V{pin}={value}"

requests.get(url)

# Function to randomly predict weather

def predict_weather():

weather_conditions = [

("Rainy", "Wet"),

("Windy", "Dry")

]

return random.choice(weather_conditions)

# Create CSV file with headers if not exists

with open("weatherdata.csv", "a", newline="") as file:

writer = csv.writer(file)

writer.writerow(["Timestamp", "Prediction"])

# Main loop

while True:

timestamp = time.strftime("%Y-%m-%d %H:%M:%S")

# Predict weather

weather_name, category = predict_weather()

if category == "Wet":

prediction = f"Wet: {weather_name}"

blynk_write(1, weather_name)

blynk_write(2, "")

else:

prediction = f"Dry: {weather_name}"

blynk_write(1, "")

blynk_write(2, weather_name)

# Combine into log entry

log_entry = f"{timestamp} {prediction}"

# Send formatted log to Terminal widget on Blynk

blynk_write(5, log_entry + "\n")

# Save clean format to CSV

with open("weatherdata.csv", "a", newline="") as file:

writer = csv.writer(file)

writer.writerow([timestamp, prediction])

print("Logged:", log_entry)

# Wait 5 seconds before next prediction

time.sleep(5)

🔹 How It Works

  1. Random Weather Prediction
  2. The predict_weather() function randomly selects between “Rainy” (Wet) or “Windy” (Dry).

  3. Send Data to Blynk
  4. Virtual pins V1 and V2 display weather based on the type.
  5. Virtual pin V5 shows the full log in a Blynk Terminal widget.

  6. Log Data Locally
  7. Each prediction is also saved to a CSV file weatherdata.csv with a timestamp.
  8. You can open this CSV later for analysis or visualization.

  9. Loop & Delay
  10. The while True loop continuously generates and sends weather data every 5 seconds.

🔹 Why This Is Useful

  1. Perfect for IoT projects, home automation, or weather tracking experiments.
  2. You can replace random predictions with real sensor data (like a rain sensor or anemometer).
  3. Blynk allows remote monitoring while CSV storage gives local backup.

🔹 Next Steps / Enhancements

  1. Add more weather conditions like Sunny, Cloudy, Stormy.
  2. Use real sensor readings instead of random values.
  3. Visualize your data using Blynk graphs or Python libraries like Matplotlib.
  4. Deploy on Raspberry Pi or ESP32 for a small IoT weather station.


💡 Conclusion

With this tutorial, you can send live weather predictions to Blynk and store them locally for analysis. It’s a simple, beginner-friendly project that can be expanded into a full IoT weather monitoring system.





Post a Comment

My Instagram

Made with by Pi-girl | Copyright © 2020 Pi-girl