LAN W5500 socket listening does not work
-
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() -
Hello @M5StickFreakler
have you tried the example? It works for me.
Note: the
Init LAN
block blocks until a client connects.Thanks
Felix -
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 -
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 worklabel2 = 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)
-
@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,