WoutD
Posts: 21
Joined: Tue Feb 17, 2015 2:06 pm

[SOLVED] how to get temperature values from internet?

Tue Mar 31, 2015 9:12 am

Hi all,

I'm trying to automate my airconditioning with the raspberry pi. For the moment I can control it with the IR-Led I placed and programmed on it. But now I want to be able to use outside temperature to tell the AC if it has to heat or cool. Ideal temperature in the office is 21°C (70°F). So if it's 25°C outside, the AC should cool. If it's 16°C outside, the AC should heat. This is all side-info :)

Over to the problem! So I want to be able to use outside temperature in my program. The best way to do this, I thought, was to get the outside temperature from internet. the problem is that I have no idea how... Also, it should be the outside temperature of my city (obviously! :p) Has anyone an idea on how to get temperature values from the internet?

Kind regards,

Wout
Last edited by WoutD on Thu Apr 02, 2015 1:42 pm, edited 1 time in total.

ondrej1024
Posts: 151
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: [HELP] how to get temperature values from internet?

Tue Mar 31, 2015 9:33 am

Hi,
Weather data for your city can be obtained from http://openweathermap.org. They have an HTTP API that you can use to retrieve the current weather data, which includes the outside temperature. It's all documented on their web page. The API request can be made using e.g. Python or curl in bash scripts. Give it a try.

Ondrej
The Telegea.org project: https://www.telegea.org

User avatar
DougieLawson
Posts: 36098
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: [HELP] how to get temperature values from internet?

Tue Mar 31, 2015 10:03 am

Try this

Code: Select all

#!/usr/bin/python3

from urllib.request import urlopen
import json

apikey="key_redacted" # sign up here http://www.wunderground.com/weather/api/ for a key

url="http://api.wunderground.com/api/"+apikey+"/conditions/q/UK/Basingstoke.json"
meteo=urlopen(url).read()
meteo = meteo.decode('utf-8')
weather = json.loads(meteo)

for key, value in weather['current_observation'].items():
    print (key, value)
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.


WoutD
Posts: 21
Joined: Tue Feb 17, 2015 2:06 pm

Re: [HELP] how to get temperature values from internet?

Tue Mar 31, 2015 2:59 pm

Thank you all for the quick response.

@DougieLawson
Thank you for the code! I had to delete the ".request" to make it work though. I'm also fairly new to API stuff, so now i have all those different weather forecast observations. But I only want the 'temp_c' value out of it... How do I get that one?

@Piotrusc
Same goes for your help. I installed the python-forecastio but I have no clue how to get the current outside temperature value. I'm new to API stuff...

User avatar
DougieLawson
Posts: 36098
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: [HELP] how to get temperature values from internet?

Tue Mar 31, 2015 7:24 pm

Change

Code: Select all

for key, value in weather['current_observation'].items():
    print (key, value)
    print ("---")
into

Code: Select all

cur_temp =weather['current_observation']['temperature_string'].split()
print (cur_temp[2][1:])
to just print the Celsius temp value.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
DougieLawson
Posts: 36098
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: [HELP] how to get temperature values from internet?

Tue Mar 31, 2015 7:44 pm

WoutD wrote: @Piotrusc
Same goes for your help. I installed the python-forecastio but I have no clue how to get the current outside temperature value. I'm new to API stuff...
The same bit of python using forecastio

Code: Select all

#!/usr/bin/python3

from urllib.request import urlopen
import json

apikey="api_key_redacted" # get a key from https://developer.forecast.io/register
# Latitude & longitude - current values are central Basingstoke.
lati="51.2673665"
longi="-1.0817624"

# Add units=si to get it in sensible ISO units not stupid Fahreneheit.
url="https://api.forecast.io/forecast/"+apikey+"/"+lati+","+longi+"?units=si"

meteo=urlopen(url).read()
meteo = meteo.decode('utf-8')
weather = json.loads(meteo)

print (weather['currently']['temperature'])
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

WoutD
Posts: 21
Joined: Tue Feb 17, 2015 2:06 pm

Re: [HELP] how to get temperature values from internet?

Thu Apr 02, 2015 1:40 pm

Thanks a lot DougieLawson! Thanks to your help I learned a lot about API and made it work!

ImagineZA
Posts: 19
Joined: Thu Oct 11, 2018 10:15 am

Re: [SOLVED] how to get temperature values from internet?

Wed Apr 24, 2019 5:35 am

Hey @DougieLawson,
I was hoping you could expand on the below:
The same bit of python using forecastio

Code: Select all

#!/usr/bin/python3

from urllib.request import urlopen
import json

apikey="api_key_redacted" # get a key from https://developer.forecast.io/register
# Latitude & longitude - current values are central Basingstoke.
lati="51.2673665"
longi="-1.0817624"

# Add units=si to get it in sensible ISO units not stupid Fahreneheit.
url="https://api.forecast.io/forecast/"+apikey+"/"+lati+","+longi+"?units=si"

meteo=urlopen(url).read()
meteo = meteo.decode('utf-8')
weather = json.loads(meteo)

print (weather['currently']['temperature'])
Basically I am able to extract the data using the code you have put here for the current data set, but when it comes to extracting data from the daily set I seem to be hitting a wall, how would you go about doing it?

My code below:

Code: Select all

#!/usr/bin/python3

from urllib.request import urlopen
import json

apikey="key redacted"
# Latitude & longitude
lati="-26.20227"
longi="28.04363"

# Add units=si to get it in sensible ISO units not stupid Fahreneheit.
url="https://api.forecast.io/forecast/"+apikey+"/"+lati+","+longi+"?units=si"

meteo=urlopen(url).read()
meteo = meteo.decode('utf-8')
weather = json.loads(meteo)

cTemp = (weather['currently']['temperature'])
cSum = (weather['currently']['summary'])
cRain1 =  (weather['currently']['precipProbability'])
cRain2 = cRain1*100
daily = (weather['daily']['summary'])

print (cTemp)
print (cSum)
print (cRain2)
print (daily)

#Everything above this point works as expected

dailyTHigh = (weather['daily']['data']['temperatureHigh'])

print (dailyTHigh)

Cheers,

Return to “Automation, sensing and robotics”