bondrogeen / web-server

15 stars 3 forks source link
esp8266 lua nodemcu webserver

web-server

Web server for NodeMCU firmware for ESP8266. Rather, this is the basic template for your projects with a web interface.

Rus

!!! The project develops in a different direction. Details DoT

Features

Structure

Initialization:

The server consists of four main scripts:

Files:

Installation

  1. The modules you need (file, gpio, net, node, sjson, tmr, uart, wifi). Building the firmware
  2. Download all files to the module.
  3. Connect to the access point ESP-XXXXXX and go to the address 192.168.4.1.

Logo

  1. Enter the login (admin) and password (0000).

Logo

  1. Go to Settings.

Logo

  1. Connect to your wi-fi network

How to work with lua scripts

Example file "my_script.lua"


local gpio_to_pin = {  -- get number pin
  GPIO5 = 1,
  GPIO4 = 2,
  GPIO0 = 3,
  GPIO2 = 4,
  GPIO14 = 5,
  GPIO12 = 6,
  GPIO13 = 7,
  GPIO15 = 8,
  GPIO3 = 9,
  GPIO1 = 10,
  GPIO9 = 11,
  GPIO10 = 12
}

local function readGPIO(tab)  -- read GPIO

  local pin, response = gpio_to_pin[tab["read"]], {}
  if pin then
--    gpio.mode(pin, gpio.INPUT)
    response[tab["read"]] = gpio.read(pin)
    return response
  end
  return false
end

local function writeGPIO(tab)  -- write GPIO

  local pin, value, response = gpio_to_pin[tab["write"]], tonumber(tab["value"]), {}  
  if (pin and (value == 1 or value == 0)) then
    gpio.mode(pin, gpio.OUTPUT)
    gpio.write(pin, value)
    response[tab["write"]] = gpio.read(pin)
    return response
  end
  return response
end

return function (args)   -- args - table with arguments
  local response = false
  if args.read then response = readGPIO(args) end
  if args.write then response = writeGPIO(args) end
 return response  -- The table will be converted to a JSON object.
end       

Parameters from forms (if any) will be passed to the table args .

http://192.168.1.49/my_script.lua?read=GPIO5

http://192.168.1.49/my_script.lua?write=GPIO13&value=0

Logo

Contributing

Contributions are welcome.

The package is made up of 2 main folders:

To setup and run a local copy:

  1. Clone this repo with git clone https://github.com/bondrogeen/web-server
  2. Run npm install in root folder

After installing the dependencies, you can start working with the sources.

  1. Run gilp build (Compile and compress files)

When you're done working on your changes, submit a PR with the details and include a screenshot if you've changed anything visually.

Restrictions.

The server processes the files in different ways, so for files with the extension .html the reading from the file is progressive, this is done to simplify the processing of the built-in Lua code, there is no limitation on the file size. With files with the .lua extension, the size of the sent data is no more than 4KB. All other files are transferred byte by byte (1024 bytes at a time), there are also no restrictions on the file size. The server can not receive data more than 1.4KB (data + header). While there was no such need.)))

Changelog

0.2.0 (2019-03-06)

0.1.2 (2018-06-18)