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?
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
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!