Jump to content




[SOLVED] Why Wont My Code Work?


  • You cannot reply to this topic
6 replies to this topic

#1 elfin8er

  • Members
  • 133 posts

Posted 06 April 2013 - 07: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?

#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 06 April 2013 - 07:58 AM

Give us PHP :D

#3 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 06 April 2013 - 08:02 AM

we need to see the php's source, the lua file looks fine

#4 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 06 April 2013 - 08:04 AM

View PostPixelToast, on 06 April 2013 - 08:02 AM, said:

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.

#5 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 06 April 2013 - 08:06 AM

View PostLBPHacker, on 06 April 2013 - 08:04 AM, said:

View PostPixelToast, on 06 April 2013 - 08:02 AM, said:

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.

#6 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 06 April 2013 - 08: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.

#7 elfin8er

  • Members
  • 133 posts

Posted 06 April 2013 - 08:30 AM

View PostremiX, on 06 April 2013 - 08:22 AM, said:

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!





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users