webserver to turn an LED on or off

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
ideal2545
Posts: 12
Joined: Tue May 17, 2016 9:08 am

webserver to turn an LED on or off

Post by ideal2545 » Fri May 20, 2016 8:01 pm

Hi Guys,

To start: I'm new to microcontroller programming in general but I like python.

Question:
With LUA I was able to figure out how to get a webserver going to turn an LED on or turn it off with an html button. I'm trying to do the same thing with micropython but I'm really having a hard time figuring this out, especially on the ESP8266 it seems. Could anyone point me in the right direction or give me some tips? I can create a basic socket http server but that is as far as i'm getting.

I tried messing with picoweb (https://github.com/pfalcon/picoweb) but I can't get all the libraries loaded. I can't figure out if there's a faster way for me to install the libraries I need rather than by using mpfshell to push them to the root folder one by one. I thought maybe I could compile them into micropython and then load up a new .bin but I can't find documentation on how to do that.

Any help or tips would be really appreciated.

Jon

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: webserver to turn an LED on or off

Post by deshipu » Fri May 20, 2016 8:56 pm

You can have all the libraries you need precompiled and included in your image simply by putting them in the "esp8266/scripts" directory before compiling.

ideal2545
Posts: 12
Joined: Tue May 17, 2016 9:08 am

Re: webserver to turn an LED on or off

Post by ideal2545 » Sun May 22, 2016 7:09 am

Got it figured out by doing some cobbling together of documents and research... for anyone who needs a little tutorial in the future:
(and if anyone see's a better/more efficient way for me to accomplish this can you please tell me?)

Code: Select all

import socket 
import machine


#HTML to send to browsers
html = """<!DOCTYPE html>
<html>
<head> <title>ESP8266 LED ON/OFF</title> </head>
<center><h2>A simple webserver for turning HUZZAH Feather LED's on and off with Micropython</h2></center>
<center><h3>(for noobs to both the ESP8266 and Micropython)</h3></center>
<form>
LED0: 
<button name="LED" value="ON0" type="submit">LED ON</button>
<button name="LED" value="OFF0" type="submit">LED OFF</button><br><br>
LED2: 
<button name="LED" value="ON2" type="submit">LED ON</button>
<button name="LED" value="OFF2" type="submit">LED OFF</button>
</form>
</html>
"""

#Setup PINS
LED0 = machine.Pin(0, machine.Pin.OUT)
LED2 = machine.Pin(2, machine.Pin.OUT)

#Setup Socket WebServer
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))
    request = str(request)
    LEDON0 = request.find('/?LED=ON0')
    LEDOFF0 = request.find('/?LED=OFF0')
    LEDON2 = request.find('/?LED=ON2')
    LEDOFF2 = request.find('/?LED=OFF2')
    #print("Data: " + str(LEDON0))
    #print("Data2: " + str(LEDOFF0))
    if LEDON0 == 6:
        print('TURN LED0 ON')
        LED0.low()
    if LEDOFF0 == 6:
        print('TURN LED0 OFF')
        LED0.high()
    if LEDON2 == 6:
        print('TURN LED2 ON')
        LED2.low()
    if LEDOFF2 == 6:
        print('TURN LED2 OFF')
        LED2.high()
    response = html
    conn.send(response)
    conn.close()

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: webserver to turn an LED on or off

Post by pfalcon » Tue May 24, 2016 7:33 am

MicroPython comes with examples of (bare minimum) HTTP clients and servers, both plain HTTP and HTTPS each: https://github.com/micropython/micropyt ... es/network . These were included in the official KS release for easy start. A more complex example of HTTP handling is WebSocket handshake routines, also in the codebase: https://github.com/micropython/micropyt ... _helper.py . Going forward, we'll be preparing more easy to use modules for HTTP serving, but the above should be a good start for anyone wanting to do it a simple way right away.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

robert1968
Posts: 4
Joined: Sun Oct 01, 2017 4:20 pm

Re: webserver to turn an LED on or off

Post by robert1968 » Sun Oct 01, 2017 4:37 pm

[quote="ideal2545"]Got it figured out by doing some cobbling together of documents and research... for anyone who needs a little tutorial in the future:
(and if anyone see's a better/more efficient way for me to accomplish this can you please tell me?)

[code]import socket
import machine
#HTML to send to browsers
.
.
.
if LEDON0 == 6:
print('TURN LED0 ON')
LED0.low()
if LEDOFF0 == 6:
print('TURN LED0 OFF')
LED0.high()
if LEDON2 == 6:
print('TURN LED2 ON')
LED2.low()
if LEDOFF2 == 6:
print('TURN LED2 OFF')
LED2.high()
conn.close()[/code][/quote]


Thanks for this simple and effective code! But must correct the code because in the new version of micropython (esp8266-20170823-v1.9.2)
machine.pin has changed not "high" and "low" but "on" and "off"

so change this part of the code:
[code]LED0.low()
to
LED0.off()[/code]

and
[code]LED0.high()
to
LED0.on()[/code]

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: webserver to turn an LED on or off

Post by pythoncoder » Tue Oct 03, 2017 7:33 am

pfalcon wrote:...A more complex example of HTTP handling is WebSocket handshake routines, also in the codebase: https://github.com/micropython/micropyt ... _helper.py...
This is a 404 and should read https://github.com/micropython/micropyt ... _helper.py
Peter Hinch
Index to my micropython libraries.

Post Reply