<?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[Tiny WebServer crashes after some hours]]></title><description><![CDATA[<p dir="auto">Hi folks</p>
<p dir="auto">I have the problem, that the following code (it's a tiny web server) crashes after some hours.<br />
I don't know what the problem exactly is, but it is not reachable after some hours of perfect running.<br />
The label "labelWebServer" does show "RUNS", but I get a "ERR_CONNECTION_TIMED_OUT" because the server does not response anymore until I restart the device.</p>
<p dir="auto">You can call the tiny web server like:<br />
<code>http://{IpAddress}/api/v1.0/level</code></p>
<p dir="auto">Does anyone have a idea, what I am doing wrong?</p>
<p dir="auto">Annotation: It is one of 2 threads. My second thread reads out some sensors and calculates the "level". The second thread is working like a charm.</p>
<h3>Code Example: Tiny Web Server</h3>
<pre><code>from m5stack import *`
from m5ui import *
from uiflow import *
import random
import unit
import wifiCfg
import socket
import _thread as th

setScreenColor(0x000000)

labelWebServer = M5TextBox(240, 20, "WS", lcd.FONT_Default,0xFFFFFF, rotate=0)
labelIpAddress = M5TextBox(300, 170, "xxx.xxx.xxx.xxx", lcd.FONT_Default,0xFFFFFF, rotate=270)
levelPercent = 100

wifiCfg.screenShow()
wifiCfg.autoConnect(lcdShow = True)

def webServer():

  global levelPercent

  ip = wifiCfg.wlan_sta.ifconfig()
  server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  server.bind((ip[0], 80))
  server.listen(1)

  web_response_json = """{"data":{"deviceName":"BlablaLevel","deviceId": "BlablablaWhatever","value": @value@,"unit": "@unit@"}}"""

  web_error_404 = """&lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;&lt;html&gt;&lt;head&gt;&lt;title&gt;404 Not Found&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Not Found&lt;/h1&gt;&lt;p&gt;The requested URL was not found on this server.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;"""

  response = None

  while True:

    try:

      labelIpAddress.setText('')
      labelIpAddress.setText('IP ' + wifiCfg.wlan_sta.ifconfig()[0])

      if not wifiCfg.wlan_sta.isconnected():
        labelWebServer.setText('WS: RECON')
        wifiCfg.reconnect()

      labelWebServer.setText('WS: RUNS')
      
      conn, addr = server.accept()
      request = conn.recv(1024)
      request = str(request)

      isRequestLevel = request.find('/api/v1.0/level ') &gt;= 0
  
      if isRequestLevel :
        labelWebServer.setText('WS: 200')
        json = web_response_json
        json = json.replace('@value@', str(("%.2f"%(levelPercent))))
        json = json.replace('@unit@', '%')
        response = json
        conn.send('HTTP/1.1 200 OK\r\n')
        conn.send('Content-Type: application/json\r\n')
        conn.send('Connection: close\r\n\r\n')
      else :
        labelWebServer.setText('WS: 404')
        response = web_error_404
        conn.send('HTTP/1.1 404 NOT FOUND\r\n')
        conn.send('Content-Type: text/html\r\n')
        conn.send('Connection: close\r\n\r\n')

      conn.sendall(response)
    
    except Exception as e:
      labelWebServer.setText('WS: ERR')

    finally:
      try:
        conn.close()
      except Exception as e:
        labelWebServer.setText('WS: ERR')


th.start_new_thread(webServer, ())
</code></pre>
]]></description><link>https://community.m5stack.com/topic/1722/tiny-webserver-crashes-after-some-hours</link><generator>RSS for Node</generator><lastBuildDate>Tue, 28 Apr 2026 19:51:24 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/1722.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 23 Feb 2020 07:37:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Tiny WebServer crashes after some hours on Wed, 26 Feb 2020 01:54:08 GMT]]></title><description><![CDATA[<p dir="auto">great job</p>
]]></description><link>https://community.m5stack.com/post/7573</link><guid isPermaLink="true">https://community.m5stack.com/post/7573</guid><dc:creator><![CDATA[lukasmaximus]]></dc:creator><pubDate>Wed, 26 Feb 2020 01:54:08 GMT</pubDate></item><item><title><![CDATA[Reply to Tiny WebServer crashes after some hours on Tue, 25 Feb 2020 17:52:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lukasmaximus" aria-label="Profile: lukasmaximus">@<bdi>lukasmaximus</bdi></a> said in <a href="/post/7539">Tiny WebServer crashes after some hours</a>:</p>
<blockquote>
<p dir="auto">I also had the same issue, or that it would crash after pressing a html button too many times. I never found the reason why</p>
</blockquote>
<p dir="auto">Hi Lukas</p>
<p dir="auto">Look at my solution. It runs since 2 days without a problem. If the socket crashes it will setup again after 30 seconds.<br />
Maybe you can adopt it</p>
<pre><code># ========================================
# WEB SERVER
# ========================================
def webServer():

    global levelPercent, setupMode

    web_response_json = """{"data":{"deviceName":"blablabla","deviceId": "blablabla","value": @value@,"unit": "@unit@"}}"""

    web_error_404 = """&lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;&lt;html&gt;&lt;head&gt;&lt;title&gt;404 Not Found&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Not Found&lt;/h1&gt;&lt;p&gt;The requested URL was not found on this server.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;"""

    while True:

        try:

            labelIpAddress.setText('')

            if not wifiCfg.wlan_sta.isconnected():
                labelWebServer.setText('WS: RECON')
                wifiCfg.reconnect()
            else:
                ip = wifiCfg.wlan_sta.ifconfig()
                labelIpAddress.setText('IP ' + wifiCfg.wlan_sta.ifconfig()[0])
                
                server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                server.settimeout(30)
                server.bind((ip[0], 80))
                server.listen(1)
            
                labelWebServer.setText('WS: RUNS')

                (conn, addr) = server.accept()
                request = conn.recv(1024)
                
                if request:
                  request = str(request)
  
                  isRequestLevel = request.find('/api/v1.0/level ') &gt;= 0
  
                  if isRequestLevel:
                      labelWebServer.setText('WS: 200')
                      json = web_response_json
                      json = json.replace('@value@', str('%.2f'
                              % levelPercent))
                      json = json.replace('@unit@', '%')
                      response = json
                      conn.send('HTTP/1.1 200 OK\r\n')
                      conn.send('Content-Type: application/json\r\n')
                      conn.send('Connection: close\r\n\r\n')
                  else:
                      labelWebServer.setText('WS: 404')
                      response = web_error_404
                      conn.send('HTTP/1.1 404 NOT FOUND\r\n')
                      conn.send('Content-Type: text/html\r\n')
                      conn.send('Connection: close\r\n\r\n')
  
                  conn.sendall(response)

        except Exception as e:
            gc.collect()
            labelWebServer.setText('WS: ERR')

        finally:
            try:
                conn.close()
                server.close()
            except Exception as e:
                pass
            finally:
                pass
</code></pre>
]]></description><link>https://community.m5stack.com/post/7568</link><guid isPermaLink="true">https://community.m5stack.com/post/7568</guid><dc:creator><![CDATA[M5StickFreakler]]></dc:creator><pubDate>Tue, 25 Feb 2020 17:52:01 GMT</pubDate></item><item><title><![CDATA[Reply to Tiny WebServer crashes after some hours on Sun, 23 Feb 2020 13:27:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lukasmaximus" aria-label="Profile: lukasmaximus">@<bdi>lukasmaximus</bdi></a> said in <a href="/post/7539">Tiny WebServer crashes after some hours</a>:</p>
<blockquote>
<p dir="auto">I also had the same issue, or that it would crash after pressing a html button too many times. I never found the reason why</p>
</blockquote>
<p dir="auto">In my opinion the problem is my code line <code>request = conn.recv(1024)</code></p>
<p dir="auto">If the connection fails, the program never will com back here to life. So my code fragment to reconnect will never run.<br />
It would help if <code>request = conn.recv(1024)</code> would crash in case of lost connection, so that I can apply code to setup everything new.</p>
]]></description><link>https://community.m5stack.com/post/7547</link><guid isPermaLink="true">https://community.m5stack.com/post/7547</guid><dc:creator><![CDATA[M5StickFreakler]]></dc:creator><pubDate>Sun, 23 Feb 2020 13:27:13 GMT</pubDate></item><item><title><![CDATA[Reply to Tiny WebServer crashes after some hours on Sun, 23 Feb 2020 12:01:58 GMT]]></title><description><![CDATA[<p dir="auto">I also had the same issue, or that it would crash after pressing a html button too many times. I never found the reason why</p>
]]></description><link>https://community.m5stack.com/post/7539</link><guid isPermaLink="true">https://community.m5stack.com/post/7539</guid><dc:creator><![CDATA[lukasmaximus]]></dc:creator><pubDate>Sun, 23 Feb 2020 12:01:58 GMT</pubDate></item><item><title><![CDATA[Reply to Tiny WebServer crashes after some hours on Sun, 23 Feb 2020 17:08:50 GMT]]></title><description><![CDATA[<p dir="auto">I am experimenting with this code. Maybe it helps....</p>
<p dir="auto"><code>server.settimeout(30)</code></p>
<pre><code># ========================================
# WEB SERVER
# ========================================
def webServer():

    global levelPercent, setupMode

    web_response_json = """{"data":{"deviceName":"blablabla","deviceId": "blablabla","value": @value@,"unit": "@unit@"}}"""

    web_error_404 = """&lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;&lt;html&gt;&lt;head&gt;&lt;title&gt;404 Not Found&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Not Found&lt;/h1&gt;&lt;p&gt;The requested URL was not found on this server.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;"""

    while True:

        try:

            labelIpAddress.setText('')

            if not wifiCfg.wlan_sta.isconnected():
                labelWebServer.setText('WS: RECON')
                wifiCfg.reconnect()
            else:
                ip = wifiCfg.wlan_sta.ifconfig()
                labelIpAddress.setText('IP ' + wifiCfg.wlan_sta.ifconfig()[0])
                
                server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                server.settimeout(30)
                server.bind((ip[0], 80))
                server.listen(1)
            
                labelWebServer.setText('WS: RUNS')

                (conn, addr) = server.accept()
                request = conn.recv(1024)
                
                if request:
                  request = str(request)
  
                  isRequestLevel = request.find('/api/v1.0/level ') &gt;= 0
  
                  if isRequestLevel:
                      labelWebServer.setText('WS: 200')
                      json = web_response_json
                      json = json.replace('@value@', str('%.2f'
                              % levelPercent))
                      json = json.replace('@unit@', '%')
                      response = json
                      conn.send('HTTP/1.1 200 OK\r\n')
                      conn.send('Content-Type: application/json\r\n')
                      conn.send('Connection: close\r\n\r\n')
                  else:
                      labelWebServer.setText('WS: 404')
                      response = web_error_404
                      conn.send('HTTP/1.1 404 NOT FOUND\r\n')
                      conn.send('Content-Type: text/html\r\n')
                      conn.send('Connection: close\r\n\r\n')
  
                  conn.sendall(response)

        except Exception as e:
            gc.collect()
            labelWebServer.setText('WS: ERR')

        finally:
            try:
                conn.close()
                server.close()
            except Exception as e:
                pass
            finally:
                pass
</code></pre>
]]></description><link>https://community.m5stack.com/post/7538</link><guid isPermaLink="true">https://community.m5stack.com/post/7538</guid><dc:creator><![CDATA[M5StickFreakler]]></dc:creator><pubDate>Sun, 23 Feb 2020 17:08:50 GMT</pubDate></item></channel></rss>