• 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

MicroPython ESP Web Server

Q&A Forum › Category: ESP8266 › MicroPython ESP Web Server
0 Vote Up Vote Down
gj.linthorst asked 6 years ago

I wanted to add one output more in the ESP Web Server
I added in boot.py file:  leda = Pin(0, Pin.OUT)
In the main.py file I modified to let “leda” go on or off but i get a failure.
I have no idee how to fix it.

Start your code here
# Complete project details at https://RandomNerdTutorials.com

def web_page():
  html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head>
  <body><h1>ESP Web Server</h1><a href=\"?led=on\"><button>ON</button></a>&nbsp;
  <a href=\"?led=off\"><button>OFF</button></a>
  <a href=\"?leda=on\"><button>ON</button></a>&nbsp;
  <a href=\"?leda=off\"><button>OFF</button></a></body></html>"""

  
  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)
  request = str(request)
  print('Content = %s' % request)
  led_on = request.find('/?led=on')
  led_off = request.find('/?led=off')
  leda_on = request.find('/?led=on')
  leda_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)
  if leda_on == 6:
    print('LED ON')
    led.value(1)
  if leda_off == 6:
    print('LED OFF')
    led.value(0)
  
  response = web_page()
  conn.send(response)
  conn.close()

 

5 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

It looks like you didn’t add the proper leda name (there’s an “a” missing):

leda_on = request.find('/?leda=on')
leda_off = request.find('/?leda=off')

It’s also missing here:

if leda_on == 6:
print('LED ON')
leda.value(1)
if leda_off == 6:
print('LED OFF')
leda.value(0)

However, that doesn’t seem to be the problem. What’s the exact error that you’re having? I need more information about what’s not working for you.

0 Vote Up Vote Down
gj.linthorst answered 6 years ago

Hello,
I modified  the main menu with the missing a. I still get the same error
When I run the file main.py: I get following text in the serial display.
download ok
exec (open(.’mainl.py’).read(),globals())
Traceback (most recent call last):
    File “<stdin>”, line 1, in <module>
    File “<string>”, line 26
IndentationError: unindent does not match any outer indentation level
>>>
 
 
 
 

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

You’re missing a space or you have extra spaces. In Python there’s no { } or ; so all that is done with indentation that you need to respect (2 or 4 spaces). For example:

while True:
if leda_on == 6:
print('LED ON')
leda.value(1)
if leda_off == 6:
print('LED OFF')
leda.value(0)

Basically, the if has 2 spaces, then the print has 4 spaces. How does your line 26 look like?

0 Vote Up Vote Down
gj.linthorst answered 6 years ago

Problem solved with the missing space and a.

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

I’m glad it’s working! Regards,
Rui

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.