In the following eBook (great book by the way!):
“Welcome to MicroPython Programming with ESP32 and ESP8266” in web servers page 203
I checked the boot.py with syntax and checked ok.
I checked Main.py with syntax check, and attempts to download and run, brings up the following error
“MicroPython v1.20.0 on 2023-04-26; ESP module with ESP8266
Type “help()” for more information.
>>>
main.py:8: undefined name ‘socket’
main.py:8: undefined name ‘socket’
main.py:8: undefined name ‘socket’
syntax finish.”
The code in line 8 reads
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Your advise greatly appreciated
Thanking you
Hi.
Try cutting the whole lines of code on boot.py and paste them to the start of main.py.
let me know if this solves the issue.
Regards,
Sara
Hello Sara
Thanking you
Pasting boot.py into main.py syntax check runs ok without any errors being flagged.
DownloadAndRun produced the following errors:
Ready to download this file,please wait!
………
download ok
exec(open(‘main.py’).read(),globals())
Traceback (most recent call last):
File “”, line 1, in
File “”, line 7, in
NameError: name ‘esp’ isn’t defined
So we have progressed - a little
Thanking you for further help
try:
import usocket as socket
except:
import socket
import network
import esp
esp.osdebug(None) #error appears to occur on this line
import gc
gc.collect()
ssid = 'REPLACE_WITH_YOUR_SSID'
password = 'REPLACE_WITH_YOUR_PASSWORD'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('Connection successful')
print(station.ifconfig())
# Complete project details at https://RandomNerdTutorials.com
def web_page():
html = """
Hello, World!"""
return html
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
print('Content = %s' % str(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()
I think you can remove the line that is causing the issue.
However, I don’t know why that error is caused 😐
Try having those lines on the boot.py and the rest of the code in main.py.
This lines would go on boot.py:
import esp
esp.osdebug(None) #error appears to occur on this line
import gc
gc.collect()
Let me know if this solves the problem.
Regards,
Sara
Hello Sara Thanking you heaps! Finally got the py program to work, with a bit of juggling
Boot.Py is now:
import esp
esp.osdebug(None) #error appears to occur on this line
import gc
gc.collect()
Main.py is now
#try:
import usocket as socket
#except:
#import socket
import network
import esp
#esp.osdebug(None)
import gc
gc.collect()
ssid = 'Telstra7E9306'
password = 's5ynjh2rdf'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('Connection successful')
print(station.ifconfig())
# Complete project details at https://RandomNerdTutorials.com
def web_page():
html = """
Hello William!"""
return html
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
print('Content = %s' % str(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(
Please note the “Hashed out” lines where errors had occurred.
Many thanks again and now onto the next task of getting the “on-Off” LEDs working!