webcommands for robots using nodejs server
parent
fd9f659392
commit
5f8659dbb2
|
@ -0,0 +1,31 @@
|
|||
// listen to web request and pass it to minetest, rnd 2018
|
||||
|
||||
const http = require('http');
|
||||
|
||||
const hostname = '192.168.0.10' //write address of your router (it will be accessible from internet then if you open firewall for nodejs process)
|
||||
const port = 80;
|
||||
|
||||
var webreq = ""
|
||||
|
||||
// take request from web and pass it to minetest
|
||||
const server = http.createServer((req, res) => {
|
||||
res.statusCode = 200;
|
||||
res.setHeader('Content-Type', 'text/plain');
|
||||
|
||||
if (req.url == '/favicon.ico') return // prevent passing this as request
|
||||
|
||||
var pos = (req.url).indexOf("/MT");
|
||||
if (pos >=0) { // did request come from minetest? then answer with latest request
|
||||
res.write(webreq);webreq = "";res.end();return
|
||||
}
|
||||
|
||||
//process web request and store it
|
||||
var ip = req.connection.remoteAddress;
|
||||
webreq = ip + ' ' + req.url
|
||||
res.write('request received: ' + webreq);res.end(); // acknowledge request
|
||||
return
|
||||
});
|
||||
|
||||
server.listen(port, hostname, () => {
|
||||
console.log(`Server running at http://${hostname}:${port}/`);
|
||||
});
|
|
@ -0,0 +1,31 @@
|
|||
-- webcommands : access url like: 192.168.0.10/hello_world
|
||||
|
||||
--[[ instructions:
|
||||
1.download nodejs server from
|
||||
https://nodejs.org/dist/v10.14.2/node-v10.14.2-win-x86.zip
|
||||
2. run nodejs server using run.bat
|
||||
:loop
|
||||
node --inspect myprogs/minetest_webcommands.js
|
||||
goto loop
|
||||
3. run robot and type 'http://192.168.0.10:80/hello this is a test' into browser
|
||||
(you need to write your router address here, i.e. ip accessible from internet OR lan address)
|
||||
--]]
|
||||
|
||||
if not fetch then
|
||||
fetch = _G.basic_robot.http_api.fetch;
|
||||
state = 0 -- ready to fetch new command
|
||||
-- WARNING: this is run outside pcall and can crash server if errors!
|
||||
result = function(res) -- res.data is string containing result
|
||||
state = 0
|
||||
if not res.succeeded then self.label("#ERROR: data couldn't be downloaded :\n" .. minetest.serialize(res) ) return end
|
||||
if res.data == "" then return end
|
||||
local req = res.data; req = string.gsub(req,"%%20","S")
|
||||
if res.data then self.label(os.clock() .. ' received cmd : ' .. res.data) end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if state == 0 then
|
||||
fetch({url = "http://192.168.0.10/MT", timeout = 30}, result)
|
||||
state = 1
|
||||
end
|
Loading…
Reference in New Issue