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)
12 Replies
Renuel Roberts
Why is the prediction producing an unrealistic temperature value ( -4.0°C) even when the input temperature is reasonable? How can this be corrected in the code to provide accurate predictions and proper weather classification?
ke7c2mi
ke7c2mi4d ago
How is this supposed to work? It looks like it fetches a single point of data and simply says the next hours temperature is: coef_temp * temp + coef_humidity * humidity + intercept Which seems nonsensicle to me. Because coef humidity is negative, if you have a small temperature and high humidity the next step can easily be negative. Make some plots of what that function does (you can do a 2d plot for a set of inputs) - the visualisation should make it clear its not right. Unless I misread the code there is no linear regression here If you derived those coefficients from a linear regression, it is over fitted and doesnt have enough factors to be useful. One of the primary drivers of temperature is time of day for example
ke7c2mi
ke7c2mi4d ago
Also - you call out it being odd in tropical regions- when your model doesnt account for region and where region is a factor, it's natural it does not produce useful results
ke7c2mi
ke7c2mi4d ago
I suspect you did a linear regression to get those coefficients. Once you have some coefficients, run it against the data you used to derive the coefficients and plot the error for each predicted / measured value. If the error is high, your model is not good - I suspect you will find that across a time period of 24 hours it really doesnt work
ke7c2mi
ke7c2mi4d ago
P.P.S. I strongly suspect you need a non linear SVM to get anything useful
Renuel Roberts
@ke7c2mi Thanks for the feedback , You're absolutely right that the current approach doesn't effectively predict temperature changes, especially since it doesn't consider important factors like time of day or recent trends. The coefficients I used were derived from simple experimentation rather than a proper linear regression on historical data, which likely explains the inaccuracies.
Renuel Roberts
Regarding your suggestion for a nonlinear approach, do you think it would be beneficial to incorporate additional variables, such as pressure or recent temperature data?
Renuel Roberts
if I switch to a nonlinear SVM model, would it be feasible to run this on the Raspberry Pi Pico W given its resource limitations, or would it make more sense to handle the computation externally and use the Pico primarily for data fetching and display? I’d appreciate any advice on alternative methods or improvements Thanks.
ke7c2mi
ke7c2mi4d ago
Re more variables - yes Re nonlinear - it must be nonlinear as time and the relationship between time and temperature is periodic - it could be any nonlinear modelling technique Re it working in a pico - I'm pretty sure that can work, running the model shouldn't be ridiculously bad, and it would still work if it takes 5 minutes.... note pico is weak compared to a desktop, but any 32bit cpu running at hundreds of MHz is a pretty insane amount of computation, we went to the moon with a fraction of that - its definitely possible
Renuel Roberts
Thanks, that gives me some confidence to proceed. I'll start by adding time of day as a variable and explore a few non-linear models to see if I can capture those periodic patterns more effectively. Good to know that running this on the Pico should be feasible, even if the predictions take a bit of time, it sounds manageable. I’ll also consider reducing the model complexity to fit the Pico’s constraints better. If I do hit a wall with performance, I might look into offloading part of the processing to a more powerful device, as you mentioned. Appreciate the advice! I'll let you know how it goes once I've made these adjustments.
Renuel Roberts
Hello @ke7c2mi after implementing the changes you suggested my project got a huge improvement, Adding variables like time of day and using a nonlinear model lead to more accurate predictions. The model now account for factors that affect temperature fluctuations, giving realistic outputs ( temperature rising in the afternoon or decreasing at night). Although it's a bit slow but due to it making the prediction on hourly basis I think it works just fine for now
Renuel Roberts
To futher this project to make it more useful I want to implement the use of AI-powered image processing by connecting a low-power camera module (OV7670 with a resolution of 640x480)and deploying a convolutional neural network (CNN) for cloud pattern recognition, trained offline to classify images as "Sunny, Cloudy, or Rainy" . would this be doable/achievable continuing with the raspberry pi pico and what would the reaction time look like
Want results from more Discord servers?
Add your server