Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Client for Mote - The Turtle Remote
- -- http://hbar.kapsi.fi/ccserver/mote/
- -- 2014 Matti Vapa
- -- Feel free to modify and redistribute
- -- this program. Attribution is appreciated,
- -- but not required.
- -- I take no responsibility on any
- -- problems this program might cause
- -- to anyone ever. Not that it will.
- if not http then
- print("HTTP required!")
- return
- end
- print("Checking fuel...")
- if turtle.getFuelLevel() == 0 then
- print("Refuel me!")
- return
- end
- print("Ok!")
- -- Query this address for commands.
- url = "http://hbar.kapsi.fi/ccserver/mote/get/"
- -- How long we sleep between queries.
- -- Please don't DoS the server.
- dt = 0.5
- -- Map the commands returned by the server to functions.
- -- These are all of the commands available at the time of
- -- writing. If you want more functions, you could use
- -- for example "menu" to change between sets of functions.
- -- Also note that this works with computers too, so you
- -- could use this to control your base doors and machines
- -- or anything. The session stays live as long as there
- -- was at least one command or query during the last hour
- -- (might change if there's need for it).
- cmds = {}
- cmds.arrowUp = turtle.forward
- cmds.arrowDown = turtle.back
- cmds.arrowLeft = turtle.turnLeft
- cmds.arrowRight = turtle.turnRight
- cmds.button1 = turtle.up
- cmds.button2 = turtle.dig
- cmds.button3 = turtle.place
- cmds.button4 = turtle.down
- cmds.menu = turtle.attack
- sID = {...}
- if #sID < 1 then
- print("Usage: mote <session ID>")
- print("Parameter session ID is the random numbers and letters")
- print("following /mote/ in the controller URL.")
- end
- sID = sID[1]
- print("Contacting server...")
- -- Make one query to determine that the service is online
- -- and remove any old commands from the session so we only
- -- listen to the ones issued after the connection.
- -- To do the query, send a POST request to the before
- -- mentioned URL with data "sessionId=XXXXXXXX", where
- -- XXXXXXXX is the session ID (duh).
- r = http.post(url, "sessionId="..sID)
- -- Problems?
- -- Untested code, might break if something is wrong.
- -- But then again something is wrong so it's the least
- -- of our worries...
- if not r then
- print("Error contacting server.")
- print("Check your session ID.")
- return
- elseif r.getResponseCode() ~= 200 then
- print("Server unsuccessful.")
- print("Code: "..tostring(r.getResponseCode()))
- print("Reason: "..r.readAll())
- return
- end
- -- Nah, we good.
- print("Connection established!")
- print("Listening for commands...")
- while true do
- -- Query for new command. Server returns "null" if
- -- there has been no new commands after the last query.
- r = http.post(url, "sessionId="..sID)
- if not r then
- print("Connection lost!")
- return
- else
- cmd = r.readAll()
- -- Uncomment this to see what the server sends.
- --print(cmd)
- -- If it is a recognized command, execute the
- -- corresponding function. Otherwise
- -- wait before trying again.
- if cmds[cmd] then
- cmds[cmd]()
- else
- sleep(dt)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement