├── uninstall.bat ├── uninstall.sh ├── src ├── ports.json ├── fLoad.lua ├── restart.lua ├── send.lua ├── fpUpdate.lua ├── fList.lua ├── fDel.lua ├── sendJson.lua ├── init.lua ├── dnsLiar.lua ├── http.lua ├── LICENSE ├── rs.lua ├── respFile.lua ├── cfg.lua ├── pState.lua ├── pUpdate.lua ├── fSave.lua ├── connect.lua ├── request.lua ├── cfgSta.lua ├── index.htm ├── cfgFile.lua ├── uart.htm ├── fSaveI.lua ├── cfg.htm ├── ide.htm ├── ju.js ├── cfgPort.htm └── ctl.htm ├── install.sh ├── install.bat ├── update.sh ├── update.bat ├── LICENSE └── README.md /uninstall.bat: -------------------------------------------------------------------------------- 1 | set ESP_LUA_PYTHON_DIR=C:\Proggy\ESP8266\uploader 2 | 3 | python %ESP_LUA_PYTHON_DIR%\upload_and_do1.py %ESP_LUA_PYTHON_DIR%\format.lua 4 | -------------------------------------------------------------------------------- /uninstall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ESP_LUA_PYTHON_DIR=~/Proggy/ESP/uploader 4 | 5 | python $ESP_LUA_PYTHON_DIR/upload_and_do.py $ESP_LUA_PYTHON_DIR/format.lua 6 | -------------------------------------------------------------------------------- /src/ports.json: -------------------------------------------------------------------------------- 1 | {"gpio":[ 2 | {"l":"Arduino%20LED","p":13,"t":"D","c":"switch"}, 3 | {"l":"Socket%201","p":2,"i":1,"t":"D","c":"switch"}, 4 | {"l":"Socket%202","p":4,"i":1,"t":"D","c":"switch"} 5 | ]} 6 | -------------------------------------------------------------------------------- /src/fLoad.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | return function(c,p,u) 4 | package.loaded[z] = nil 5 | z = nil 6 | if u > 1 then 7 | require("rs")(c, 401) 8 | return 9 | end 10 | require("respFile")(c, p and p.name, "json") 11 | end 12 | -------------------------------------------------------------------------------- /src/restart.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | return function(c,p,u) 4 | package.loaded[z] = nil 5 | z = nil 6 | if u > 1 then 7 | require("rs")(c, 401) 8 | return 9 | end 10 | require("rs")(c, 200, "") 11 | tmr.create():alarm(1000, 0, function() node.restart() end) 12 | end 13 | -------------------------------------------------------------------------------- /src/send.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | return function(co,p) 4 | package.loaded[z] = nil 5 | z = nil 6 | uart.on("data", "\n", function(re) 7 | uart.on("data") 8 | require("rs")(co, 200, node.heap().."\n"..re) 9 | end, 0) 10 | uart.write(0, (p.cmd or "NG").."\n") 11 | end 12 | -------------------------------------------------------------------------------- /src/fpUpdate.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | return function() 4 | package.loaded[z] = nil 5 | z = nil 6 | local f = file.open("ports.json") 7 | local d = cjson.decode(f:read()).gpio 8 | f:close(); f = nil 9 | tmr.create():alarm(200, 0, function() require("pUpdate")(d) end) 10 | end 11 | -------------------------------------------------------------------------------- /src/fList.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | return function(c,p,u) 4 | package.loaded[z] = nil 5 | z = nil 6 | p = nil 7 | if u > 1 then 8 | require("rs")(c, 401) 9 | return 10 | end 11 | collectgarbage() 12 | local d = {} 13 | d.f = file.list() 14 | d.ir, d.iu, d.it = file.fsinfo() 15 | require("sendJson")(c, d) 16 | end 17 | -------------------------------------------------------------------------------- /src/fDel.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | return function(co,p,u) 4 | package.loaded[z] = nil 5 | z = nil 6 | if u > 1 then 7 | require("rs")(co, 401) 8 | return 9 | end 10 | if p and p.name then 11 | file.remove(p.name) 12 | require("rs")(co, 200, "") 13 | else 14 | require("rs")(co, 403, "File to delete not specified") 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ESP_LUA_PYTHON_DIR=~/Proggy/ESP/uploader 4 | 5 | #python $ESP_LUA_PYTHON_DIR/uploader_9600.py $ESP_LUA_PYTHON_DIR/init.lua 6 | #sleep 2 7 | #python $ESP_LUA_PYTHON_DIR/upload_and_do_9600.py $ESP_LUA_PYTHON_DIR/restart.lua 8 | #sleep 5 9 | for i in ./src/*; do 10 | python $ESP_LUA_PYTHON_DIR/uploader.py "$i" 11 | done 12 | 13 | -------------------------------------------------------------------------------- /install.bat: -------------------------------------------------------------------------------- 1 | set ESP_LUA_PYTHON_DIR=C:\Proggy\ESP8266\uploader 2 | 3 | python %ESP_LUA_PYTHON_DIR%\uploader1_9600.py %ESP_LUA_PYTHON_DIR%\init.lua 4 | %windir%\System32\timeout /t 2 5 | python %ESP_LUA_PYTHON_DIR%\upload_and_do1_9600.py %ESP_LUA_PYTHON_DIR%\restart.lua 6 | %windir%\System32\timeout /t 5 7 | cd src 8 | for %%i in (*) do python %ESP_LUA_PYTHON_DIR%\uploader1.py %%i 9 | -------------------------------------------------------------------------------- /src/sendJson.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | return function(co, d) 4 | package.loaded[z] = nil 5 | z = nil 6 | local e = false 7 | co:on("sent", function(c) 8 | if e then 9 | c:close() 10 | c = nil 11 | return 12 | end 13 | c:send(cjson.encode(d)) 14 | d = nil 15 | e = true 16 | collectgarbage() 17 | end) 18 | require("rs")(co, 200, "", "application/json") 19 | end 20 | -------------------------------------------------------------------------------- /src/init.lua: -------------------------------------------------------------------------------- 1 | ls = "" 2 | cf = {} 3 | tmr.create():alarm(2000, 0, function() 4 | if not cjson then _G.cjson = sjson end 5 | uart.setup(0, 115200, 8, 0, 1, 0) 6 | uart.write(0, "\n\n") 7 | require("connect")(function() 8 | tmr.create():alarm(100, 0, function() 9 | require("http") 10 | require("dnsLiar") 11 | pcall(function() require("apps")() end) 12 | end) 13 | end) 14 | end) 15 | -------------------------------------------------------------------------------- /update.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ESP_LUA_PYTHON_DIR=~/Proggy/ESP/uploader 4 | 5 | python $ESP_LUA_PYTHON_DIR/upload_and_do.py $ESP_LUA_PYTHON_DIR/format.lua 6 | sleep 2 7 | #python $ESP_LUA_PYTHON_DIR/uploader.py $ESP_LUA_PYTHON_DIR/init.lua 8 | #sleep 2 9 | python $ESP_LUA_PYTHON_DIR/upload_and_do.py $ESP_LUA_PYTHON_DIR/restart.lua 10 | sleep 5 11 | for i in ./src/*; do 12 | python $ESP_LUA_PYTHON_DIR/uploader.py "$i" 13 | done 14 | -------------------------------------------------------------------------------- /src/dnsLiar.lua: -------------------------------------------------------------------------------- 1 | local o = not net.createUDPSocket 2 | ns = o and net.createServer(net.UDP) or net.createUDPSocket() 3 | local function dl(q) 4 | return q:sub(1, 2).."\129\128\0\1\0\1\0\0\0\0"..q:sub(13, q:find("\0", 13)).."\0\1\0\1\192\12\0\1\0\1\0\0\0\42\0\4\192\168\4\1" 5 | end 6 | local function od(c, q) c:send(dl(q)) end 7 | local function nd(c, q, p, i) c:send(p, i, dl(q)) end 8 | ns:on("receive", o and od or nd) 9 | ns:listen(53) 10 | -------------------------------------------------------------------------------- /update.bat: -------------------------------------------------------------------------------- 1 | set ESP_LUA_PYTHON_DIR=C:\Proggy\ESP8266\uploader 2 | 3 | python %ESP_LUA_PYTHON_DIR%\upload_and_do1.py %ESP_LUA_PYTHON_DIR%\format.lua 4 | %windir%\System32\timeout /t 2 5 | python %ESP_LUA_PYTHON_DIR%\uploader1.py %ESP_LUA_PYTHON_DIR%\init.lua 6 | %windir%\System32\timeout /t 2 7 | python %ESP_LUA_PYTHON_DIR%\upload_and_do1.py %ESP_LUA_PYTHON_DIR%\restart.lua 8 | %windir%\System32\timeout /t 5 9 | cd src 10 | for %%i in (*) do python %ESP_LUA_PYTHON_DIR%\uploader1.py %%i 11 | -------------------------------------------------------------------------------- /src/http.lua: -------------------------------------------------------------------------------- 1 | w = net.createServer(net.TCP, 20) 2 | w:listen(80, function(co) 3 | co:on("receive", function(c, q) 4 | local p, e, u, g = require("request")(q) 5 | q = nil 6 | if u > 9 then return end 7 | c:on("sent", function(ci) 8 | ci:close() 9 | ci = nil 10 | end) 11 | collectgarbage() 12 | if u > 2 then 13 | require("rs")(c, 401) 14 | elseif not e then 15 | tmr.create():alarm(100, 0, function() 16 | if not pcall(function() require(p)(c, g, u) end) then 17 | require("rs")(c, 404) 18 | end 19 | end) 20 | else 21 | g = nil 22 | require("respFile")(c, p, e) 23 | end 24 | end) 25 | end) 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2015-2021 Taras Greben 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /src/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2015-2021 Taras Greben 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /src/rs.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | local function rH(r, t, m, h) 4 | return "HTTP/1.1 "..r.."\r\nServer: devlab (nodemcu)\r\nContent-Type: "..(m or "text/html")..(h or '').. 5 | "\r\nConnection: close\r\nCache-Control: private, max-age="..(t and ("0, no-cache, no-store\r\n\r\n"..t) or "3628800\r\n\r\n") 6 | end 7 | 8 | return function(c, e, t, m) 9 | package.loaded[z] = nil 10 | z = nil 11 | if e == 200 then 12 | c:send(rH("200 OK", t, m)) 13 | elseif e == 401 then 14 | c:send(rH("401 Unauthorized", "", nil, "\r\nWWW-Authenticate: Basic realm=\"esp-devlab\"")) 15 | elseif e == 404 then 16 | c:send(rH("404 Not Found", t or "")) 17 | else 18 | c:send(rH("403 Forbidden", t or "")) 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /src/respFile.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | return function(co, p, e) 4 | package.loaded[z] = nil 5 | z = nil 6 | local res = {htm = "text/html", js = "application/javascript", json="application/json"} 7 | local m = res[e] 8 | res = nil 9 | local o = 0 10 | if m and file.exists(p) then 11 | co:on("sent", function(c) 12 | if not p then 13 | c:close() 14 | c = nil 15 | return 16 | end 17 | local f1 = file.open(p) 18 | f1:seek("set", o) 19 | local d = f1:read(1024) 20 | f1:close(); f1 = nil 21 | if d then 22 | if #d < 1024 then 23 | p = nil 24 | else 25 | o = o + #d 26 | end 27 | c:send(d) 28 | d = nil 29 | else 30 | p = nil 31 | c:close() 32 | c = nil 33 | end 34 | collectgarbage() 35 | end) 36 | require("rs")(co, 200, e == "json" and "" or nil, m) 37 | else 38 | require("rs")(co, 404, "Page not found") 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /src/cfg.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | return function(co,p,u) 4 | package.loaded[z] = nil 5 | z = nil 6 | if u > 1 then 7 | require("rs")(co, 401) 8 | elseif not p then 9 | require("rs")(co, 403, "No config passed") 10 | else 11 | if p.m == "sta" then 12 | if p.file then 13 | local d = cjson.decode(p.file) 14 | if not d.pwd then 15 | d.pwd = "" 16 | elseif #d.pwd < 8 then 17 | require("rs")(co, 403, "Password shall be either 8 - 64 chars long, or not specified") 18 | end 19 | ls = "Connecting to: "..(d.ssid or "").." ..." 20 | tmr.create():alarm(2000, 0, function(t) 21 | d.save = true 22 | wifi.sta.config(d) 23 | t:alarm(2000, 0, function() node.restart() end) 24 | end) 25 | end 26 | p = nil 27 | require("rs")(co, 200, ls) 28 | else 29 | local _, fm, fc = require("cfgFile")(p) 30 | p = nil 31 | require("rs")(co, fc, fm) 32 | end 33 | end 34 | collectgarbage() 35 | end 36 | -------------------------------------------------------------------------------- /src/pState.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | local function tx(s, f) 4 | uart.on("data", "\n", f, 0) 5 | uart.write(0, s.."\n") 6 | end 7 | 8 | local function loadP(p, f) 9 | if not p.t then 10 | f() 11 | return 12 | end 13 | tx(p.t.."G P"..(p.p or "0"), function(v) 14 | uart.on("data") 15 | if v and #v > 4 then 16 | v = v:sub(5) 17 | p.v = tonumber(v) 18 | else 19 | p.v = 0 20 | end 21 | v = nil 22 | collectgarbage() 23 | f() 24 | end) 25 | end 26 | 27 | return function(co,p) 28 | package.loaded[z] = nil 29 | z = nil 30 | p = nil 31 | local fp = file.open("ports.json") 32 | local d = cjson.decode(fp:read()) 33 | fp:close(); fp = nil 34 | local gp = d.gpio 35 | tmr.wdclr() 36 | local x = 1 37 | local i = #gp + 1 38 | tmr.create():alarm(10, 1, function(t) 39 | if not x then return end 40 | x = nil 41 | i = i - 1 42 | if i > 0 then 43 | loadP(gp[i], function() x = 1 end) 44 | else 45 | t:unregister() 46 | loadP = nil 47 | tx = nil 48 | collectgarbage() 49 | require("sendJson")(co,d) 50 | end 51 | end) 52 | end 53 | -------------------------------------------------------------------------------- /src/pUpdate.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | local function isI(c) 4 | return (c == "level" or c == "led") 5 | end 6 | 7 | local function tx(s, f) 8 | uart.on("data", "\n", function(v) 9 | v = nil 10 | uart.on("data") 11 | f() 12 | end, 0) 13 | uart.write(0, s.."\n") 14 | end 15 | 16 | local function storeP(p, f) 17 | if not p or p.t == "A" then f() return end 18 | if not p.p then p.p = 0 end 19 | local r = "CE P"..p.p 20 | if isI(p.c) then 21 | r = r..(p.u and " I1" or " I0") 22 | else 23 | r = r.." O"..p.t 24 | end 25 | tx(r, function() 26 | if not isI(p.c) then 27 | tx(p.t.."E P"..p.p.." V"..(p.v or "0"), f) 28 | else 29 | f() 30 | end 31 | end) 32 | end 33 | 34 | return function(d) 35 | package.loaded[z] = nil 36 | z = nil 37 | local x = 1 38 | local i = #d + 1 39 | tmr.create():alarm(10, 1, function(t) 40 | if not x then return end 41 | x = nil 42 | i = i - 1 43 | collectgarbage() 44 | if i > 0 then 45 | storeP(d[i], function() x = 1 end) 46 | else 47 | t:unregister() 48 | d = nil 49 | collectgarbage() 50 | end 51 | end) 52 | end 53 | -------------------------------------------------------------------------------- /src/fSave.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | local function save(v, fc) 4 | if v == nil then 5 | fc("Nothing") 6 | return 7 | end 8 | if not v.name or not v.file then 9 | fc("File body missing") 10 | return 11 | end 12 | local p = tonumber(v.pos) 13 | if p == 0 then p = nil end 14 | if not p then 15 | file.remove("-~"..v.name) 16 | end 17 | local rm, _, _ = file.fsinfo() 18 | if (rm - (p and 1 or 500) - 5000) < #v.file then 19 | fc("Not enough space on disk") 20 | return 21 | end 22 | p = nil 23 | rm = nil 24 | collectgarbage() 25 | tmr.wdclr() 26 | require("fSaveI")(v, fc) 27 | end 28 | 29 | local function after(c, v) 30 | local rs = true 31 | if v.name == "ports.json" and v.flush == "1" then 32 | require("fpUpdate")() 33 | end 34 | v = nil 35 | collectgarbage() 36 | require("rs")(c, 200, "") 37 | end 38 | 39 | return function(c,v,u) 40 | package.loaded[z] = nil 41 | z = nil 42 | collectgarbage() 43 | if u > 1 then 44 | require("rs")(c, 401) 45 | else 46 | save(v, function(er) 47 | if er == nil then 48 | after(c, v) 49 | else 50 | require("rs")(c, 403, "Not saved: "..er) 51 | end 52 | end) 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /src/connect.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | local i = 0 4 | 5 | local function setAP(a, f) 6 | local n = {ip = "192.168.4.1", netmask = "255.255.255.0", gateway = "192.168.4.1"} 7 | wifi.ap.config(a) 8 | wifi.ap.setip(n) 9 | a = nil 10 | n = nil 11 | f() 12 | end 13 | 14 | local function chk(m, a, f, t) 15 | i = i + 1 16 | local s = wifi.sta.status() 17 | if i >= 30 then s = 42 end 18 | local e = {[2]="Wrong password",[3]="No wireless network found",[4]="Connect fail",[42]="Connect timeout"} 19 | ls = e[s] 20 | e = nil 21 | if s == 5 or ls then 22 | t:unregister() 23 | if s ~= 5 and m == wifi.STATION then 24 | wifi.setmode(wifi.STATIONAP) 25 | setAP(a, f) 26 | else 27 | f() 28 | end 29 | end 30 | end 31 | 32 | return function(f) 33 | package.loaded[z] = nil 34 | z = nil 35 | local c = require("cfgFile")() 36 | local m = c.sta and wifi.STATION or wifi.STATIONAP 37 | local a = {} 38 | a.ssid = c.ssid or "esp-devlab-setup" 39 | a.pwd = c.pwd or "We1c0me!" 40 | c = nil 41 | wifi.setmode(m) 42 | wifi.sta.autoconnect(1) 43 | if m == wifi.STATION then 44 | tmr.create():alarm(1000, 1, function(t) chk(m, a, f, t) end) 45 | else 46 | setAP(a, f) 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /src/request.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | return function(q) 4 | package.loaded[z] = nil 5 | z = nil 6 | local u = cf[1] and (cf[2] and 9 or 2) or 1 7 | local s1, st = q:find("\r\n\r\n") 8 | local h = s1 and q:sub(1, s1 - 1) or q 9 | local g = {} 10 | if st and (st + 1) < #q then 11 | g["file"] = q:sub(st + 1) 12 | end 13 | q = nil 14 | collectgarbage() 15 | if u > 1 then 16 | local ab = h:match("Authorization: Basic ([A-Za-z0-9+/=]+)") 17 | if ab then 18 | if ab == cf[1] then u = 1 elseif ab == cf[2] then u = 2 end 19 | end 20 | end 21 | if u > 2 then 22 | return nil, nil, u, nil 23 | end 24 | local _, _, m, p, vs = h:find("([A-Z]+) (.+)?(.+) HTTP") 25 | if not m then 26 | _, _, m, p = h:find("([A-Z]+) (.+) HTTP") 27 | end 28 | h = nil 29 | if not m then return nil, nil, 10, nil end 30 | if not p or p == "/" then 31 | p = "index.htm" 32 | else 33 | p = p:sub(2, -1) 34 | end 35 | if vs then 36 | for k, v in vs:gmatch("(%w+)=([^&]+)") do 37 | g[k] = v:gsub("+", " ") 38 | end 39 | vs = nil 40 | end 41 | local _, _, _, e = p:find("(.*)%.(%w*)%c*$") 42 | if not e and m == "GET" then e = "" end 43 | m = nil 44 | h = nil 45 | collectgarbage() 46 | return p, e, u, g 47 | end 48 | -------------------------------------------------------------------------------- /src/cfgSta.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | return function(co,p,u) 4 | package.loaded[z] = nil 5 | z = nil 6 | p = nil 7 | if u > 1 then 8 | require("rs")(co, 401) 9 | return 10 | end 11 | local st = true 12 | local a = nil 13 | co:on("sent", function(c) 14 | if a then 15 | if #a > 1024 then 16 | c:send(a:sub(1, 1024)) 17 | a = a:sub(1025) 18 | else 19 | c:send(a) 20 | a = nil 21 | end 22 | elseif st then 23 | st = false 24 | local s = {} 25 | s.ls = ls or "" 26 | if wifi.sta.getconfig then 27 | local s1 = wifi.sta.getconfig() 28 | if s1 then 29 | s.ls = s1..", "..s.ls 30 | end 31 | end 32 | s.ip, s.nm, s.gw = wifi.sta.getip() 33 | s.aip, s.anm, s.agw = wifi.ap.getip() 34 | s.als = wifi.ap.getmac() 35 | if wifi.ap.getconfig then 36 | local s1 = wifi.ap.getconfig() 37 | if s1 then 38 | s.als = s1..", "..s.als 39 | end 40 | end 41 | c:send(',"stat":'..cjson.encode(s)..'}') 42 | s = nil 43 | else 44 | c:close() 45 | c = nil 46 | end 47 | collectgarbage() 48 | end) 49 | wifi.sta.getap(function (b) 50 | a = cjson.encode(b) 51 | b = nil 52 | require("rs")(co, 200, '{"aps":', "application/json") 53 | end) 54 | end 55 | -------------------------------------------------------------------------------- /src/index.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | IoT 11 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
ControlConfigurePortsUARTIDE

32 | 33 | 34 | -------------------------------------------------------------------------------- /src/cfgFile.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | local function getCf() 4 | local c = {} 5 | local f = file.open("esp.cfg") 6 | if f then 7 | c = cjson.decode(f:read()) 8 | cf[1] = c.aPwd 9 | cf[2] = c.uPwd 10 | f:close(); f = nil 11 | end 12 | return c 13 | end 14 | 15 | local function setCf(c) 16 | local f = file.open("esp.cfg", "w+") 17 | if f then 18 | f:write(cjson.encode(c)) 19 | f:flush() 20 | f:close(); f = nil 21 | end 22 | end 23 | 24 | return function(p) 25 | package.loaded[z] = nil 26 | z = nil 27 | local c = getCf() 28 | if not p then return c, "", 200 end 29 | local d = {} 30 | if p.file then d = cjson.decode(p.file) end 31 | local m = p.m 32 | p = nil 33 | collectgarbage() 34 | if d.old == "" then d.old = nil end 35 | if d.pwd == "" then d.pwd = nil end 36 | local r = "" 37 | if m == "ap" then 38 | if d.pwd and #d.pwd < 8 then 39 | return c, "Password too short", 403 40 | end 41 | c.sta = d.opt 42 | c.ssid = d.ssid 43 | c.pwd = d.pwd 44 | tmr.create():alarm(2000, 0, function() node.restart() end) 45 | r = "Setting AP: "..(c.ssid or "").." ..." 46 | elseif m == "admin" then 47 | if d.old ~= c.aPwd then 48 | return c, "Old password wrong", 403 49 | end 50 | c.aPwd = d.pwd 51 | cf[1] = c.aPwd 52 | r = "Password changed for admin" 53 | elseif m == "user" then 54 | c.uPwd = d.pwd 55 | cf[2] = c.uPwd 56 | r = "Password changed for user" 57 | else 58 | return c, "Unknown cfg", 403 59 | end 60 | d = nil 61 | m = nil 62 | collectgarbage() 63 | setCf(c) 64 | return c, r, 200 65 | end 66 | -------------------------------------------------------------------------------- /src/uart.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 14 | UART 15 | 42 | 43 | 44 |

Send command via UART:

45 | 46 |

47 | 48 | 49 |
IDTimeCommandOutputHeap
50 | 58 | 59 | -------------------------------------------------------------------------------- /src/fSaveI.lua: -------------------------------------------------------------------------------- 1 | local z = ... 2 | 3 | return function(v, fc) 4 | package.loaded[z] = nil 5 | z = nil 6 | local n = v.name 7 | local p = tonumber(v.pos) 8 | if not p then p = 0 end 9 | local f = v.file 10 | local fl = tonumber(v.flush) 11 | if fl == 0 then fl = nil end 12 | v = nil 13 | collectgarbage() 14 | local n1 = "~"..n 15 | local n2 = "-"..n1 16 | local f2 = file.open(n2, "a") 17 | if not f2 then 18 | fc("Can't open file: "..n2) 19 | return 20 | end 21 | if p ~= nil and p > 0 then 22 | local cur = f2:seek("end") 23 | if cur ~= p then 24 | f2:close(); f2 = nil 25 | fc("File seek error, pos: "..p..", cur: "..(cur or "nil")) 26 | return 27 | end 28 | end 29 | local function fc2() 30 | n1 = nil 31 | n2 = nil 32 | n = nil 33 | f = nil 34 | collectgarbage() 35 | fc(nil) 36 | end 37 | local function fc1() 38 | f2:close(); f2 = nil 39 | if fl then 40 | tmr.create():alarm(10, 0, function() 41 | if file.exists(n) then 42 | file.remove(n1) 43 | file.rename(n, n1) 44 | end 45 | if not file.rename(n2, n) then 46 | if not file.rename(n2, n) then 47 | file.rename(n1, n) 48 | fc("File rename error") 49 | return 50 | end 51 | end 52 | file.remove(n1) 53 | fc2() 54 | end) 55 | else 56 | fc2() 57 | end 58 | end 59 | if not f2:write(f) then 60 | tmr.create():alarm(10, 0, function() 61 | local cur = f2:seek("set", p) 62 | if cur ~= p or not f2:write(f) then 63 | f2:close(); f2 = nil 64 | fc("File write error, pos: "..p) 65 | else 66 | fc1() 67 | end 68 | end) 69 | else 70 | fc1() 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /src/cfg.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 16 | 17 | Configure 18 | 132 | 133 | 134 |
135 |

Connect to wireless network:

136 | 137 | 138 | 142 | 143 | 144 | 145 | 146 | 147 |
Name: 139 |
Password:
148 |
149 |
150 | 158 | 159 | -------------------------------------------------------------------------------- /src/ide.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 11 | IDE 12 | 194 | 195 | 196 | 197 |

198 |
199 | File: 202 | 203 | 204 | 205 | 206 | 207 |

208 |
209 | 210 | 218 | 219 | -------------------------------------------------------------------------------- /src/ju.js: -------------------------------------------------------------------------------- 1 | function StringBuffer() { 2 | var r = []; 3 | 4 | function p(v) { 5 | if(v) r.push(v); 6 | } 7 | 8 | function pp(v, len, ch) { 9 | if(!ch) ch = '0'; 10 | v = (v ? v.toString() : ""); 11 | if(v.length < len) { 12 | for(var i = v.length; i < len; ++i) p(ch); 13 | } 14 | p(v); 15 | } 16 | 17 | function p1(v, l, s) { 18 | pp(v, l); 19 | p(s); 20 | } 21 | 22 | return { 23 | toString : function() { 24 | return r.join(''); 25 | }, 26 | push : p, 27 | pushPadded : pp, 28 | pushTimestamp : function() { 29 | var d = new Date(); 30 | p1(d.getDate(), 2, '.'); 31 | p1(d.getMonth() + 1, 2, '.'); 32 | p1(d.getFullYear(), 4, ' '); 33 | p1(d.getHours(), 2, ':'); 34 | p1(d.getMinutes(), 2, ':'); 35 | p1(d.getSeconds(), 2, ':'); 36 | p1(d.getMilliseconds(), 3); 37 | } 38 | }; 39 | }; 40 | 41 | function getTimestamp() { 42 | var b = StringBuffer(); 43 | b.pushTimestamp(); 44 | return b.toString(); 45 | } 46 | 47 | function empty(s) { 48 | return (!s || s.length == 0); 49 | } 50 | 51 | function fixRN(s) { 52 | return s ? s.replace( /\r?\n/g, "\n" ) : s; 53 | } 54 | 55 | function createXmlHttp(handler) { 56 | var x = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); 57 | x.onreadystatechange = function() { 58 | if(x.readyState == 4) { 59 | handler(x.responseText, x.status); 60 | } 61 | } 62 | return x; 63 | } 64 | 65 | function http(u, b, f) { 66 | if(empty(u) || !f) return false; 67 | b = fixRN(b); 68 | var q = createXmlHttp(function(t, s) { 69 | if(s == 200) { 70 | f(t, s); 71 | } else { 72 | f("" + s + ": " + t, s); 73 | } 74 | }); 75 | q.open(b != null ? "POST" : "GET", u, true); 76 | try { 77 | q.send(b); 78 | } catch(e) { 79 | f(e, -1); 80 | } 81 | } 82 | 83 | function upload(n, b, f) { 84 | if(empty(n) || empty(b) || !f) return false; 85 | b = fixRN(b); 86 | var p = 0; 87 | var CHS = 512; 88 | function sCh() { 89 | var q = "/fSave?name=" + uri(n) + "&pos=" + p; 90 | var d = CHS; 91 | if((p + d) >= b.length) q += "&flush=1"; 92 | return http(q, b.substr(p, d), function(re, s) { 93 | if(s == 200) { 94 | p += d; 95 | (p < b.length) ? sCh() : f(true); 96 | } else { 97 | confirm("Error when uploading " + n + ": " + re + ". Try again?") ? sCh() : f(false); 98 | } 99 | }); 100 | }; 101 | sCh(); 102 | } 103 | 104 | dc = document; 105 | function hT(v,t,a) {return "<"+ t + (a ? " " + a : "") + (v ? ">" + v + "" : "/>");} 106 | function hO(v) {return hT(v, "option") + "\n";} 107 | function hArg(n,v) {return ((v != null) ? ' ' + n + '="' + v + '"' : '');} 108 | function hB(v) {return hT(v, "b");} 109 | function hTd(v, a) {return hT(v, "td", a);} 110 | function hTr(v, a) {return hT(v, "tr", a) + "\n";} 111 | function hInput(t,i,n,v,a){return hT(null, 'input', hArg('type', t) + hArg('id', i) + hArg('name', n) + hArg('value', v) + (a?" " + a:""));} 112 | function hRadio(l,v,i,n,s,c) {return hInput('radio', i, n, v, hArg('onclick', c) + hArg('checked', (s?"checked":null))) + hT(l, "label", 'for="' + i + '"');} 113 | function hInputH(i,n,v,a) {return hInput("hidden",i,n,v,a);} 114 | function hInputB(i,n,v,a) {return hInput("button",i,n,v,a);} 115 | function hInputC(i,n,v,a) {return hInput("checkbox",i,n,v,a);} 116 | function hInputT(i,n,v,a) {return hInput("text",i,n,v,a);} 117 | function hInputP(i,n,v,a) {return hInput("password",i,n,v,a);} 118 | function hButton(t,l,n,v,a) {return hT(l, 'button', hArg('type', t) + hArg('value', v) + hArg('name', n) + (a?" " + a:""));} 119 | function hButtonS(l,n,v) {return hButton('submit',l,n,v);} 120 | function hFRow(l,c,h) {return hTr(hTd(hB(l)) + hTd(c) + (h?hTd(h):''));} 121 | function ce(c, t) { 122 | var e = dc.createElement(t ? t : 'div'); 123 | if(c) e.className = c; 124 | return e; 125 | } 126 | function ge(id) {return dc.getElementById(id);} 127 | function gt(o, tag) {return o.getElementsByTagName(tag);} 128 | function euri(v) {return encodeURIComponent(v);} 129 | function duri(v) {return decodeURIComponent(v);} 130 | function uri(v) {return euri(v).replace(/%20/g,'+');} 131 | function escp(v) {return v.replace(/\\/g,'\\\\').replace(/\"/g,'\\\"');} 132 | function sh(e,v) {e.innerHTML = v;} 133 | function gv(e) {return e.value;} 134 | function sv(e,v) {e.value = v;} 135 | function ric(r) { 136 | var i = 0; 137 | return function() {return r.insertCell(i++);}; 138 | } 139 | 140 | function getFormValue(e) { 141 | var t = null; 142 | if(e.length != null) t = e[0].type; 143 | if(!t) t = e.type; 144 | if(!t) return null; 145 | 146 | switch(t) { 147 | case 'radio': 148 | for(var i = 0; i < e.length; ++i) { 149 | if(e[i].checked) return gv(e[i]); 150 | } 151 | return null; 152 | case 'select-multiple': 153 | var r = []; 154 | for(var i = 0; i < e.length; ++i) { 155 | if(e[i].selected) r.push(gv(e[i])); 156 | } 157 | return r.join(','); 158 | case 'checkbox': 159 | return e.checked; 160 | default: 161 | return gv(e); 162 | } 163 | } 164 | 165 | function getEncodeType(n) { 166 | if(n == 'l' || n == 'f') return 2; 167 | if(n == 'p' || n == 'i' || n == 'v' || n == 'u') return 0; 168 | return 1; 169 | } 170 | 171 | function getForm(f, m, eq) { 172 | var r = []; 173 | var n = false; 174 | function p(v) {r.push(v);} 175 | p('{'); 176 | for(var i = 0; i < f.elements.length; ++i) { 177 | var e = f.elements[i]; 178 | if(!e.name) continue; 179 | var v = getFormValue(e); 180 | if(m) v = m(f, e.name, v); 181 | if(!v) continue; 182 | if(n) p(','); 183 | p('"' + e.name + '"' + eq); 184 | if(v === true) { 185 | p(1); 186 | } else if(v === false) { 187 | p(0); 188 | } else if(typeof v.replace === 'undefined') { 189 | p(v); 190 | } else { 191 | var t = getEncodeType(e.name); 192 | if(t > 0) { 193 | p('"' + (t > 1 ? euri(v) : escp(v)) + '"'); 194 | } else { 195 | p(v); 196 | } 197 | } 198 | n = true; 199 | } 200 | p('}'); 201 | return r.join(""); 202 | } 203 | 204 | function getFormJson(f, m) { 205 | return getForm(f, m, ":"); 206 | } 207 | 208 | function postForm(f, u, v, vv, r, h, m) { 209 | if(v) { 210 | if(!v(f, vv)) return false; 211 | } 212 | var d = getFormJson(f, m); 213 | http(u, d, function(re, s) { 214 | if(!empty(r)) { 215 | sh(ge(r), re); 216 | } 217 | if(h) h(re, s); 218 | }); 219 | return false; 220 | } 221 | -------------------------------------------------------------------------------- /src/cfgPort.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | Configure Ports 17 | 190 | 191 | 192 | 193 |

Loading...

194 | 195 | 196 | 202 | 203 |
New Control:
204 | 205 |
206 | 207 |
208 | 216 | 217 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266 NodeMCU IoT Generic Switch 2.0.0 2 | A front-end NodeMCU HTTP server for operating a back-end controller through UART. An example of back-end controller is an Arduino board flashed with [**Arduino GenericPin sketch**](https://github.com/dev-lab/arduino-generic-pin). 3 | 4 | The software allows both to set-up and to control ESP8266 device through Web Interface. 5 | 6 | ## Features 7 | * Operate ports of a secondary controller (connected via UART to ESP8266) through Web Interface; 8 | * HTTP Server with optional authentication for 2 users: `admin` and `user` (configurable through Web interface); 9 | * Wi-Fi configurable through Web interface (both connecting to existing Wi-Fi network and creating a new Wi-Fi Access Point (AP) with the option of disabling AP on successfull connectin to Wi-Fi network); 10 | * In AP mode all the DNS queries are resolved to ESP8266 IP address; 11 | * Configurable through the Web interface **Ports View**; 12 | * Allows to send raw UART commands through Web interface; 13 | * Built-in IDE for editing the software through Web interface; 14 | * Responsive Web UI (usable from smartphones); 15 | * NodeMCU version agnostic (polyfills allow to work on on NodeMCU 1.5.4.1 - 3.0.0). 16 | 17 | ### Web UI Animations: 18 | 19 | #### UI overview: 20 | [![Web UI overview (animation)](https://github.com/dev-lab/blob/blob/master/iot-power-strip/generic-switch-web-ui-overview-pic.png)](https://github.com/dev-lab/blob/blob/master/iot-power-strip/generic-switch-web-ui-overview.gif) 21 | 22 | #### UI overview with port configuration: 23 | [![Web UI overview with port config (animation)](https://github.com/dev-lab/blob/blob/master/iot-power-strip/generic-switch-web-ui-overview-with-config-port-pic.png)](https://github.com/dev-lab/blob/blob/master/iot-power-strip/generic-switch-web-ui-overview-with-config-port.gif) 24 | 25 | #### Setup Wi-Fi access point: 26 | [![Setup Wi-Fi access point (animation)](https://github.com/dev-lab/blob/blob/master/iot-power-strip/generic-switch-web-ui-config-wifi-ap-pic.png)](https://github.com/dev-lab/blob/blob/master/iot-power-strip/generic-switch-web-ui-config-wifi-ap.gif) 27 | 28 | #### Setup `admin` authentication: 29 | [![Setup admin authentication (animation)](https://github.com/dev-lab/blob/blob/master/iot-power-strip/generic-switch-web-ui-config-admin-auth-pic.png)](https://github.com/dev-lab/blob/blob/master/iot-power-strip/generic-switch-web-ui-config-admin-auth.gif) 30 | 31 | ## Usage 32 | 1. Flash the ESP8266 module with the NodeMCU firmware as described in [Flashing the firmware](https://nodemcu.readthedocs.io/en/release/flash/) with any tool you like (e.g. [esptool.py](https://github.com/espressif/esptool), [NodeMCU Flasher](https://github.com/nodemcu/nodemcu-flasher) etc.). The custom NodeMCU firmware can be built online with the service: [NodeMCU custom builds](https://nodemcu-build.com/). If your ESP8266 EEPROM is only 512Kb, you have to take extra steps to be sure that firmware is as small as possible, and there is enough space for project files in EEPROM file system. So, build your custom firmware based on 1.5.4.1-final NodeMCU with the only 7 modules selected: `cjson`, `file`, `net`, `node`, `tmr`, `uart`, `wifi`, no TLS, no debug, and take the integer version of it. The firmware I using with the options above is 393892 bytes, which gave me about 78 Kb of space available in my ESP-01 EEPROM file system. In the case if your ESP module goes with larger EEPROM installed, you can build even the most recent NodeMCU version (select the `release` option on online build tool) with more modules selected, just make sure that at least 70Kb of file system space is available when you run NodeMCU. Make sure the following 7 modules are selected when you are building the firmware: 33 | * `file`; 34 | * `net`; 35 | * `node`; 36 | * `SJSON` (for new NodeMCU; if you build older NodeMCU (1.5.4.1) select `cJSON` instead); 37 | * `timer`; 38 | * `UART`; 39 | * `WiFi`. 40 | 41 | [![Flashing ESP8266 with custom NodeMCU](https://github.com/dev-lab/blob/blob/master/iot-power-strip/flash-nodemcu-pic.png)](https://github.com/dev-lab/blob/blob/master/iot-power-strip/flash-nodemcu.gif) 42 | 43 | 2. Upload the project files (all the files from [src/](src/) directory) to the ESP module (read the [Uploading code](https://nodemcu.readthedocs.io/en/release/upload/) how to do it). If you prefer, you can use the tool for uploading NodeMCU files from https://github.com/dev-lab/esp-nodemcu-lua-uploader. Upload the software with either [install.sh](install.sh), or [install.bat](install.bat) depending on your OS: 44 | 45 | [![Uploading IoT Generic Switch software into ESP8266](https://github.com/dev-lab/blob/blob/master/iot-power-strip/upload-soft-pic.png)](https://github.com/dev-lab/blob/blob/master/iot-power-strip/upload-soft.gif) 46 | 47 | 3. Restart the ESP8266 module (turn it off and turn back on). After restarting you will see a new Wi-Fi access point with the name: `esp-devlab-setup`. You will be able to connect to the module with the default password: `We1c0me!`. The default Wi-Fi AP name and password are specified in file: [`connect.lua`](src/connect.lua). 48 | 4. On successfull connection to the `esp-devlab-setup` Access Point you will be able to reach the Web UI through the browser by typing anything looking like domain name as an URL, e.g.: `any.site.my`. You can do that because the software starts a DNS liar server (it responds with the ESP8266 IP to any DNS request) in AP mode. The DNS server evolved based on the work published on the following resources: 49 | * [Re: Tiny DNS server written in Ncat-Lua - anyone interested?](http://seclists.org/nmap-dev/2013/q3/196) 50 | * https://svn.nmap.org/!svn/bc/31535/nmap-exp/d33tah/lua-exec-examples/ncat/scripts/dns.lua 51 | * [ESP8266 WiFi Throwies](http://hackaday.com/2015/05/03/esp8266-wifi-throwies/) 52 | * [Wifi throwie : improved version - faster, smaller, cheaper](http://iotests.blogspot.fr/2015/10/wifi-throwie-improved-version-faster.html) 53 | 5. Web UI shall be quite self-explaining to use. You only have to remember that the best way to brick the software is to use Web IDE without checking twice what you are uploading to the file system. The changes are taken into account immediately. A bricked NodeMCU can be cured only with connecting of ESP module to computer through UART, formatting of NodeMCU file system, and rewriting the Lua software. In some cases you even have to re-flash the NodeMCU (e.g. if you did the mistake and removed the delay in [`init.lua`](src/init.lua). 54 | 55 | ## The Use Case: 56 | [IoT Power Strip](http://www.thingiverse.com/thing:1211810) 57 | 58 | ## [License](LICENSE) 59 | Copyright (c) 2015-2021 Taras Greben 60 | 61 | Licensed under the [Apache License](LICENSE). 62 | -------------------------------------------------------------------------------- /src/ctl.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 26 | Control 27 | 273 | 274 | 275 |

276 |
277 | 285 | 286 | --------------------------------------------------------------------------------