--[[ XChip's NodeMCU IDE http://www.esp8266.com/viewtopic.php?f=19&t=1549 Updated by mmiscool Create, Edit and run NodeMCU files using your webbrowser. Examples: http:/// will list all the files in the MCU http:///newfile.lua displays the file on your browser http:///newfile.lua?edit allows to creates or edits the specified script in your browser http:///newfile.lua?run it will run the specified script and will show the returned value ]]-- --to make it go and runn task after running ide for 30 seconds noactivity = 1 srv=net.createServer(net.TCP) srv:listen(80,function(conn) local rnrn=0 local Status = 0 local DataToGet = 0 local method="" local url="" local vars="" conn:on("receive",function(conn,payload) if Status==0 then _, _, method, url, vars = string.find(payload, "([A-Z]+) /([^?]*)%??(.*) HTTP") print(method, url, vars) end if method=="POST" then if Status==0 then --print("status", Status) _,_,DataToGet, payload = string.find(payload, "Content%-Length: (%d+)(.+)") if DataToGet~=nil then DataToGet = tonumber(DataToGet) --print(DataToGet) rnrn=1 Status = 1 else print("bad length") end end -- find /r/n/r/n if Status==1 then --print("status", Status) local payloadlen = string.len(payload) local mark = "\r\n\r\n" local i for i=1, payloadlen do if string.byte(mark, rnrn) == string.byte(payload, i) then rnrn=rnrn+1 if rnrn==5 then payload = string.sub(payload, i+1,payloadlen) file.open(url, "w") file.close() Status=2 break end else rnrn=1 end end if Status==1 then return end end if Status==2 then --print("status", Status) if payload~=nil then DataToGet=DataToGet-string.len(payload) --print("DataToGet:", DataToGet, "payload len:", string.len(payload)) file.open(url, "a+") file.write(payload) file.close() else conn:send("HTTP/1.1 200 OK\r\n\r\nERROR") Status=0 end if DataToGet==0 then conn:send("HTTP/1.1 200 OK\r\n\r\nOK") Status=0 end end return end DataToGet = -1 if url == "favicon.ico" then conn:send("HTTP/1.1 404 file not found") return end conn:send("HTTP/1.1 200 OK\r\n\r\n") -- it wants a file in particular if url~="" and vars=="" then DataToGet = 0 return end conn:send("


") conn:send("
-Reset-") if vars=="edit" then conn:send("-Open---Run This Script On Boot---run-") conn:send("
") conn:send("
") end if vars=="run" then --local st, result=pcall(dofile, url) launchtask(url) end if vars=="RunOnBoot" then file.open("runOnBoot.txt","w") file.write(url) file.close() end if url=="" then noactivity = 0 conn:send("
") conn:send("") local l = file.list(); for k,v in pairs(l) do conn:send("") end conn:send("
File Name---------Size
") end if vars=="reset" then node.restart() end conn:send("") end) conn:on("sent",function(conn) if DataToGet>=0 and method=="GET" then if file.open(url, "r") then file.seek("set", DataToGet) local line=file.read(512) file.close() if line then conn:send(line) DataToGet = DataToGet + 512 if (string.len(line)==512) then return end end end end conn:close() end) end) function trylaunchtask() print("Checking to launch task") local task = "" if (nil ~=file.open("runOnBoot.txt", "r"))then task = file.readline() file.close() end if (noactivity == 1 and task ~= "") then launchtask(task) end if (noactivity == 0 ) then tmr.stop(0) end end function launchtask(task) --conn:close() srv:close() collectgarbage() dofile(task) end print("listening, free:", node.heap()) tmr.alarm(0, 30000, 1, trylaunchtask)