main.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import os
  2. import machine
  3. import blueLE-test
  4. uart = machine.UART(0, 115200)
  5. os.dupterm(uart)
  6. known_nets = {
  7. 'UpUpUp': {'pwd': 't4k4tuk4l4nd2', 'wlan_config': ('192.168.2.115', '255.255.255.0', '192.168.2.1', '192.168.2.1')},
  8. 'Vidit-Fabrik': {'pwd': '%m1n!5NK=$', 'wlan_config': ('192.168.0.115', '255.255.255.0', '192.168.0.1', '192.168.0.1')}, # (ip, subnet_mask, gateway, DNS_server)
  9. }
  10. if machine.reset_cause() != machine.SOFT_RESET:
  11. from network import WLAN
  12. wl = WLAN()
  13. wl.mode(WLAN.STA)
  14. original_ssid = wl.ssid()
  15. original_auth = wl.auth()
  16. print("Scanning for known wifi nets")
  17. available_nets = wl.scan()
  18. nets = frozenset([e.ssid for e in available_nets])
  19. known_nets_names = frozenset([key for key in known_nets])
  20. net_to_use = list(nets & known_nets_names)
  21. try:
  22. net_to_use = net_to_use[0]
  23. net_properties = known_nets[net_to_use]
  24. pwd = net_properties['pwd']
  25. sec = [e.sec for e in available_nets if e.ssid == net_to_use][0]
  26. if 'wlan_config' in net_properties:
  27. wl.ifconfig(config=net_properties['wlan_config'])
  28. wl.connect(net_to_use, (sec, pwd), timeout=10000)
  29. while not wl.isconnected():
  30. machine.idle() # save power while waiting
  31. print("Connected to "+net_to_use+" with IP address:" + wl.ifconfig()[0])
  32. except Exception as e:
  33. print("Failed to connect to any known network, going into AP mode")
  34. wl.init(mode=WLAN.AP, ssid=original_ssid, auth=original_auth, channel=6, antenna=WLAN.INT_ANT)