This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Left4Cake's profile picture

Web Based Remote, Controll your Computers and Turtles from a browser.

Started by Left4Cake, 08 January 2013 - 03:25 PM
Left4Cake #1
Posted 08 January 2013 - 04:25 PM
Requires that you have access to a PHP web server

This programs allow you to use a web interface to interact with you computer or turtle. This is just a very basic programe where the server sent the computer/turtle a program and the computer/turtle (if the program supports it) will send back status updates of what it is doing.


Download server code here: http://www.lozengia....mote_server.rar
Interface on Default web host: http://www.lozengia..../test/input.php
Note about Default host:(nothing will happen in the log unless at least one person out there is running the program as defult.)

Computer/turtal code
(change dir to the directory on the server you uploaded the server code too. (unless you want my public example controlling you stuff.)


local function sendOutput( text )
local response = http.get(output .. "?output=" .. textutils.urlEncode(text))
print("Connecting to http for output.")
if response then
-- if page is reseved
if response then
print("Sending output.")
local sResponse = response.readAll()
response.close()
end
end
end

dir = "http://www.lozengia.com/tmas/Downloads/Minecraft/test/"
input = dir .. "input.txt"
output = dir .. "getOutput.php"
while true do
print("Connecting to http")
	local response = http.get(input)
	 if response then
	 print("Page found attepting to download.")
-- if page is reseved
while response do
local sResponse = response.readAll()
		response.close()
		cache = shell.resolve( "temp" )
		local file = fs.open( cache, "w" )
		file.write( sResponse )
		file.close()
		response = nil
		print("Running input from http.")
		shell.run( "temp" )
end
   text = "Finished Running Code."
   sendOutput("Finished Running Code.")
else
   print("Page not found.")
end
print("Yileding befor loop.")
os.sleep( 5 )
shell.run( "clear" )
end


To Do
Spoiler-Add buttons for easier basic movement control (In Progress)
-Fix log so it auto refreshes.
-Add server ip and computer/turtle id to the output it sends to the server.
-Add some form of arguments.

Considering but not sure about.
-Add all default turtle programs. (In Progress?)

old versions

V 0.2 server

V 0.1
SpoilerAs of right now the way it works is you set dir to the url of the directory you place the files in. The computer/turtle will check the directory for an file named 'input' and then download it and run it. It could be a full program or just shell.run( "program name here"). Then it sent a get string to getOutput.php witch then saves $_GET['output'] to a file with a time stamp and then delete the input file to prevent the program from just running it in a loop. This process starts over.

The idea is that to sent command all you have to do is upload a file with the command

You have to change dir to a file on a server you have access to.

dir = "your url here"
input = dir .. "input"
output = dir .. "getOutput.php"
while true do
print("Connecting to http")
	local response = http.get(input)
	 if response then
	 print("Page found attepting to download.")
-- if page is reseved
  while response do
   local sResponse = response.readAll()
		   response.close()
		   cache = shell.resolve( "temp" )
		   local file = fs.open( cache, "w" )
		   file.write( sResponse )
		   file.close()
		   response = nil
		   print("Running input from http.")
		   shell.run( "temp" )
  end

	 local response = http.get(output .. "?output=" .. textutils.urlEncode("Finished Running Code."))
	 print("Connecting to http for output.")
	  if response then
  -- if page is reseved
   if response then
	print("Sending output.")
	local sResponse = response.readAll()
		   response.close()
   end
  end
else
	 print("Page not found.")
end
print("Yileding befor loop.")
os.sleep( 5 )
shell.run( "clear" )
end

getOutput.php for the server.

<?
$filename = "log.txt";
if (isset($_GET['output'])){
$theOutput =  urlEncode(date("Y-m-d")) . ' ' . date("H:i:s", time()) . ' ' . $_GET['output'] . '
';
$handle = fopen($filename, 'a');
if(!$handle){
  echo "Error, Could not open log file!!!";
} else if(!fwrite($handle,stripslashes($theOutput))) {
  echo "Error, Could not save log to file!!!";
} else {
  fclose($handle);
  unlink('input.txt');
}
}
?>



Back up all 'input' files if you don't want to type it out aging.
*only tested on single-player, use caution early beta.
Left4Cake #2
Posted 09 January 2013 - 05:29 AM
Updated to have a web based interface. Plan on making it better as time goes on.
Zudo #3
Posted 09 January 2013 - 07:07 AM
I started developing something like this but stopped after a serious problem with backslashes.
Left4Cake #4
Posted 09 January 2013 - 07:18 AM
Well that is
I started developing something like this but stopped after a serious problem with backslashes.

Now that is very little info but I am assuming your problem is that backslashes are used to escape strings in PHP, just like the percent sign is to lua.
pielover88888 #5
Posted 25 January 2013 - 05:38 AM
Um..
Can you give me EXACT instructions on how to make this work?
Left4Cake #6
Posted 25 January 2013 - 07:04 AM
Well you will need a http server with PHP to upload the file in server.rar. You can either pay for one or install one (like xammp) on to your computer and port forward if you want to be able to accese over the web.

Server
1. Extract server.rar
2. Upload/Copy the server file in it own directory on the sever. (take note of where you put it)
3. Now you should be able to open it in your browser. (http://domain-or-ip/whereeveryouputit/input.php)

Client for Computer or Turtal

1. Copy the program to Computer/Turtal.
2. Change dir to the url of the directory you put the server files in.
3. Run.

Let me know if it need any more detailes, as the exactness of how to excute these step can vary on a few thing. (Like weather you are hosting the server yourself or are paying for one.