<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[M5Stack LAN BASE trying to be a web server with Micropython]]></title><description><![CDATA[<p dir="auto">I hope someone has achieved this and can point me in the right direction.</p>
<p dir="auto">I have an M5 with the W5500 LAN base and am trying to have it run just a simple web form to turn a couple of ports on or off.  The code below works fine if I use WiFi, but just hangs at the 'conn, addr = s.accept()' command when I used the wired LAN connection.  I am getting an IP address and I can ping it, but when I try and browse to it I get a 'connection refused' error.</p>
<p dir="auto">Help!</p>
<p dir="auto">Here is the code:</p>
<p dir="auto">from m5stack import *<br />
from m5ui import *<br />
from uiflow import *<br />
import module<br />
import network<br />
import socket</p>
<p dir="auto">lcd.clear()</p>
<p dir="auto">lan = module.get(module.LANBASE)<br />
eth0Ip = lan.get_if_config()[0]<br />
label1 = M5TextBox(10, 10, "Eth0 ip :" + str(eth0Ip), lcd.FONT_Ubuntu, 0xfff500, rotate=0)<br />
#lan.tcp_udp_config(eth0Ip, 80, 1, 1)<br />
#label2 = M5TextBox(10, 25, "UDP configured", lcd.FONT_Ubuntu, 0xfff500, rotate=0)</p>
<h1>Setup GPIO</h1>
<p dir="auto">pin1 = machine.Pin(5, machine.Pin.OUT)<br />
pin2 = machine.Pin(2, machine.Pin.OUT)</p>
<p dir="auto">def generate_html():<br />
# Generate HTML based on GPIO states<br />
state1 = "Power is open - Click to close" if pin1.value() else "Power is closed - click to open"<br />
state2 = "Reset is open - Click to close" if pin2.value() else "Reset is closed - click to open"</p>
<pre><code>html = """&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt; &lt;title&gt;Remote Reset and Power Control&lt;/title&gt; &lt;/head&gt;
&lt;body&gt; &lt;h1&gt;Remote Reset and Power Control&lt;/h1&gt;
  &lt;button onclick="toggleGPIO(1)"&gt;{state1}&lt;/button&gt;&lt;br&gt;
  &lt;button onclick="toggleGPIO(2)"&gt;{state2}&lt;/button&gt;&lt;br&gt;
  &lt;script&gt;
  function toggleGPIO(gpio) {{
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {{
      if (this.readyState == 4 &amp;&amp; this.status == 200) {{
        window.location.reload(); // Reload the page
      }}
    }};
    xhr.open('GET', '/toggle?gpio=' + gpio, true);
    xhr.send();
  }}
  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
"""
return html
</code></pre>
<h1>HTTP response</h1>
<p dir="auto">def http_response(status, content_type, body):<br />
return 'HTTP/1.1 {} OK\r\nContent-Type: {}\r\n\r\n{}'.format(status, content_type, body)</p>
<h1>Handle HTTP requests</h1>
<p dir="auto">def handle_http_request(conn):<br />
request = conn.recv(1024)<br />
request = str(request)<br />
#print('Content = %s' % request)<br />
gpio = request.find('/toggle?gpio=')<br />
if gpio == 6:<br />
gpio_number = int(request[19])<br />
if gpio_number == 1:<br />
pin1.value(not pin1.value())<br />
elif gpio_number == 2:<br />
pin2.value(not pin2.value())</p>
<pre><code>response = generate_html()
conn.sendall(http_response(200, "text/html", response))
conn.close()
</code></pre>
<h1>Main loop</h1>
<p dir="auto">addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]</p>
<p dir="auto">#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # This was something that ChatGPT suggested - made no difference<br />
s = socket.socket()<br />
s.bind(addr)<br />
s.listen(1)</p>
<p dir="auto">label2 = M5TextBox(10, 40, "Listening on :" + str(addr), lcd.FONT_Ubuntu, 0xfff500, rotate=0)</p>
<p dir="auto">while True:<br />
try:<br />
label2 = M5TextBox(10,90, "Working...", lcd.FONT_Ubuntu, 0xfff500, rotate=0)<br />
conn, addr = s.accept()<br />
label2 = M5TextBox(10, 70, "Connection from :" + str(addr), lcd.FONT_Ubuntu, 0xfff500, rotate=0)<br />
handle_http_request(conn)<br />
except OSError as e:<br />
conn.close()<br />
label2 = M5TextBox(10, 70, "Connection closed :" + str(addr), lcd.FONT_Ubuntu, 0xfff500, rotate=0)</p>
<h1>END</h1>
]]></description><link>https://community.m5stack.com/topic/5939/m5stack-lan-base-trying-to-be-a-web-server-with-micropython</link><generator>RSS for Node</generator><lastBuildDate>Sun, 15 Mar 2026 23:14:24 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/5939.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 04 Jan 2024 01:38:43 GMT</pubDate><ttl>60</ttl></channel></rss>