ComputerCraft

Computer network via modem

Connect multiple computers wirelessly using the rednet API. Send commands, receive status updates, and remotely control machines across your base.

What you will end up with

Two computers communicating wirelessly: a sender that broadcasts commands, and a receiver that listens and acts on them — for example, toggling a redstone output or printing a status message.

What you need

Each computer has a unique numeric ID. You can see it at the top of the terminal or with the command id. You will need the receiver's ID for the sender script.

How rednet works

rednet is a high-level messaging API built on top of the raw modem API. You open it on the side where the modem is attached, then use rednet.send() to send to a specific computer ID or rednet.broadcast() to send to all computers in range.

Step 1 — Set up the receiver

On Computer A (the receiver), attach a wireless modem to the top and create this script:

-- receiver.lua -- Listens for commands and executes them rednet.open("top") print("Receiver ready. ID: " .. os.getComputerID()) while true do local senderId, message = rednet.receive() print("From " .. senderId .. ": " .. tostring(message)) if message == "activate" then redstone.setOutput("right", true) os.sleep(1) redstone.setOutput("right", false) rednet.send(senderId, "done") elseif message == "ping" then rednet.send(senderId, "pong") end end

Run it:

receiver

Note the ID printed on screen — you need it for the sender.

Step 2 — Set up the sender

On Computer B (the sender), attach a wireless modem to the top and create this script. Replace TARGET_ID with the ID you noted from the receiver.

-- sender.lua -- Sends a command to the receiver computer local TARGET_ID = 5 -- replace with your receiver's ID rednet.open("top") print("Sending 'activate' to computer " .. TARGET_ID) rednet.send(TARGET_ID, "activate") local _, reply = rednet.receive(5) -- wait up to 5 seconds if reply then print("Reply: " .. reply) else print("No reply received.") end rednet.close("top")

Run it:

sender

The receiver should print the message and toggle its redstone output.

Broadcasting to all computers

If you want to send a message to every computer in range without specifying an ID, use rednet.broadcast():

rednet.open("top") rednet.broadcast("hello everyone") rednet.close("top")

Using protocols

For more complex networks, pass a protocol string to filter messages by type:

-- Send with protocol rednet.send(TARGET_ID, "status_ok", "my_protocol") -- Receive only messages matching this protocol local id, msg = rednet.receive("my_protocol", 5)

Troubleshooting