37. Lektion: Request: Unterschied zwischen den Versionen

Aus Attraktor Wiki

Wechseln zu: Navigation, Suche
(Wetter Web Seite)
(Wetter Web Seite)
Zeile 61: Zeile 61:
 
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?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?lat={lat}&lon={lon}&appid={API key}'
 +
 +
</pre>
 +
 +
<pre>
 +
# request_001.py
 +
#
 +
 +
import wlantools as wt
 +
import urequests as ur
 +
 +
# api_key = '3e55a61880698b8a4f912216b7ec6755'
 +
api_key = '3ecdd8a467d072e969d159a83a47339f'
 +
# api_key = 'c710309ef5aadccdc82c51631f6edaeb'
 +
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)
  
 
</pre>
 
</pre>

Version vom 4. Januar 2024, 17:53 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 Weathrt Data
Subscripe
API-Key holen
Ganz oben auf API klicken, dann die API Doc anklicken
Dort den Abschnitt Built-in API request by city name suchen

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}'

# request_001.py
#

import wlantools as wt
import urequests as ur

# api_key = '3e55a61880698b8a4f912216b7ec6755'
api_key = '3ecdd8a467d072e969d159a83a47339f'
# api_key = 'c710309ef5aadccdc82c51631f6edaeb'
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)


https://www.youtube.com/watch?v=fOPWZytYsFM https://www.youtube.com/watch?v=X1Y3HQy5Xfo



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