🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    LAN W5500 socket listening does not work

    Scheduled Pinned Locked Moved Modules
    5 Posts 3 Posters 6.3k Views
    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.
    • M5StickFreaklerM Offline
      M5StickFreakler
      last edited by M5StickFreakler

      Hi friends

      I have written a simply micropython test program as a web server.
      It works when I am using the configured ip address of wifi connection (ip via dhcp).
      Then I have tried the LAN base module W5500 V12.
      Unfortunately it does not work with the configured ip address of the ethernet module (ip via dhcp) .

      I have used....
      lan = module.get(module.LANBASE)
      ...instead of...
      lan = network.LAN(mdc=23, mdio=18, power=21, phy_type=network.PHY_W5500, phy_addr=0)
      lan.active(True)
      ...because I get an error message that network does not support LAN methode.

      Here is my micropython example code:

      from m5stack import *
      from m5ui import *
      from uiflow import *
      import wifiCfg
      import network
      import socket
      import module

      #WiFi
      wifiCfg.screenShow()
      wifiCfg.autoConnect(lcdShow=True)
      wifiIp = wifiCfg.wlan_sta.ifconfig()[0]
      title0 = M5Title(title="SERVER-EXAMPLE", x=95, fgcolor=0xFFFFFF, bgcolor=0xff001d)
      label0 = M5TextBox(10, 40, "WiFi ip :" + str(wifiIp), lcd.FONT_Ubuntu, 0xfff500, rotate=0)

      #Ethernet
      lan = module.get(module.LANBASE)
      #lan = network.LAN(mdc=23, mdio=18, power=21, phy_type=network.PHY_W5500, phy_addr=0)
      #lan.active(True)
      eth0Ip = lan.get_if_config()[0]
      label1 = M5TextBox(10, 60, "Eth0 ip :" + str(eth0Ip), lcd.FONT_Ubuntu, 0xfff500, rotate=0)

      #Choose ip address
      ip = wifiIp # works
      #ip = eth0Ip # does not work

      #Socket binding
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.bind((ip, 80))

      label2 = M5TextBox(10, 80, "SERVER running at " + ip, lcd.FONT_Ubuntu, 0xfff500, rotate=0)

      #Listening
      sock.listen(1)
      while True:
      conn, addr = sock.accept()
      print('Connection from:', addr)
      conn.sendall('Hello, world!')
      conn.close()

      1 Reply Last reply Reply Quote 0
      • felmueF Offline
        felmue
        last edited by

        Hello @M5StickFreakler

        have you tried the example? It works for me.

        Note: the Init LAN block blocks until a client connects.

        Thanks
        Felix

        GPIO translation table M5Stack / M5Core2
        Information about various M5Stack products.
        Code examples

        1 Reply Last reply Reply Quote 0
        • M5StickFreaklerM Offline
          M5StickFreakler
          last edited by

          Hi felmue
          Thanks for your appreciated reply.
          Unfortunately it is unusable for me in that way. I would like to bind the ip address to a socket on port 80 (http server). And I dont want that the application is blocked until a client connects.
          best regards

          1 Reply Last reply Reply Quote 0
          • M5StickFreaklerM Offline
            M5StickFreakler
            last edited by

            tried with modified code, but no luck:

            from m5stack import *
            from m5ui import *
            from uiflow import *
            import wifiCfg
            import network
            import socket
            import module

            #WiFi
            wifiCfg.screenShow()
            wifiCfg.autoConnect(lcdShow=True)
            wifiIp = wifiCfg.wlan_sta.ifconfig()[0]
            title0 = M5Title(title="SERVER-EXAMPLE", x=95, fgcolor=0xFFFFFF, bgcolor=0xff001d)
            label0 = M5TextBox(10, 40, "WiFi ip :" + str(wifiIp), lcd.FONT_Ubuntu, 0xfff500, rotate=0)

            counter = 0

            #Ethernet
            lan = module.get(module.LANBASE)
            eth0Ip = lan.get_if_config()[0]
            label1 = M5TextBox(10, 60, "Eth0 ip :" + str(eth0Ip), lcd.FONT_Ubuntu, 0xfff500, rotate=0)
            lan.tcp_udp_config(eth0Ip, 80, 1, 1) ## es muss ein call gemacht werden, damit das Programm weiter läuft

            #Choose ip address
            #ip = wifiIp # works
            ip = eth0Ip # does not work

            label2 = M5TextBox(10, 80, "SERVER is running", lcd.FONT_Ubuntu, 0xfff500, rotate=0)

            label3 = M5TextBox(10, 100, "waiting...", lcd.FONT_Ubuntu, 0xfff500, rotate=0)
            label4 = M5TextBox(10, 120, "received?", lcd.FONT_Ubuntu, 0xfff500, rotate=0)

            if ip == wifiIp:

            server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            server.settimeout(30)
            server.bind((wifiIp, 80))
            
            while True:
                server.listen(1)
                
                (conn, addr) = server.accept()
                request = conn.recv(4096)
            
                if request:
                    counter = counter + 1
                    request = str(request)
                    conn, addr = server.accept()
                    label3.setText(str(counter))
                    label4.setText(str(request))
                    response = 'Hello, world!'
                    conn.sendall(response)
                    conn.close()
            
                wait_ms(2)
            

            if ip == eth0Ip:

            while True:
                if lan.is_available_packet(1):
                    counter = counter + 1
                    request = str(lan.tcp_receive_packet(0))
                    label3.setText(str(counter))
                    label4.setText(str(request))
                    response = 'Hello, world!'
                    lan.tcp_send_packet(response)
            
                wait_ms(2)
            
            T 1 Reply Last reply Reply Quote 0
            • T Offline
              Treadbrook @M5StickFreakler
              last edited by

              @m5stickfreakler hi - did you ever solve this? I am having the same problem with both the LAN Module and the LAN Base - it initialises the LAN, gets an IP address, but just won't accept data from a browser when I try the following code:
              s = socket.socket()
              s.bind(('172.17.15.87',80))
              s.listen(1)

              I would love to get this sorted!

              Cheers,

              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
              • First post
                Last post