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

Rednet beyond me...

Started by Tassyr, 31 July 2015 - 11:56 AM
Tassyr #1
Posted 31 July 2015 - 01:56 PM
So I've come up with an idea to use wireless Rednet to control a massive gate/elevator mechanism in Tekkit. It's not overly complex, at least from where I'm standing- I'd make a program with four commands (Open, Close, Up, Down). The idea is that I hit the command on the transmitting unit, and it broadcasts it to the receiving unit, which then runs the required program. Thing is, I have no idea how to make rednet work. And it -has- to be wireless, sadly.

Anyone think they can point me to a tutorial I missed, or something?
RootSlayers #2
Posted 31 July 2015 - 03:11 PM
Hello !

You can read the CC Wiki here.

- On the transmitting unit, you need to use : "rednet.send(id, "message")".

For example your receiving unit has the ID #1 :

rednet.send(1, "Open")
rednet.send(1, "Close")
And so…


- On the receiving unit, you need to use : "rednet.receive()".

rednet.open(side)
while true do   -- Infinit loop to receive the message
	local senderId, message, protocol = rednet.receive()
  
	if message == "Open" then
		-- Do your stuff here.
	elseif message == "Close" then
		--
	elseif message == "Up" then
		--
	elseif message == "Down" then
		--
	end
end  
Tassyr #3
Posted 31 July 2015 - 03:55 PM
Hello !

You can read the CC Wiki here.

- On the transmitting unit, you need to use : "rednet.send(id, "message")".

For example your receiving unit has the ID #1 :

rednet.send(1, "Open")
rednet.send(1, "Close")
And so…


- On the receiving unit, you need to use : "rednet.receive()".

rednet.open(side)
while true do   -- Infinit loop to receive the message
	local senderId, message, protocol = rednet.receive()
  
	if message == "Open" then
		-- Do your stuff here.
	elseif message == "Close" then
		--
	elseif message == "Up" then
		--
	elseif message == "Down" then
		--
	end
end  

You are a god among programmers. Thank you! … I wonder if I could set up an email program with rednet. (Yes, I realise that is stupidly ambitious, but I have a week of vacation staring me in the face, so. xD)
Tassyr #4
Posted 31 July 2015 - 04:45 PM
Erm. Problem. I'm not sure I'm doing this right, 'cause nothing happens. Can you check over my stuff for me? The 'receiver' unit.


redstone.setOutput("back", false)
redstone.setOutput("left", false)
redstone.setOutput("right", false)
redstone.setOutput("top", false) -- clearing all the junk in case
shell.run("down")
shell.run("close") -- reset to base status
rednet.open("bottom")
while true do   -- Infinite loop to receive the message
	    local senderId, message, protocol = rednet.receive()

	    if message == "Open" then
					    shell.run("open")
					    shell.run("up")
	    elseif message == "Close" then
					    shell.run("down")
					    shell.run("close")
	    elseif message == "Reset" then
					    shell.run("clearall")
	    end
end

And the transmitter.


rednet.open("back")
while true do
	    term.clear()
	    term.setCursorPos(1,1)
	    print("Vault Lift Control (01)")
	    term.setCursorPos(1,2)
	    print("Enter Command:")
	    term.setCursorPos(1,3)
	    print("[O]pen, [C]lose, [R]eset")
	    term.setCursorPos(1,5)
	  
	    input = read()
	    if input == "O" then
			    rednet.send(2,Open)
	    elseif input == "o" then
			    rednet.send(2,Open)
	    elseif input == "Open" then
			    rednet.send(2,Open)
	    elseif input == "open" then
			    rednet.send(2,Open)
	    elseif input == "C" then
			    rednet.send(2,Close)
	    elseif input == "c" then
			    rednet.send(2,Close)
	    elseif input == "Close" then
			    rednet.send(2,Close)
	    elseif input == "close" then
			    rednet.send(2,Close)
	    elseif input == "R" then
			    rednet.send(2,Reset)
	    elseif input == "r" then
			    rednet.send(2,Reset)
	    elseif input == "Reset" then
			    rednet.send(2,Reset)
	    elseif input == "reset" then
			    rednet.send(2,Reset)
	    else
	    term.setCursorPos(1,6)
	    print("Syntax Error")
	    sleep(3)
	    end   
sleep(2)
end

What'd I break? I get no errors- just no response. The receiver is ID'd as #2.
KingofGamesYami #5
Posted 31 July 2015 - 05:01 PM
Rednet sends and receives strings. You're using variables which aren't strings.

Also, I can shorten your input loop considerably:


local input = read():lower()
if input == "o" or input == "open" then
  rednet.send( 2, "Open" )
elseif input == "c" or "close" then
  rednet.send( 2, "Close" )
elseif input == "r" or input == "reset" then
  rednet.send( 2, "Reset" )
end
MKlegoman357 #6
Posted 31 July 2015 - 05:06 PM
That's because you're sending the contents of the variables 'Open', 'Close' and 'Reset', not strings "Open", "Close" and "Reset".

You can simplify the if statements and, later, your program by not having each if do the exact same thing, but putting all the conditions in one if statement. Remember that programming's one of the most important rule is: never repeat code. If you're repeating code you probably are doing something wrong, although this might not always be the case (but it mostly is).


input = read():lower():sub(1, 1)

if input == "o" then
  rednet.send(2, "Open")
elseif input == "c" then
  rednet.send(2, "Close")
elseif input == "r" then
  rednet.send(2, "Reset")
else
  term.setCursorPos(1, 6)
  print("Syntax Error")
  sleep(3)
end   
Tassyr #7
Posted 31 July 2015 - 05:12 PM
Derp, I forgot quotation marks. Thank you. And thanks for the tip on the shortened loop! I like it.