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

Rednet Question

Started by SilentThief, 18 December 2012 - 05:17 PM
SilentThief #1
Posted 18 December 2012 - 06:17 PM
Hello, recently I have been experementing with ComputerCraft, I have not yet had enough time to fully understand lua just yet. However, I do know just a little bit. Anyways, on to the main topic.

I am messing around with rednet at the moment and seeing if I can transfer a message from one computer using a different computer, but, I have no idea where to start at this time, or even how to use rednet. Earlier today I was putting in some pre-made code by a user on these forums, I'm looking at it right now and it seems as if it was a password to a vault I guess. Here is the code -


print("Computer ID " , os.computerID())
redstone.setoutput("back" , true)
rednet.open("top")
repeat
local allowedIDs = { [33] = true, [34] = true }
local sEvent, param1, param2 = os.pullEvent()
if sEvent == "rednet_message" and allowedIDs[param1] then
redstone.setoutput("back" , false)
sleep(4)
redstone.setOutput("back" , true)
end
until event == "char" and p1=="x"

I really have no idea what that means, that's why I need some help understanding the lua coding. If anyone could help me with this I would be very greatful.
Luanub #2
Posted 18 December 2012 - 06:27 PM
I would go through and take some of the tutorials that is listed in the tutorial section of the forums. There is even one there for rednet. Come back when you have code that you're having problems with and we will be more then happy to help.
AndreWalia #3
Posted 18 December 2012 - 06:40 PM
sender

to = 25 --change this to the receiving Computer ID
while true do
   write("Message: ")
   message = io.read()
   rednet.send(to,message) ---its either this or rednet.send(message   ,to)
   print(" ")
   sleep(0.000001/999999) --this is so it has time to yeild
end
receiver:

while true do
   id,msg = rednet.receive()
   print(id..": "..msg)
   sleep(0.000001/999999) --this is so it has time to yeild
end
Orwell #4
Posted 18 December 2012 - 06:52 PM

   sleep(0.000001/999999) --this is so it has time to yeild
That would do the exact same thing as:

   sleep(0)
and:

  coroutine.yield()
Those are just a bit less elaborate.
Learning_inpaired #5
Posted 18 December 2012 - 06:59 PM
an addon question for this topic, is there a way to send/receve rednet to all computers in the area?
im trying to setup a master server but i would like for the server to send the rednet to all computhers, butonly have sertin comuters be able to receve sertin signals like for example : server sends out data from a bunch of inputs to the rednet, and computer can only receve data A wile computer b can only receve data B and so on for like 5 "clients"
ideas on this?
SilentThief #6
Posted 18 December 2012 - 06:59 PM
Well, I was following this tutorial on the Wiki - http://www.computercraft.info/wiki/Rednet_Tutorial
And it didn't work…
I'll try Scripters method next.
theoriginalbit #7
Posted 18 December 2012 - 07:13 PM
an addon question for this topic, is there a way to send/receve rednet to all computers in the area?

yes. rednet.broadcast( message ) will send to all computers in range. unlike rednet.send( id, message ) only sends to the computer with that id. rednet.receive( optionalTimeout ) will receive the message no matter how its sent out.
Luanub #8
Posted 18 December 2012 - 07:21 PM
sender

to = 25 --change this to the receiving Computer ID
while true do
   write("Message: ")
   message = io.read()
   rednet.send(to,message) ---its either this or rednet.send(message   ,to)
   print(" ")
   sleep(0.000001/999999) --this is so it has time to yeild
end
receiver:

while true do
   id,msg = rednet.receive()
   print(id..": "..msg)
   sleep(0.000001/999999) --this is so it has time to yeild
end

You don't need either of the sleeps, the io.read() yields and so does the rednet.receive().

The first set of code should have worked. Did you have your modem on the top of the computer? Here is a real basic send / receive scripts.

Sender:

rednet.open("side") --side needs to be replaced by the side that the modem is on.

rednet.send(0,"this is a test")  -- you can use read() or whatever you want to capture user input into a var and then use the vars instead

--so
print("Enter SendTo ID: ")
local id = read()
print("Enter message ")
local msg = read()
rednet.send(id, msg)


Receive

rednet.open("side")
while true do
  local id, msg = rednet.receive()
  print("the senders id is ",id)
  print("the message is ",msg)
end

To make things easier so you dont have to remember the side the modem is on(or worry about your users setting things up right) just use a function similar to this.

for _, side in pairs (rs.getSides()) do
  if peripheral.getType(side) == "modem" then
	rednet.open(side)
	break
  end
end
SilentThief #9
Posted 18 December 2012 - 07:56 PM
sender

to = 25 --change this to the receiving Computer ID
while true do
   write("Message: ")
   message = io.read()
   rednet.send(to,message) ---its either this or rednet.send(message   ,to)
   print(" ")
   sleep(0.000001/999999) --this is so it has time to yeild
end
receiver:

while true do
   id,msg = rednet.receive()
   print(id..": "..msg)
   sleep(0.000001/999999) --this is so it has time to yeild
end

You don't need either of the sleeps, the io.read() yields and so does the rednet.receive().

The first set of code should have worked. Did you have your modem on the top of the computer? Here is a real basic send / receive scripts.

Sender:

rednet.open("side") --side needs to be replaced by the side that the modem is on.

rednet.send(0,"this is a test")  -- you can use read() or whatever you want to capture user input into a var and then use the vars instead

--so
print("Enter SendTo ID: ")
local id = read()
print("Enter message ")
local msg = read()
rednet.send(id, msg)


Receive

rednet.open("side")
while true do
  local id, msg = rednet.receive()
  print("the senders id is ",id)
  print("the message is ",msg)
end

To make things easier so you dont have to remember the side the modem is on(or worry about your users setting things up right) just use a function similar to this.

for _, side in pairs (rs.getSides()) do
  if peripheral.getType(side) == "modem" then
	rednet.open(side)
	break
  end
end
In your code at
print("Enter SendTo ID: ")
What's the ID?
theoriginalbit #10
Posted 18 December 2012 - 07:58 PM
sender

to = 25 --change this to the receiving Computer ID
while true do
   write("Message: ")
   message = io.read()
   rednet.send(to,message) ---its either this or rednet.send(message   ,to)
   print(" ")
   sleep(0.000001/999999) --this is so it has time to yeild
end
receiver:

while true do
   id,msg = rednet.receive()
   print(id..": "..msg)
   sleep(0.000001/999999) --this is so it has time to yeild
end

You don't need either of the sleeps, the io.read() yields and so does the rednet.receive().

The first set of code should have worked. Did you have your modem on the top of the computer? Here is a real basic send / receive scripts.

Sender:

rednet.open("side") --side needs to be replaced by the side that the modem is on.

rednet.send(0,"this is a test")  -- you can use read() or whatever you want to capture user input into a var and then use the vars instead

--so
print("Enter SendTo ID: ")
local id = read()
print("Enter message ")
local msg = read()
rednet.send(id, msg)


Receive

rednet.open("side")
while true do
  local id, msg = rednet.receive()
  print("the senders id is ",id)
  print("the message is ",msg)
end

To make things easier so you dont have to remember the side the modem is on(or worry about your users setting things up right) just use a function similar to this.

for _, side in pairs (rs.getSides()) do
  if peripheral.getType(side) == "modem" then
	rednet.open(side)
	break
  end
end
In your code at
print("Enter SendTo ID: ")
What's the ID?

it uses the next line of code, the read( ) to get the id…
Luanub #11
Posted 18 December 2012 - 08:41 PM
The ID is the computers ID that you want to send the message to
SilentThief #12
Posted 18 December 2012 - 09:47 PM
The ID is the computers ID that you want to send the message to
Ok, well once I do that and enter the message it says:
Looking for positive number
And does not send a message to reciver.
remiX #13
Posted 18 December 2012 - 10:42 PM
Try changing

local id = read()
to
local id = tonumber(read())

Not entirely sure this will fix it
SilentThief #14
Posted 19 December 2012 - 10:02 AM
Fixed it to maximum efficiency.

Sender:

rednet.open("top")

rednet.send(0,"this is a test")

--so
print("Enter SendTo ID: ")
local id = tonumber(read())
print("Enter message ")
local msg = read()
rednet.send(id, msg)

Receiver:

rednet.open("top")
while true do
	local id, msg = rednet.reveive()
	print("the senders id is ",id)
	print("the message is",msg)
	end

Works pretty good for me.