• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP 8266 web server hangs at "exec(open('main.py').read(),globals())"

Q&A Forum › Category: ESP8266 › ESP 8266 web server hangs at "exec(open('main.py').read(),globals())"
0 Vote Up Vote Down
William Spedding asked 2 years ago

Hello Random Nerds
I have ESP8266 Webserver in Micro Python Programming from Ebook not working
I have updated the code to eliminate syntax errors
Error is as follows
>>>
Ready to download this file,please wait!
………………
download ok
exec(open(‘main.py’).read(),globals())
……………..
Then just hangs there.
boot.py as follows

try:
import usocket as socket
except:
import socket
from machine import Pin
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())
led = Pin(2, Pin.OUT)

Main.py as follows

# Complete project details at https://RandomNerdTutorials.com
#Downloaded 26/5/2023. Updated to remove errors on check syntax
#try:
import usocket as socket
#except:
#import socket
import network
import esp
esp.osdebug(None)
import gc
gc.collect
ssid = 'Telstra7E9306'
password = 's5ynjh2rdf'
from machine import Pin
led = Pin(2, Pin.OUT)
def web_page():
html = """
ESP Web ServerON 
OFF"""
return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
try:
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)
led_on = request.find('/?led=on')
led_off = request.find('/?led=off')
if led_on == 6:
print('LED ON')
led.value(1)
if led_off == 6:
print('LED OFF')
led.value(0)
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')

Would really appreciate some help with this
Interestingly
Clicking on “Stop” icon
Then manually typing in
Pin(2,1) or Pin(2,0) will turn the LED on or off
So some of the program is in there and running.
Seems to be in the HTML section?

3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Hi.
It seems that maybe the boot.py file is not running.
Can you copy everything from the boot.py to the beginning of main.py? And try again?
Regards,
Sara

0 Vote Up Vote Down
William Spedding answered 2 years ago

Hi Sara
Well! That worked!
All good
Much progress
Problem solved
I inverted the logic, as “led(1) was actually Led off, but I think that was mentioned somewhere in this or another tutorial
Now for multiple LEDs with on off control for each, which is my goal
Many thanks
Keep the tutorials coming!

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Great!.
For some reason, it is not reading the boot.py before running the main.py
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.