Problems in connecting to WiFi network while using Thonny: "WiFi internal error"

4,222 views
Skip to first unread message

Lauri Mat

unread,
Oct 25, 2021, 3:11:46 PM10/25/21
to thonny
Hi folks,

I am trying to connect with Thonny IDE while using the following code scripts of boot.py and main.py, and with ESP32 + DHT11 sensor measuring temperature and humidity. However, I never get a notification to the REPL stating an established connection. Instead, I do get sensor values, but I get either nothing after the sensor values, or I get "OS Error: WiFi internal error". My WiFi works fine otherwise; I can access webpages, so I'm relatively sure this is not about my WiFi.

This is weird, because this used to work earlier and I got an IP address to the REPL to open a webpage (for observing sensor data). But now I only get the temperature and humidity values to my REPL, without data about connecting to the wifi network.

Could the fact of the REPL not giving the IP address have something to do with available memory? This is how it was done in a similar topic by adding a line of C-code, but I think it is a different line for micropython: https://github.com/micropython/micropython/issues/7719

How could I possibly resolve this? Thanks in advance for any ideas.

Here is the code:

boot.py:

try:

import usocket as socket

except:

import socket

from time import sleep

from machine import Pin


import network

import esp

esp.osdebug(None)


import gc

gc.collect()


ssid = 'Mokkula'

password = 'XXXXXXXXX'


station = network.WLAN(network.STA_IF)


station.active(True)

station.connect(ssid, password)


while station.isconnected() == False:

pass


print('Connection successful')

print(station.ifconfig())


main.py:

#dht:

import dht

sensor = dht.DHT11(Pin(14))


def web_page():

temp = sensor.temperature() #dht

hum = sensor.humidity() #dht

dp = temp - (100 - hum)/5


html = """<!DOCTYPE HTML><html><head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">

<style> html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center; }

h2 { font-size: 3.0rem; } p { font-size: 3.0rem; } .units { font-size: 1.2rem; }

.ds-labels{ font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px; }

</style></head>

<body><h2>ESP32 Mittaukset</h2>

<p><table border=0 align=center><tr><td align=left><span class="ds-labels">L&auml;mp&ouml;tila </span></td><td><span class="ds-labels">""" + str(round(temp * 10 / 10, 2)) + """</span><span class="ds-labels"> &deg;C</span></td></tr>

<tr><td align=left><span class="ds-labels">Kosteus </span></td><td><span class="ds-labels">""" + str(hum) + """</span><span class="ds-labels"> %</span></td></tr>

<tr><td align=left><span class="ds-labels">Kastepiste </span></td><td><span class="ds-labels">""" + str(dp) + """</span><span class="ds-labels">&deg;C</span></td></tr></table>

</p>

</body></html>


"""

return html


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind(('', 80))

s.listen(5)


while True:

try:

sleep(2) #dht

sensor.measure() #dht

temp = sensor.temperature() #dht

hum = sensor.humidity() #dht

temp_f = temp * (9/5) + 32.0 #dht

print('Temperature: %3.1f C' %temp) #dht

print('Temperature: %3.1f F' %temp_f) #dht

print('Humidity: %3.1f %%' %hum) #dht

if gc.mem_free() < 102000:

gc.collect()

conn, addr = s.accept()

conn.settimeout(3.0)

print('Got a connection from %s' % str(addr))

request = conn.recv(1024)

conn.settimeout(None)

request = str(request)

print('Content = %s' % request)

response = web_page()

conn.send('HTTP/1.1 200 OK\n')

conn.send('Content-Type: text/html\n')

conn.send('Connection: close\n\n')

conn.sendall(response)

conn.close()

except OSError as e:

conn.close()

print('Connection closed')

Message has been deleted

Lauri Mat

unread,
Oct 26, 2021, 11:13:24 AM10/26/21
to thonny
Today I rebooted my modem and tried again. Don't know if that made the difference, but now I am at least getting info about connection attempts to the REPL. First it displayed only the sensor values, but after a half a minute or so it began to connect:

"MicroPython v1.16 on 2021-06-23; ESP32 module with ESP32
Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT
Temperature: 19.0 C
Temperature: 66.2 F
Humidity: 44.0 %
Got a connection from ('192.168.8.103', 34802)
Content = b'GET / HTTP/1.1\r\nHost: 192.168.8.105\r\nConnection: keep-alive\r\nCache-Control: max-age=0\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9\r\n\r\n'
Temperature: 19.0 C
Temperature: 66.2 F
Humidity: 42.0 %
Got a connection from ('192.168.8.103', 34804)
Content = b'GET /favicon.ico HTTP/1.1\r\nHost: 192.168.8.105\r\nConnection: keep-alive\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36\r\nAccept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8\r\nReferer: http://192.168.8.105/\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9\r\n\r\n'
Temperature: 19.0 C
Temperature: 66.2 F
Humidity: 42.0 %
Got a connection from ('192.168.8.103', 34806)
Connection closed
Temperature: 19.0 C
Temperature: 66.2 F
Humidity: 40.0 %"

But as this output states, the connection gets closed right away. So I still cannot access that IP address in my browser.

Oh yes, I am using Ubuntu 20.04, if that makes any difference.

Lauri Mat

unread,
Oct 26, 2021, 11:17:33 AM10/26/21
to thonny
Lol, I had forgotten that I should press the EN button on the ESP32 dev board to run my code, instead of pressing the Run button at the Thonny menu bar. Now it connects perfectly.Cheers.
Reply all
Reply to author
Forward
0 new messages