micpy init
This commit is contained in:
45
aaaSonstiges/micropython/boot.py
Normal file
45
aaaSonstiges/micropython/boot.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import network
|
||||
import time
|
||||
import gc
|
||||
|
||||
gc.collect()
|
||||
|
||||
# (SSID: Passwort)
|
||||
known_networks = {
|
||||
"Vodafone-9454": "AchXHta93YCTgC3M",
|
||||
"no": "",
|
||||
"placeholder1": "placeholder1",
|
||||
"placeholder2": "placeholder2"
|
||||
}
|
||||
|
||||
def connect_wifi():
|
||||
wlan = network.WLAN(network.STA_IF)
|
||||
wlan.active(True)
|
||||
|
||||
# Suche nach verfügbaren Netzwerken
|
||||
print("Scanne nach WLAN-Netzwerken...")
|
||||
available_networks = wlan.scan()
|
||||
print(f"Gefundene Netzwerke: {(available_networks)}")
|
||||
for net in available_networks:
|
||||
ssid = net[0].decode('utf-8') if isinstance(net[0], bytes) else net[0]
|
||||
if ssid in known_networks:
|
||||
password = known_networks[ssid]
|
||||
print(f"Versuche Verbindung mit '{ssid}'...")
|
||||
wlan.connect(ssid, password)
|
||||
|
||||
while not wlan.isconnected():
|
||||
time.sleep(1)
|
||||
print(".", end="")
|
||||
|
||||
if wlan.isconnected():
|
||||
print(f"\nVerbunden mit '{ssid}'")
|
||||
print("IP-Adresse:", wlan.ifconfig()[0])
|
||||
return True
|
||||
else:
|
||||
print(f"\n❌ Verbindung mit '{ssid}' fehlgeschlagen.")
|
||||
|
||||
print("Kein bekanntes Netzwerk verfügbar oder Verbindung fehlgeschlagen.")
|
||||
return False
|
||||
|
||||
# Verbindung herstellen beim Boot
|
||||
connect_wifi()
|
||||
44
aaaSonstiges/micropython/main.py
Normal file
44
aaaSonstiges/micropython/main.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import urequests #this is not an issue, because micropython has its own urequests module already
|
||||
import json
|
||||
import socket
|
||||
|
||||
# one time dns resolve, damit der arme ESP und nicht wegkocht.
|
||||
def resolve_ip(hostname):
|
||||
try:
|
||||
print(f"🌐 Resolvieren von '{hostname}' ...")
|
||||
addr_info = socket.getaddrinfo(hostname, 80)
|
||||
ip = addr_info[0][4][0]
|
||||
print(f"🔎 IP-Adresse gefunden: {ip}")
|
||||
return ip
|
||||
except Exception as e:
|
||||
print("❌ Fehler beim Resolvieren:", e)
|
||||
return None
|
||||
|
||||
# beispiel-fetch von der hauptadresse (muss evtl in ne roop und nen thread)
|
||||
def fetch_json(ip, host, path):
|
||||
url = f"http://{ip}{path}"
|
||||
try:
|
||||
print(f"Sende Anfrage an {url} mit Host '{host}' ...")
|
||||
headers = {"Host": host}
|
||||
response = urequests.get(url, headers=headers)
|
||||
|
||||
if response.status_code == 200:
|
||||
print("✅ Antwort erhalten:")
|
||||
print(response.text)
|
||||
data = json.loads(response.text)
|
||||
print("📦 JSON geladen:", data)
|
||||
else:
|
||||
print(f"❌ Fehler: Status-Code {response.status_code}")
|
||||
|
||||
response.close()
|
||||
|
||||
except Exception as e:
|
||||
print("⚠️ Fehler bei der Anfrage (äußerer Block):", e)
|
||||
|
||||
# --- Hauptteil ---
|
||||
host = "lires.de"
|
||||
path = "/unsecure/esp/"
|
||||
|
||||
ip = resolve_ip(host)
|
||||
if ip:
|
||||
fetch_json(ip, host, path)
|
||||
Reference in New Issue
Block a user