Giter Site home page Giter Site logo

espwebserver's Introduction

ESPWebServer

This is a very lightweight web server for MicroPython on ESP8266/32.It only accept GET, POST and PUT requests.It adopts the programming style of ESP8266WebServer library in ESP8266 Arduino Core.This make it suitable for serving REST API.The original code was inspired from the project Controlling a GPIO through an ESP8266-based web server.

Installation

Just upload ESPWebServer.py to your ESP8266/32 board and you're done.

Usage

To use ESPWebServer.py library, you should:

  1. Write functions as handlers for each path you'd like to serve contents.

  2. Start the server by calling begin().

  3. Regsiter the handlers you just prepared by calling onPath().

  4. You can also uploading HTML files onto somewhere in the filesystem and settnig the document path by calling setDocPath().

  5. Call handleClient() repeatly to process requests.

Documents and Templates

With setDocPath(), you can spcicified the path for all html files. For examples, if you call setDocPath('www'), and put index.html into /www, you can browse the file with 'http://server_ip/www/index.html.

If you put a file with suffix '.p.html', the server would do formatting processing before output the content. You should first call setTplData() with a dictionary before accessing any template file, the server uses the elements in the dictionary to replacing all the formatting string in the template file.

If you access the document path without filename, the server would try to find out if there's a index.html or index.p.html file existed, and output the file.

Function reference

begin(port)

Start the server at specified port.

onPath(path, handler)

Legacy method to ensure compatibility. Calls onGetPath(path, handler).

onGetPath(path, handler)

Registers a handler for handling GET Requests.

The Handlers expected Method Signature: methodName(socket, args)

onPostPath(path, handler)

Registers a handler for handling POST Requests.

The Handlers expected Method Signature: methodName(socket, args, contenttype, content)

onPutPath(path, handler)

Registers a handler for handling PUT Requests.

The Handlers expected Method signature: methodName(socket, args, contenttype, content)

setDocPath(path)

Specified the directory in the filesystem containing all the HTML files.

setTplData(dic)

Specified the dictionary for template file. dic sould be a dictionary with all keys are string and contains all the names in replacing fields in all the template files.

setMaxContentLength(size)

Defines the maximum Content Length of incoming request bodies (POST, PUT) in bytes. Default: 1024

handleClient()

Check for new request and call corresponding handler to process it.

Examples

You can upload www directory and index.p.html to "/" on ESP8266 board and run TestWebServer.py to see how it works.

main.py contains an example for handling POST Requests. PUT Requests are acting the same way.

TestWebServer.py will show its own IP address through serial monitor.Just open your browser and connect it to http://serverIP:8899 or http://serverIP:8899/index.p.html, you'll get the main page that can turn on/off the buildin led on ESP8266 board. The main page also demonstrate the template file usage.

You can also open http://serverip:8899/www/index.html or http://serverip:8899/www/ to view alternative version of controlling page that use AJAX to asynchronously turn on/off led.

You can use http://serverip:8899/switch to switch led on/off led directly. Or you can use http://serverip:8899/cmd?led=on to turn the led on and http://serverip:8899/cmd?led=off to turn the led off.

espwebserver's People

Contributors

codemee avatar samson6460 avatar software-atelier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

espwebserver's Issues

How to have ESPWebServer.py report messages to onboard display

I am fairly new to MicroPython/Python but have been programming in several languages for years.

Device:
Lilygo ESP32 TTGO with T-Display (on-board display)

firmware with ST7789 driver(s)
https://github.com/russhughes/st7789_mpy
(MicroPython v1.18-128-g2ea21abae-dirty on 2022-02-12; TTGO T-Display with ESP32)
ST7789 config file for T-Display
https://github.com/russhughes/st7789_mpy/tree/master/examples/configs/tdisplay_esp32

webserver software (from here)
https://github.com/codemee/ESPWebServer

my copy of TestWebServer.py is modified to work in AP (private network) mode and to use the on-board display to display IP address. I would like this to also handle messages from ESPWebServer.py.

Currently, ESPWebServer.py is unmodified in regards to its reporting (serial monitor, print()). This, of course, requires you to have another device connected to see the messages (computer, smartphone with usb serial software, etc.). This is fine for testing, not so much for stand alone operation.

How would I get ESPWebServer.py to notify its importer (TestWebServer.py in this case) that A) it has a message, B) type (general/error/etc.) and C) the message's content so that the importer could process the message (example: "ERROR" and "file $name$ not found"; "General" and "file $name$ found"; "Status", "GPIO## is set HIGH")?

Option: can ESPWebServer.py be written/rewritten to be a standalone program (ESPWebServer-Standalone.py?, no need for TestWebServer.py), therefore handling all reporting requests itself (with print() at first then I could add my modifications to that)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.