|
|
(17 dazwischenliegende Versionen von einem anderen Benutzer werden nicht angezeigt) |
Zeile 38: |
Zeile 38: |
| API anklicken | | API anklicken |
| <br> | | <br> |
− | Current Weathrt Data | + | Current Weather Data |
| <br> | | <br> |
− | Subscripe
| + | Subscribe |
| <br> | | <br> |
| API-Key holen | | API-Key holen |
Zeile 48: |
Zeile 48: |
| Dort den Abschnitt '''Built-in API request by city name''' suchen | | Dort den Abschnitt '''Built-in API request by city name''' suchen |
| <br> | | <br> |
| + | https://openweathermap.org/current |
| + | <pre> |
| + | api_key = '3e55a61880698b8a4f912216b7ec6755' |
| + | city = 'Hamburg' |
| + | country = 'de' |
| + | lat = None |
| + | lon = None |
| + | units = 'metric' |
| + | lang = 'de' |
| | | |
| + | url = f'https://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}' |
| + | url = f'https://api.openweathermap.org/data/2.5/weather?q={city name},{country code}&appid={API key}' |
| + | url = f'https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API key}' |
| + | |
| + | url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&lang={lang}&units={units}' |
| + | |
| + | </pre> |
| + | |
| + | Die empfangenen Daten im Json-Format: |
| + | Es handelt sich um ein komplexes Dictionary. |
| <pre> | | <pre> |
− | key = '3e55a61880698b8a4f912216b7ec6755'
| + | { |
| + | "coord": { |
| + | "lon": 10.99, |
| + | "lat": 44.34 |
| + | }, |
| + | "weather": [ |
| + | { |
| + | "id": 501, |
| + | "main": "Rain", |
| + | "description": "moderate rain", |
| + | "icon": "10d" |
| + | } |
| + | ], |
| + | "base": "stations", |
| + | "main": { |
| + | "temp": 298.48, |
| + | "feels_like": 298.74, |
| + | "temp_min": 297.56, |
| + | "temp_max": 300.05, |
| + | "pressure": 1015, |
| + | "humidity": 64, |
| + | "sea_level": 1015, |
| + | "grnd_level": 933 |
| + | }, |
| + | "visibility": 10000, |
| + | "wind": { |
| + | "speed": 0.62, |
| + | "deg": 349, |
| + | "gust": 1.18 |
| + | }, |
| + | "rain": { |
| + | "1h": 3.16 |
| + | }, |
| + | "clouds": { |
| + | "all": 100 |
| + | }, |
| + | "dt": 1661870592, |
| + | "sys": { |
| + | "type": 2, |
| + | "id": 2075663, |
| + | "country": "IT", |
| + | "sunrise": 1661834187, |
| + | "sunset": 1661882248 |
| + | }, |
| + | "timezone": 7200, |
| + | "id": 3163858, |
| + | "name": "Zocca", |
| + | "cod": 200 |
| + | } |
| + | </pre> |
| + | |
| + | |
| + | <pre> |
| + | # request_001.py |
| + | # |
| + | |
| + | import wlantools as wt |
| + | import urequests as ur |
| + | |
| + | api_key = '3e55a61880698b8a4f912216b7ec6755' |
| + | |
| + | city = 'Hamburg' |
| + | country = 'de' |
| + | lat = None |
| + | lon = None |
| + | units = 'metric' |
| + | lang = 'de' |
| + | |
| + | # url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}' |
| + | # url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&lang={lang}' |
| + | url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&lang={lang}&units={units}' |
| + | |
| + | data = ur.get(url).json() |
| + | |
| + | print(data) |
| + | print() |
| + | print(f'Das Wetter in {city}:') |
| + | print(f'{data["weather"][0]["description"]}') |
| + | print(f'Temperatur: {data["main"]["temp"]} °C') |
| + | print(f'Luftfeuchte: {data["main"]["humidity"]} %') |
| + | print(f'Luftdruck: {data["main"]["pressure"]} mm') |
| </pre> | | </pre> |
| <br> | | <br> |
− | https://www.youtube.com/watch?v=fOPWZytYsFM | + | * https://www.youtube.com/watch?v=fOPWZytYsFM |
| + | * https://www.youtube.com/watch?v=X1Y3HQy5Xfo |
| + | |
| <br> | | <br> |
| <br> | | <br> |
| + | |
| + | == Daten auswerten== |
| + | Die Daten werden im Json-Format gesendet. |
| + | |
| + | |
| + | |
| + | |
| | | |
| ===== Links:===== | | ===== Links:===== |
Aktuelle Version vom 6. Februar 2024, 18:43 Uhr
Webserver ansprechen
Zur einfacheren Verbindung mit dem Wlan kann man sich ein Modul wie wlantools.py schreiben. Diese Datei muss sich im Verzeichnis "/" oder "/lib" befinden.
# wlantools.py
#
# Modul mit Funktionen zum Wlan.
#
import network
# Wlan Verbindung herstellen
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(SSID, PW)
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
Anwendung:
>>> import wlantools as wl
network config: ('192.168.5.120', '255.255.255.0', '192.168.5.1', '192.168.5.1')
>>> wl.wlan.ifconfig()[0]
'192.168.5.120'
Beim Importieren wird die Datei wlantools.py ausgeführt. Dadurch wird die Verbindung zum Wlan hergestellt. Im Hauptprogramm steht es dann unter wl.wlan() zur Verfügung.
Wetter Web Seite
https://openweathermap.org
API anklicken
Current Weather Data
Subscribe
API-Key holen
Ganz oben auf API klicken, dann die API Doc anklicken
Dort den Abschnitt Built-in API request by city name suchen
https://openweathermap.org/current
api_key = '3e55a61880698b8a4f912216b7ec6755'
city = 'Hamburg'
country = 'de'
lat = None
lon = None
units = 'metric'
lang = 'de'
url = f'https://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}'
url = f'https://api.openweathermap.org/data/2.5/weather?q={city name},{country code}&appid={API key}'
url = f'https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API key}'
url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&lang={lang}&units={units}'
Die empfangenen Daten im Json-Format:
Es handelt sich um ein komplexes Dictionary.
{
"coord": {
"lon": 10.99,
"lat": 44.34
},
"weather": [
{
"id": 501,
"main": "Rain",
"description": "moderate rain",
"icon": "10d"
}
],
"base": "stations",
"main": {
"temp": 298.48,
"feels_like": 298.74,
"temp_min": 297.56,
"temp_max": 300.05,
"pressure": 1015,
"humidity": 64,
"sea_level": 1015,
"grnd_level": 933
},
"visibility": 10000,
"wind": {
"speed": 0.62,
"deg": 349,
"gust": 1.18
},
"rain": {
"1h": 3.16
},
"clouds": {
"all": 100
},
"dt": 1661870592,
"sys": {
"type": 2,
"id": 2075663,
"country": "IT",
"sunrise": 1661834187,
"sunset": 1661882248
},
"timezone": 7200,
"id": 3163858,
"name": "Zocca",
"cod": 200
}
# request_001.py
#
import wlantools as wt
import urequests as ur
api_key = '3e55a61880698b8a4f912216b7ec6755'
city = 'Hamburg'
country = 'de'
lat = None
lon = None
units = 'metric'
lang = 'de'
# url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}'
# url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&lang={lang}'
url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&lang={lang}&units={units}'
data = ur.get(url).json()
print(data)
print()
print(f'Das Wetter in {city}:')
print(f'{data["weather"][0]["description"]}')
print(f'Temperatur: {data["main"]["temp"]} °C')
print(f'Luftfeuchte: {data["main"]["humidity"]} %')
print(f'Luftdruck: {data["main"]["pressure"]} mm')
Daten auswerten
Die Daten werden im Json-Format gesendet.
Links:
Navigation
Zurück zur "Micropython Kurs 2023 Teil 2" Startseite
Zurück zur "Micropython Kurs 2023" Startseite
Zurück zur Programmieren Startseite
Zurück zur Wiki Startseite