Before you can start using rednet you will need two computers and two wireless modems attached to the a fore mentioned computers, Once attached we are going to have to 'open' the rednet on this modem.
This is NOT the same-as opening channels using the modem API, this will be covered in future parts.
To open your rednet your going to want the modem 'wrapped' as a peripheral, we will use this is other tutorials when we move onto the modem API, essentially means you can access it as a variable and uses its functions like sending and receiving messages, here's the very basic way of opening a modem, for this you will need to hardcode the side in which the modem resides;
local wireless = peripheral.wrap("top") --#Side of modem
rednet.open("top") --#Open Modem
The first line wraps the modem allowing you to use it later, the second opens the modem, the modem should now have a red ring around it. The only problem here is that errors may arise if the modem isnt present or is on another side, to fix that we will need to create more sophisticated code, like so;
for i, side in ipairs(rs.getSides()) do --#Loop for each side on the computer that redstone can be used, these can also be used by modems.
if peripheral.getType(side) == 'modem' then --#Did we find a modem... Or some other peripheral?
print('Found Modem On Side '..side.."!")
rednet.open(side)
wireless = peripheral.wrap(side) --# For when we use the Modem API
break --#Opened modem, dont open any others.
end
end
This will open the first modem it finds on any side of the computer, very helpful, although this will open wired modems as well, so make sure you only attach wireless modems, they are both called modems so there is no easy way to distinguish the two…
Now we have two modems open, one on each computer your ready to start communicating, to do so we need to set up some event listening, events are queued by the computer when something happens, like a key press or a mouse click, along with the event variables are passed containing important information. Such information included the rednet message, to setup event listening lets do the easy way first:
local id, message, protocol = rednet.receive()
This code is VERY simple, it will wait for ever or until a rednet message is received, this isnt a very 'good' way to receive messages as this requires the use of parallel or co-routine unnecessarily. You could instead use event listeners, when a message is received send it off to get processed, although it really depends on the situation, my programs use various methods, if I dont NEED to be listening for other events then I go for the simple way, else I use this code snippet:
function mainEventLoop()
while true do
local e, p1, p2, p3, p4, p5, p6 = os.pullEvent() --#Waits for events and places all values into local vars.
if e == "rednet_message" then --#Handle rednet message
elseif e == "mouse_click" then element.tryClick(e, p1, p2, p3)
elseif e == "terminate" then os.shutdown() end
sleep() --#Prevents too long without yielding errors that MAY occur.
end
end
This uses my basic framework, although even this is obselete for my purpose, I use the more common event registration and call a function upon event appearance, I may get into this if you guys want in a future part, for the sake of the tutorial I will use the simple method of receiving messages, lets start our first test program, this program will be on the receiving computer;
function waitForMessage(displayMessage)
local sender, message, protocol = rednet.receive()
if displayMessage then print(message) else return message end
end
waitForMessage(true)
This waits for a message and the true im passing to the function tells it to print the message rather than return it as a var. This part will be on the sending computer;
function sendMessage(destination, message, protocol)
if destination and message then
rednet.send(destination, message, protocol)
end
end
sendMessage(49, 'hey')
This will send a message to computer ID 49 with the message 'hey', to find your computer id type
id
into the terminal.Complete Code
Spoiler
Just incase you lost your way heres all the code in one view;PC1
for i, side in ipairs(rs.getSides()) do --#Loop for each side on the computer that redstone can be used, these can also be used by modems.
if peripheral.getType(side) == 'modem' then --#Did we find a modem... Or some other peripheral?
print('Found Modem On Side '..side.."!")
rednet.open(side)
wireless = peripheral.wrap(side)
break --#Opened modem, dont open any others.
end
end
function waitForMessage(displayMessage)
local sender, message, protocol = rednet.receive()
if displayMessage then print(message) else return message end
end
waitForMessage(true)
PC2
for i, side in ipairs(rs.getSides()) do --#Loop for each side on the computer that redstone can be used, these can also be used by modems.
if peripheral.getType(side) == 'modem' then --#Did we find a modem... Or some other peripheral?
print('Found Modem On Side '..side.."!")
rednet.open(side)
wireless = peripheral.wrap(side)
break --#Opened modem, dont open any others.
end
end
function sendMessage(destination, message, protocol)
if destination and message then
rednet.send(destination, message, protocol)
end
end
sendMessage(49, 'hey')
Well everyone, that concludes part one, next part will cover protocols and table sending. If you want a second part then let me know!
- Harry