Become a leader in the IoT community!

New DevHeads get a 320-point leaderboard boost when joining the DevHeads IoT Integration Community. In addition to learning and advising, active community leaders are rewarded with community recognition and free tech stuff. Start your Legendary Collaboration now!

Step 1 of 5

CREATE YOUR PROFILE *Required

Change Email
OR
Step 2 of 5

WHAT BRINGS YOU TO DEVHEADS? *Choose 1 or more

Collaboration & Work 🤝
Learn & Grow 📚
Contribute Experience & Expertise 🔧
Step 3 of 5

WHAT'S YOUR INTEREST OR EXPERTISE? *Choose 1 or more

Hardware & Design 💡
Embedded Software 💻
Edge Networking
Step 4 of 5

Personalize your profile

Step 5 of 5

Read & agree to our COMMUNITY RULES

  1. We want this server to be a welcoming space! Treat everyone with respect. Absolutely no harassment, witch hunting, sexism, racism, or hate speech will be tolerated.
  2. If you see something against the rules or something that makes you feel unsafe, let staff know by messaging @admin in the "support-tickets" tab in the Live DevChat menu.
  3. No age-restricted, obscene or NSFW content. This includes text, images, or links featuring nudity, sex, hard violence, or other graphically disturbing content.
  4. No spam. This includes DMing fellow members.
  5. You must be over the age of 18 years old to participate in our community.
  6. Our community uses Answer Overflow to index content on the web. By posting in this channel your messages will be indexed on the worldwide web to help others find answers.
  7. You agree to our Terms of Service (https://www.devheads.io/terms-of-service/) and Privacy Policy (https://www.devheads.io/privacy-policy)
By clicking "Finish", you have read and agreed to the our Terms of Service and Privacy Policy.

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)
  1. Renuel Roberts#0000

    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?

  2. ke7c2mi#0000

    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

  3. ke7c2mi#0000

    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

  4. ke7c2mi#0000

    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

  5. ke7c2mi#0000

    P.P.S. I strongly suspect you need a non linear SVM to get anything useful

  6. Renuel Roberts#0000

    @mike7c2 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.

  7. Renuel Roberts#0000

    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?

  8. Renuel Roberts#0000

    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.

  9. ke7c2mi#0000

    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

  10. Renuel Roberts#0000

    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.

  11. Renuel Roberts#0000

    Hello @mike7c2 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

  12. Renuel Roberts#0000

    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 640×480)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

CONTRIBUTE TO THIS THREAD

Browse other questions tagged