Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Webserver - Access point

    Scheduled Pinned Locked Moved Ideas
    5 Posts 3 Posters 78 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • E Offline
      EmbeddedGuy
      last edited by

      Hi All,
      Where can I find a good functional example of a web server that acts as an access point?
      If possible I would like to store the web pages in the file system.
      It should be easy to add routes.
      It will be awesome if provision is made for dynamic data
      Any guidance will be appreciated.

      1 Reply Last reply Reply Quote 0
      • G Offline
        Gijs
        last edited by

        Hi,
        I made this example a while ago. Some adapting is required but I believe you should be able to make it work!
        Best,

        import usocket
        import _thread
        import time
        from network import WLAN
        import pycom
        
        availablecolor = 0x001100
        connectioncolor = 0x110000
        
        # Thread for handling a client
        def client_thread(clientsocket,n):
            # Receive maxium of 12 bytes from the client
            r = clientsocket.recv(4096)
        
            # If recv() returns with 0 the other end closed the connection
            if len(r) == 0:
                clientsocket.close()
                return
            else:
                # Do something wth the received data...
                print("Received: {}".format(str(r))) #uncomment this line to view the HTTP request
        
            http = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection:close \r\n\r\n" #HTTP response
            
            if "GET / " in str(r):
                #this is a get response for the page   
                # Sends back some data
                clientsocket.send(http + "<html><body><h1> You are connection "+ str(n) + "</h1><br> Your browser will send multiple requests <br> <a href='/hello'> hello!</a><br><a href='/color'>change led color!</a></body></html>")
            elif "GET /hello "in str(r):
                
                clientsocket.send(http + "<html><body><h1> Hello to you too! </h1><br> <a href='/'> go back </a></body></html>")
            elif "GET /color" in str(r):
                pycom.rgbled(0xFFFFFF)
                clientsocket.send(http + "<html><body><h1> You are connection "+ str(n) + "</h1><br> Your browser will send multiple requests <br> <a href='/hello'> hello!</a><br><a href='/color'>change led color!</a></body></html>")
        
            # Close the socket and terminate the thread
        
            clientsocket.close()
            pycom.rgbled(connectioncolor)
            time.sleep_ms(500)
            pycom.rgbled(availablecolor)  
        
        time.sleep(1)
        wifi = WLAN()
        wifi.init(mode=WLAN.AP, ssid="hello", auth=None, channel=1)
        print("WiFi is up!")
        time.sleep(1)
        pycom.heartbeat(False)
        pycom.rgbled(availablecolor)
        
        # Set up server socket
        serversocket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
        serversocket.setsockopt(usocket.SOL_SOCKET, usocket.SO_REUSEADDR, 1)
        serversocket.bind(("192.168.4.1", 80))
        
        # Accept maximum of 5 connections at the same time
        serversocket.listen(5)
        
        # Unique data to send back
        c = 1
        while True:
            # Accept the connection of the clients
            (clientsocket, address) = serversocket.accept()
            # Start a new thread to handle the client
            _thread.start_new_thread(client_thread, (clientsocket, c))
            c = c+1
        serversocket.close()
        
        E 1 Reply Last reply Reply Quote 1
        • N Offline
          nftylitak
          last edited by

          We have been using MicroWebSrv2 that allows you to serve any files needed. Also supports a custom file type: pyhtml that you can embed python code, it will be first processed by the web engine, then produce the actual html file finally it will serve it.

          E 1 Reply Last reply Reply Quote 1
          • E Offline
            EmbeddedGuy @Gijs
            last edited by

            @Gijs Thnx for the feedback. I will definitely look into it. It looks simple enough to do an interface for setting network credentials etc. Much appreciated.

            1 Reply Last reply Reply Quote 0
            • E Offline
              EmbeddedGuy @nftylitak
              last edited by

              @nftylitak Thnx for the feedback. I did see this. I think it is approx 150K in size, a bit big but comes with a lot of functionality.
              Not sure how to get started with it. THNX!!

              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • 1 / 1
              • First post
                Last post

              Pycom on Twitter

              Pycom