Renuel Roberts
Renuel Roberts
DIIDevHeads IoT Integration Server
Created by Renuel Roberts on 12/18/2024 in #📡-edge-networking
Resolving Unrealistic Weather Predictions in MicroPython Linear Regression Model on Raspberry Pi Pic
While implementing a networked weather station on the Raspberry Pi Pico W using MicroPython, I encountered an unexpected result in the predicted temperature and weather classification. The code is supposed to fetch real-time weather data and use a simple linear regression model to predict the next hour’s weather, but the output prediction is producing unrealistic temperature values like negative temperatures in tropical regions. Here is the relevant portion of my code:
import network
import urequests
import json

def connect_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

while not wlan.isconnected():
print("Connecting to WiFi...")
time.sleep(1)

print("Connected to WiFi", wlan.ifconfig())

def get_weather(api_key, city):
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = urequests.get(url)
data = json.loads(response.text)
temperature = data['main']['temp']
humidity = data['main']['humidity']
return temperature, humidity

coef_temp = 0.5
coef_humidity = -0.2
intercept = 15

def predict_next_hour(temp, humidity):
next_temp = coef_temp * temp + coef_humidity * humidity + intercept
return next_temp

def classify_weather(temp, humidity):
if temp > 25 and humidity < 60:
return "Sunny"
elif humidity > 80:
return "Rainy"
else:
return "Cloudy"

connect_wifi('********', 'xucgkudwetr,.')
api_key = '*************'
city = 'your_city'

while True:
temp, humidity = get_weather(api_key, city)
next_temp = predict_next_hour(temp, humidity)
weather = classify_weather(next_temp, humidity)

print(f"Current Temp: {temp}°C, Humidity: {humidity}%")
print(f"Predicted Next Hour Temp: {next_temp}°C")
print(f"Predicted Weather: {weather}")

time.sleep(3600)
import network
import urequests
import json

def connect_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

while not wlan.isconnected():
print("Connecting to WiFi...")
time.sleep(1)

print("Connected to WiFi", wlan.ifconfig())

def get_weather(api_key, city):
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = urequests.get(url)
data = json.loads(response.text)
temperature = data['main']['temp']
humidity = data['main']['humidity']
return temperature, humidity

coef_temp = 0.5
coef_humidity = -0.2
intercept = 15

def predict_next_hour(temp, humidity):
next_temp = coef_temp * temp + coef_humidity * humidity + intercept
return next_temp

def classify_weather(temp, humidity):
if temp > 25 and humidity < 60:
return "Sunny"
elif humidity > 80:
return "Rainy"
else:
return "Cloudy"

connect_wifi('********', 'xucgkudwetr,.')
api_key = '*************'
city = 'your_city'

while True:
temp, humidity = get_weather(api_key, city)
next_temp = predict_next_hour(temp, humidity)
weather = classify_weather(next_temp, humidity)

print(f"Current Temp: {temp}°C, Humidity: {humidity}%")
print(f"Predicted Next Hour Temp: {next_temp}°C")
print(f"Predicted Weather: {weather}")

time.sleep(3600)
13 replies