37. Lektion: Request: Difference between revisions

From Attraktor Wiki
Jump to navigation Jump to search
Created page with "===== Links:===== * Wetter-API ** https://www.youtube.com/watch?v=IpJyyY5Zp6Q"
 
Antowima (talk | contribs)
 
(31 intermediate revisions by one other user not shown)
Line 1: Line 1:
== 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.
<pre>
# 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())
</pre>
Anwendung:
<pre>
>>> 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'
</pre>
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==
<br>
https://openweathermap.org
<br>
API anklicken
<br>
Current Weather Data
<br>
Subscribe
<br>
API-Key holen
<br>
Ganz oben auf API klicken, dann die API Doc anklicken
<br>
Dort den Abschnitt '''Built-in API request by city name''' suchen
<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>
{
  "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>
<br>
* https://www.youtube.com/watch?v=fOPWZytYsFM
* https://www.youtube.com/watch?v=X1Y3HQy5Xfo
<br>
<br>
== Daten auswerten==
Die Daten werden im Json-Format gesendet.
===== Links:=====
===== Links:=====
* Wetter-API
* Wetter-API
** https://www.youtube.com/watch?v=IpJyyY5Zp6Q
** https://www.youtube.com/watch?v=IpJyyY5Zp6Q
==Navigation==
[[Micropython_Kurs_2023_-_Teil_2|Zurück zur "Micropython Kurs 2023 Teil 2" Startseite]]<br>
[[Micropython Kurs 2023|Zurück zur "Micropython Kurs 2023" Startseite]]<br>
[[Programmieren|Zurück zur Programmieren Startseite]]<br>
[[Attraktor_Wiki|Zurück zur Wiki Startseite]]<br>

Latest revision as of 19:43, 6 February 2024

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