37. Lektion: RequestAus Attraktor WikiVersion vom 4. Januar 2024, 18:04 Uhr von Kapest (Diskussion | Beiträge) InhaltsverzeichnisWebserver ansprechenZur 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
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' 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)
Daten auswertenDie Daten werden im Json-Format gesendet.
Links:
Zurück zur "Micropython Kurs 2023 Teil 2" Startseite |