Skip to main content

Some wifi functions for MicroPython

Project description

Eydam-Prototyping: ep_http

Simple Library to run a http-server on an ESP32 or other MicroPython-enabled device. Currently not really stable.

Usage

# main.py

import ep_file_server
import ep_rest_server
import ep_http
import network
import time 
import ubinascii

wlan=network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("<SSID>", "<Password>")

while not wlan.isconnected():
    time.sleep(1)
print("Connected")

# just return files to client
fs = ep_file_server.file_server(
    html_dir="/html/",             # directory with files
    default_file="index.html"      # default file that is returned when no specific file is requested
)

# edit a json configuration file via rest api 
# currently supported: GET, PUT
crs = ep_rest_server.config_rest_server(
    config_file="./config.json"
)

# sensor reading for sensor_rest_server, must return json serializeable dict
def scan_wifi(path):
    nets = wlan.scan()
    result = []
    for ssid, bssid, channel, rssi, authmode, hidden in nets:
        net = {
            "ssid": ssid.decode("ascii"),
            "bssid": ubinascii.hexlify(bssid).upper(),
            "channel": channel,
            "rssi": rssi,
            "authmode": authmode,
            "hidden": hidden
        }
        result.append(net)
    return result

# return sensor reatings via rest api
# currently supported: GET
srs = ep_rest_server.sensor_rest_server(
    [
        ("^wifinets$", scan_wifi)   # assignment function <-> route
    ]
)

def default_route(sock, request):
    print("unhandled request")
    print(request)    

routes = [
    # files are available via http://<ip>/files/yourfile.html
    ("^\/?files\/([A-Za-z0-9_\.\/]*)\??([A-Za-z0-9_\.\/]*)$", lambda sock, req: fs.serve(sock, req)),
    # configurations are available via http://<ip>/config/hierachy/of/json/file
    ("^\/?config\/?([A-Za-z0-9_\.\/]*)\??([A-Za-z0-9_\.\/]*)$", lambda sock, req: crs.serve(sock, req)),
    # sensor readings are available via http://<ip>/sensor/wifinets
    ("^\/?sensor\/?([A-Za-z0-9_\.\/]*)\??([A-Za-z0-9_\.\/]*)$", lambda sock, req: srs.serve(sock, req)),
    # favicon is forwarded to file server
    ("^(favicon\\.ico)$", lambda sock, req: fs.serve(sock, req)),
    # route, if nothing else matches
    ("^(.*)$", default_route),
]

s = ep_http.http_server(routes=routes, micropython_optimize=True)
print("Starting HTTP-Server: " + wlan.ifconfig()[0])
s.start()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

File details

Details for the file micropython_eydam-prototyping_ep_http-0.0.2.tar.gz.

File metadata

  • Download URL: micropython_eydam-prototyping_ep_http-0.0.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.2

File hashes

Hashes for micropython_eydam-prototyping_ep_http-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b426b728be62ef6693831c8c40d5c7bcd8e2dc0b4208881b975364e2ab7462b0
MD5 f84085088582e0bfd8eaed58f02c5c21
BLAKE2b-256 0aedb879e8e6e0cd75ad86f92366ad0fb711d82a29786e63ff9e54489b8cc6f0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page