WiPy 2.0 Captive Portal Network Config



  • Hello,
    I was trying to implement a captive portal for the initial configuration of the device. I've been found some implementations on ESP8266 and ESP32 boards on the internet and decided to go with the below:
    https://github.com/Matt4/micropython-captive-portal-network-setup
    which uses DNS redirection method.
    I've faced some issues regarding network library in Pycom ( using firmware 1.15.1 ).

    • Ap mode is successfully initializes below code, but I've seen the IP of the board remains 0.0.0.0
    • When I connect my android phone to the board, WiFi details in android as -> IP: 1.2.3.5 / Gateway: 1.2.3.4 / DNS: 1.2.3.4 Subnet: 255.255.255.0 , so it is correct I believe.
    • On the android chrome when I enter the mock website address (asd.net), the redirection isn't happening.
      How it's possible to make automatic redirection to the captive portal, when a client connects to the board, am I missing something ?
    import network
    import usocket as socket
    import ure
    
      ap = network.WLAN(mode=network.WLAN.AP, ssid="wipy" )
      ap.ifconfig(id=1, config=('1.2.3.4', '255.255.255.0', '1.2.3.4', '0.0.0.0'))
    
      ip = ap.ifconfig()[0]
      print("DNS Server: dom.query. 60 in A {:s}".format(ip))                 # Prints- DNS Server: dom.query. 60 in A 0.0.0.0
    
      udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
      udps.setblocking(False)
      udps.bind(('',53))
    
      s = socket.socket()
      addr = (ip, 80)
    
      s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      s.bind(addr)
      s.listen(1)
      s.settimeout(2)
      print("Web Server: Listening http://{}:80/".format(ip))                 # Prints- Web Server: Listening http://0.0.0.0:80/
      
      regex = ure.compile("network\?ssid=(.*?)&password=(.*?)\sHTTP")
    
      set_connection = False
      while set_connection is False:
        try:
          data, addr = udps.recvfrom(4096)
          print("Incoming data, from : ", addr)
          DNS = DNSQuery(data)
          udps.sendto(DNS.response(ip), addr)
          print("Replying: {:s} -> {:s}".format(DNS.domain, ip))               # Prints- Replying: beacons2.gvt2.com. -> 0.0.0.0
        except:
          print("No DNS")
    
        # Web server accepting connections
        try:
          res = s.accept()
          client_sock = res[0]
          client_addr = res[1]
    
          req = client_sock.recv(4096)
          print("Request:")
          print(req)
          client_sock.send(CONTENT)
          client_sock.close()
          print()
          search_result = regex.search(req)
          if search_result:
            incoming_network_name = search_result.group(1)
            incoming_network_pass = search_result.group(2)
            con = connection(incoming_network_name, incoming_network_pass)
            if con is True:
              d = {"network_name": incoming_network_name, "network_password": incoming_network_pass}
              f = open("config.json", "w")
              f.write(ujson.dumps(d))
              f.close()
        set_connection = True
    
        except:
          print("Timeout")
        time.sleep_ms(1000)
      udps.close()
    

    Thanks in advance
    -Baris



  • Hello,
    I released a fully new version (v2.0) of my web server here : https://github.com/jczic/MicroWebSrv2.
    Open source MIT, fully asynchronous, more robust, more fast and more efficient on Pycom modules.
    It is delivered with a good documentation.
    For captive portal, you can use a simple DNS server here (see readme) : https://github.com/jczic/MicroDNSSrv

    Thank you for your support and feedback. ☺️



  • @soulkiller , did you get this working? Can you share an example? Thanks.



  • Hello @paul-thornton , thank you for the reply. I've upgraded the firmware to 1.18.2, but still I get the same outputs.



  • @soulkiller said in WiPy 2.0 Captive Portal Network Config:

    1.15.1

    Hello @soulkiller,

    That firmware version is rather old now. a ton has been fixed in the networking area in later builds. Try giving this a go under 1.18. (latest stable). Hopefully you'll see better results.



Pycom on Twitter