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
- Python installed on your Raspberry Pi, ESP32 with MicroPython, or PC.
- Blynk account & project (get your auth token).
- 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
- Python installed on your Raspberry Pi, ESP32 with MicroPython, or PC.
- Blynk account & project (get your auth token).
- 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
- Random Weather Prediction
- The predict_weather() function randomly selects between “Rainy” (Wet) or “Windy” (Dry).
- Send Data to Blynk
- Virtual pins V1 and V2 display weather based on the type.
- Virtual pin V5 shows the full log in a Blynk Terminal widget.
- Log Data Locally
- Each prediction is also saved to a CSV file weatherdata.csv with a timestamp.
- You can open this CSV later for analysis or visualization.
- Loop & Delay
- The while True loop continuously generates and sends weather data every 5 seconds.
🔹 Why This Is Useful
- Perfect for IoT projects, home automation, or weather tracking experiments.
- You can replace random predictions with real sensor data (like a rain sensor or anemometer).
- Blynk allows remote monitoring while CSV storage gives local backup.
🔹 Next Steps / Enhancements
- Add more weather conditions like Sunny, Cloudy, Stormy.
- Use real sensor readings instead of random values.
- Visualize your data using Blynk graphs or Python libraries like Matplotlib.
- 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