Maker Pro
Maker Pro

W600-pico webserver

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,722
Joined
Nov 17, 2011
Messages
13,722
Just got me a pair of W600-pico modules with built-in micropython interpreter.
These modules are relatively recent and documentation is scarce.
Using REPL via terminal and accessing the filesystem via ftpserver works like a charm.

Where I'm stuck is this: Could anyone point me to info how to create a webserver in mircopython for these modules so I can read input and control outputs via a HTML page?
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,722
Joined
Nov 17, 2011
Messages
13,722
I found out how to do it by cobbling together info from several websites and tutorials:
Code:
# based mainly on https://www.youtube.com/watch?v=GVMuER7A770
from machine import Pin
import network, gc
import easyw600, w600

# setup garbage collection
gc.collect()

print("Connecting to local WiFi") #debugging
# connect to local WiFi network
ssid = "My_Dark_Home_Network"
password = "HartToTell"
easyw600.connect(ssid, password)

print("Connection successful")
#print(sta.ifconfig())

# setup the webserver
try:
    import usocket as socket
except:
    import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create new socket, address family type = AF_INET, socket type = SOCK_STREAM
s.bind(('', 80))
s.listen(5) # accept connections. backlog = 5 = max. number of unaccepted connections before refusing new connections

# interact with the hardware
led = Pin(Pin.PA_00, Pin.OUT, Pin.PULL_FLOATING)
led_off = 1 # inverted logic
led_on = 0

# web page data
def web_page():
    file = open("index.html", "r")
    page = file.read()
    file.close()
    return page


# this is the main loop to handle requests
while True:
    conn, addr = s.accept() # accept connection with a new socket object "conn" to remote address "addr"
    request = conn.recv(1024) # receive from connected socket "conn" using 1024 byte buffer
    print(request) # for debugging only
    if "GET /?led=on" in request:
        led.value(led_on)
    if "GET /?led=off" in request:
        led.value(led_off)
      
#    led_status = ("ON", "OFF") led.value() ==1 # this should be valid python code, too, but I prefer the better readable form below.
    if led.value == 1: # same as before but imho better readable
        led_status = "ON"
    else:
        led_status = "OFF"
    response = web_page() % led_status # insert the actual status into the web page
    print(response) # debug
    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()

You need the index.html file, too:
Code:
<html>
<body>
    <h1>Hello - This is my micropython webserver's index.html</h1>
    <p>LED Status: <strong>%s</strong></p>
    <p><button onclick="window.location.href = '/?led=on'">ON</button></p>
    <p><button onclick="window.location.href = '/?led=off'">OFF</button> </p> 
</body>
</html>
 
Top