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

[SOLVED] Why Wont My Code Work?

Started by elfin8er, 06 April 2013 - 06:55 AM
elfin8er #1
Posted 06 April 2013 - 08:55 AM
So what I'm trying to do here is kind of complicated. What I'm pretty much trying to do, is be able to control computers with my cell phone. I have all of the back end php done, I just have to get the computercraft part working. Here's the code I have so far.


while true do
command = http.get("http://24.152.212.26:8080/tropo/command.txt")


if (command.readAll() == "lights_on") then
rs.setOutput("back", true)
http.get("http://24.152.212.26:8080/tropo/nullcommand.php")
end

if (command.readAll() == "lights_off") then
rs.setOutput("back", false)
http.get("http://24.152.212.26:8080/tropo/nullcommand.php")
end
end

So pretty much all that's going on here, is the computer is looking at a file called command.txt. The php that I have will change the contents of that file depending on what I tell it to do via my cell phone. Currently that file would be set to either null, lights_on, or lights_off. If I choose to turn the lights on, the command.txt file will be changed to lights_on. Then the redstone output in the back will be set to true. nullcommand.php just sets command.txt to null.

Everything works great, except lights_off. Lights_on works like a charm, but I'm not sure why lights_off wont work. If I switch lights_on and lights_off, lights_off works, but lights_on does not.

Anyone have any suggestions?
LBPHacker #2
Posted 06 April 2013 - 08:58 AM
Give us PHP :D/>
PixelToast #3
Posted 06 April 2013 - 09:02 AM
we need to see the php's source, the lua file looks fine
LBPHacker #4
Posted 06 April 2013 - 09:04 AM
we need to see the php's source, the lua file looks fine

Except he doesn't close the HTTP handle - not sure if that's what causes the problem though.
Sammich Lord #5
Posted 06 April 2013 - 09:06 AM
we need to see the php's source, the lua file looks fine

Except he doesn't close the HTTP handle - not sure if that's what causes the problem though.
You don't have to close the HTTP handle. It is not a real file handle. However, it is good practice to.

But we need to see the PHP to be able to help.
Also, you shouldn't constantly spam HTTP.
remiX #6
Posted 06 April 2013 - 09:22 AM
I'll take a bet that a problem you're having is that it never reads that it's lights_off when it is?
This is because once you readAll the first time, there is nothing to read anymore so it will be nil. (Didn't even read OP until after I said this :P/>)

while true do
	local command = http.get("http://24.152.212.26:8080/tropo/command.txt")
	local status = command.readAll()
	print( status ) -- debug
	if status == "lights_on" then
		rs.setOutput("back", true)
	elseif status == "lights_off" then -- elseif statement ! :)/>
		rs.setOutput("back", false)
	end
	
	--[[ You simply, you could do this:
	rs.setOutput( "back", status == "lights_on" )
	-]]
end

Always have a debug line which prints something so you know what is wrong.

If you are using a free webhoster, their might be extra code at the end of the page that readAll() will catch, you will need to use string.gsub to remove it.
elfin8er #7
Posted 06 April 2013 - 09:30 AM
I'll take a bet that a problem you're having is that it never reads that it's lights_off when it is?
This is because once you readAll the first time, there is nothing to read anymore so it will be nil. (Didn't even read OP until after I said this :P/>)

while true do
	local command = http.get("http://24.152.212.26:8080/tropo/command.txt")
	local status = command.readAll()
	print( status ) -- debug
	if status == "lights_on" then
		rs.setOutput("back", true)
	elseif status == "lights_off" then -- elseif statement ! :)/>/>
		rs.setOutput("back", false)
	end
	
	--[[ You simply, you could do this:
	rs.setOutput( "back", status == "lights_on" )
	-]]
end

Always have a debug line which prints something so you know what is wrong.

If you are using a free webhoster, their might be extra code at the end of the page that readAll() will catch, you will need to use string.gsub to remove it.
Worked wonderfully! Thank you so much!