Advertisement
hbar

mote_old

Mar 26th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | None | 0 0
  1. -- Client for Mote - The Turtle Remote
  2. -- http://hbar.kapsi.fi/ccserver/mote/
  3. -- 2014 Matti Vapa
  4. -- Feel free to modify and redistribute
  5. -- this program. Attribution is appreciated,
  6. -- but not required.
  7.  
  8. -- I take no responsibility on any
  9. -- problems this program might cause
  10. -- to anyone ever. Not that it will.
  11.  
  12. if not http then
  13.   print("HTTP required!")
  14.   return
  15. end
  16.  
  17. print("Checking fuel...")
  18. if turtle.getFuelLevel() == 0 then
  19.   print("Refuel me!")
  20.   return
  21. end
  22. print("Ok!")
  23.  
  24. -- Query this address for commands.
  25. url = "http://hbar.kapsi.fi/ccserver/mote/get/"
  26. -- How long we sleep between queries.
  27. -- Please don't DoS the server.
  28. dt = 0.5
  29.  
  30. -- Map the commands returned by the server to functions.
  31. -- These are all of the commands available at the time of
  32. -- writing. If you want more functions, you could use
  33. -- for example "menu" to change between sets of functions.
  34. -- Also note that this works with computers too, so you
  35. -- could use this to control your base doors and machines
  36. -- or anything. The session stays live as long as there
  37. -- was at least one command or query during the last hour
  38. -- (might change if there's need for it).
  39. cmds = {}
  40. cmds.arrowUp = turtle.forward
  41. cmds.arrowDown = turtle.back
  42. cmds.arrowLeft = turtle.turnLeft
  43. cmds.arrowRight = turtle.turnRight
  44. cmds.button1 = turtle.up
  45. cmds.button2 = turtle.dig
  46. cmds.button3 = turtle.place
  47. cmds.button4 = turtle.down
  48. cmds.menu = turtle.attack
  49.  
  50. sID = {...}
  51. if #sID < 1 then
  52.   print("Usage: mote <session ID>")
  53.   print("Parameter session ID is the random numbers and letters")
  54.   print("following /mote/ in the controller URL.")
  55. end
  56. sID = sID[1]
  57.  
  58. print("Contacting server...")
  59. -- Make one query to determine that the service is online
  60. -- and remove any old commands from the session so we only
  61. -- listen to the ones issued after the connection.
  62. -- To do the query, send a POST request to the before
  63. -- mentioned URL with data "sessionId=XXXXXXXX", where
  64. -- XXXXXXXX is the session ID (duh).
  65. r = http.post(url, "sessionId="..sID)
  66.  
  67. -- Problems?
  68. -- Untested code, might break if something is wrong.
  69. -- But then again something is wrong so it's the least
  70. -- of our worries...
  71. if not r then
  72.   print("Error contacting server.")
  73.   print("Check your session ID.")
  74.   return
  75. elseif r.getResponseCode() ~= 200 then
  76.   print("Server unsuccessful.")
  77.   print("Code: "..tostring(r.getResponseCode()))
  78.   print("Reason: "..r.readAll())
  79.   return
  80. end
  81.  
  82. -- Nah, we good.
  83. print("Connection established!")
  84. print("Listening for commands...")
  85.  
  86.  
  87. while true do
  88.   -- Query for new command. Server returns "null" if
  89.   -- there has been no new commands after the last query.
  90.   r = http.post(url, "sessionId="..sID)
  91.   if not r then
  92.     print("Connection lost!")
  93.     return
  94.   else
  95.     cmd = r.readAll()
  96.     -- Uncomment this to see what the server sends.
  97.     --print(cmd)
  98.     -- If it is a recognized command, execute the
  99.     -- corresponding function. Otherwise
  100.     -- wait before trying again.
  101.     if cmds[cmd] then
  102.       cmds[cmd]()
  103.     else
  104.       sleep(dt)
  105.     end
  106.   end
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement