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

Rednet Help

Started by xWoody25, 15 April 2012 - 03:32 AM
xWoody25 #1
Posted 15 April 2012 - 05:32 AM
Hi i am trying to make a program that allows me send a command to a wireless mining turtle from a computer, i am fairly new to the mod, i have only been using it for about 5 days and finding the redset command/program very hard, i'm fed up of making basic password doors and want a bigger challenge, thank you all very much.

p.s Please try to make it easy to understand from a noobs point of view :)/>/>
Luanub #2
Posted 15 April 2012 - 06:09 AM
Here's a simple example that will move a turtle forward 1 space. It should give you the general idea and you can go from there.

turtle

rednet.open("right") -- turn on the turtles modem
while true do -- infinite loop to keep the turtle "listening" for commands
local event, id, msg = os.pullEvent("rednet_message")  -- wait for a rednet message event
if msg == "forward" then -- check what the message/command was
turtle.forward() -- execute what the command was ordering done
end  -- end the if
end -- end the loop

Computer

rednet.open("side") -- change to the side your modem is on
shell.run("clear")
while true do
print ("Press Any Key to Send the Command") -- Build a menu to replace this with to do more stuff
os.pullEvent("key") -- wait for a key event
rednet.send("turtleID", "forward") -- change turtleID to your turtles ID or use rednet.broadcast("forward") to send the message to anything listening
end

Havent tried it but it should work.
xWoody25 #3
Posted 15 April 2012 - 04:07 PM
Here's a simple example that will move a turtle forward 1 space. It should give you the general idea and you can go from there.

turtle

rednet.open("right") -- turn on the turtles modem
while true do -- infinite loop to keep the turtle "listening" for commands
local event, id, msg = os.pullEvent("rednet_message")  -- wait for a rednet message event
if msg == "forward" then -- check what the message/command was
turtle.forward() -- execute what the command was ordering done
end  -- end the if
end -- end the loop

Computer

rednet.open("side") -- change to the side your modem is on
shell.run("clear")
while true do
print ("Press Any Key to Send the Command") -- Build a menu to replace this with to do more stuff
os.pullEvent("key") -- wait for a key event
rednet.send("turtleID", "forward") -- change turtleID to your turtles ID or use rednet.broadcast("forward") to send the message to anything listening
end

Havent tried it but it should work.
I have put the code in exactly how you have done it, and when i press the key to send the command is says "Positive Number expected", does this mean i have to put the key id for the up arrow on the keyboard
Cloudy #4
Posted 15 April 2012 - 05:43 PM
No. Replace "turtleID" in the rednet.send function with the id of the turtle, without quotation marks around it.
xWoody25 #5
Posted 15 April 2012 - 05:45 PM
No. Replace "turtleID" in the rednet.send function with the id of the turtle, without quotation marks around it.
i have done that, do i need to include the '#' symbol?
xWoody25 #6
Posted 15 April 2012 - 05:47 PM
No. Replace "turtleID" in the rednet.send function with the id of the turtle, without quotation marks around it.
i have done that with "" but it still comes up with that error, do i need to include the # in the ID of the turtle aswell
xWoody25 #7
Posted 15 April 2012 - 06:11 PM
right i have got that all fixed with no errors but now the turtle does not move forward, ive checked all of the spellings and everything, please help :)/>/>
cant_delete_account #8
Posted 15 April 2012 - 07:40 PM
Computer (with rednet modem on RIGHT):

print("Type 'forward' to move turtle forward, 'back' to move turtle back, etc.")
while true do
sleep(0.1)
term.clear()
term.setCursorPos(1,1)
write("Command: ")
input = read()
rednet.broadcast(input)
end
Turtle (WIRELESS turtle. mining turtle):

while true do
sleep(0.1)
term.clear()
term.setCursorPos(1,1)
print("Listening on port 80...")
local id, msg, distance = rednet.receive()
print("We've got a message from computer ID #"..id.."!")
print("The message is:")
print(msg)
print("This message was received from "..distance.." blocks away!")
if msg == "forward" then
turtle.forward()
elseif msg == "back" then
turtle.back()
elseif msg == "dig" then
turtle.dig()
elseif msg == "right" then
turtle.turnRight()
elseif msg == "left" then
turtle.turnLeft()
elseif msg == "down" then
turtle.down()
elseif msg == "up" then
turtle.up()
elseif msg == "exit" then
error()
end
xWoody25 #9
Posted 15 April 2012 - 07:54 PM
Computer (with rednet modem on RIGHT):

print("Type 'forward' to move turtle forward, 'back' to move turtle back, etc.")
while true do
sleep(0.1)
term.clear()
term.setCursorPos(1,1)
write("Command: ")
input = read()
rednet.broadcast(input)
end
Turtle (WIRELESS turtle. mining turtle):

while true do
sleep(0.1)
term.clear()
term.setCursorPos(1,1)
print("Listening on port 80...")
local id, msg, distance = rednet.receive()
print("We've got a message from computer ID #"..id.."!")
print("The message is:")
print(msg)
print("This message was received from "..distance.." blocks away!")
if msg == "forward" then
turtle.forward()
elseif msg == "back" then
turtle.back()
elseif msg == "dig" then
turtle.dig()
elseif msg == "right" then
turtle.turnRight()
elseif msg == "left" then
turtle.turnLeft()
elseif msg == "down" then
turtle.down()
elseif msg == "up" then
turtle.up()
elseif msg == "exit" then
error()
end
Thanks but can you simplifie it a bit :)/>/>
Luanub #10
Posted 15 April 2012 - 10:36 PM
right i have got that all fixed with no errors but now the turtle does not move forward, ive checked all of the spellings and everything, please help :)/>/>

The code I posted should work as long as your putting in the turtle id as say 1 not "1" or #1. Also make sure you are changing the side of the modem on the computer script to the side that your modem is on. Make sure the red bands are on all of the modems.

Sorry for the errors yesterday like I said I couldn't test it. I have now and it works. I put what I used in the spoiler.

SpoilerComputer

rednet.open("right") -- change to the side your modem is on
shell.run("clear")
while true do
print ("Press Any Key to Send the Command") -- Build a menu to replace this with to do more stuff
os.pullEvent("key") -- wait for a key event
rednet.send( 19, "forward") -- change 19 to your turtles ID or use rednet.broadcast("forward") to send the message to anything listening
end

Turtle

rednet.open("right") -- turn on the turtles modem
while true do -- infinite loop to keep the turtle "listening" for commands
local event, id, msg = os.pullEvent("rednet_message")  -- wait for a rednet message event
if msg == "forward" then -- check what the message/command was
turtle.forward() -- execute what the command was ordering done
end  -- end the if
end -- end the loop
xWoody25 #11
Posted 16 April 2012 - 12:24 AM
right i have got that all fixed with no errors but now the turtle does not move forward, ive checked all of the spellings and everything, please help :)/>/>

The code I posted should work as long as your putting in the turtle id as say 1 not "1" or #1. Also make sure you are changing the side of the modem on the computer script to the side that your modem is on. Make sure the red bands are on all of the modems.

Sorry for the errors yesterday like I said I couldn't test it. I have now and it works. I put what I used in the spoiler.

SpoilerComputer

rednet.open("right") -- change to the side your modem is on
shell.run("clear")
while true do
print ("Press Any Key to Send the Command") -- Build a menu to replace this with to do more stuff
os.pullEvent("key") -- wait for a key event
rednet.send( 19, "forward") -- change 19 to your turtles ID or use rednet.broadcast("forward") to send the message to anything listening
end

Turtle

rednet.open("right") -- turn on the turtles modem
while true do -- infinite loop to keep the turtle "listening" for commands
local event, id, msg = os.pullEvent("rednet_message")  -- wait for a rednet message event
if msg == "forward" then -- check what the message/command was
turtle.forward() -- execute what the command was ordering done
end  -- end the if
end -- end the loop
right i have checked over my code, it is identical to yours (except the bits that are not meant to be like the turtle ID but every time i press any key the text "press any key . ect" keeps popping up and the turtle does not move) i have made sure that the red band has lit up on both the computer and turtle but the turtle doesnt move, i dont have a clue :S thanks for the help by the way, i would show you my code but its on a online server, if you tell me how to copy it that would be great (do i just have to type it manually?)
xWoody25 #12
Posted 16 April 2012 - 01:09 AM
i have fixed the code so it works, now i want to make it so when i press a different arrow it will go forward, back, left and right plz can you post an easy example
cant_delete_account #13
Posted 16 April 2012 - 01:12 AM
Thanks but can you simplifie it a bit :)/>/>
How is that not simple? Its as simple as simple can be.
xWoody25 #14
Posted 16 April 2012 - 01:58 AM
Thanks but can you simplifie it a bit :)/>/>
How is that not simple? Its as simple as simple can be.
i have fixed the code, please read my last post :)/>/>
Luanub #15
Posted 16 April 2012 - 08:09 AM
You will want to use a "key" event and then send the appropriate command to the turtle for the key that is pressed.


while true do
local evt, key = os.pullEvent("key")
if key == 200 then -- up arrow pressed
rednet.send(id, "command")
elseif key == 208 then -- down arrow pressed
rednet.send(id, "command")
elseif key == 205 then -- right arrow
rednet.send(id, "command")
elseif key == 203 then -- left arrow
rednet.send(id, "command")
end
SuperN00b #16
Posted 22 October 2012 - 03:39 AM
I used Lua Nub's code, but whenever I type in the command on the computer it says:
turtle:5: attempt to call nil

What is wrong, and how can I fix it?
Lyqyd #17
Posted 22 October 2012 - 03:53 AM
His line five doesn't call anything, so obviously, you're not using his code. Post the complete code that you are using.
SuperN00b #18
Posted 23 October 2012 - 01:09 AM
rednet.open("Right")
shell
.run("clear")
while true do
print ("Press Any Key to Send the Command")
os
.pullEvent("key")
rednet.send("9", "forward")
end
This is the code I used
Lyqyd #19
Posted 23 October 2012 - 01:16 AM
And that code doesn't have any problems on line five, unless you had overriden os.pullEvent() elsewhere. Are you retyping the code or copying and pasting it? We need the exact code that you're using, not retyped code.
ChunLing #20
Posted 23 October 2012 - 01:16 AM
Okay, there are some problems. You didn't post the entire error message, though. When you use a function from an API, the error message includes the name and line of the API where the error actually occurred. I'm guessing the problem was from the rednet API, because your rednet.send("9", "forward") call is borked. It should be rednet.send(9, "forward"). I'm also not sure that rednet.open("Right") works the same as rednet.open("right").
SuperN00b #21
Posted 23 October 2012 - 01:27 AM
First, not sure how to copy and paste yet, yes I am a little new, but i did copy what i wrote word for word, and i did change the Right to right, but i get the same message:

Press any key to send the command
turtle:5: attempt to call nil
ChunLing #22
Posted 23 October 2012 - 01:35 AM
Go to your saves directory and look in worldname/computer/ID (where worldname is the name of the world and ID is the ID of the computer you stored this program on). Open the file with notepad or any other text editor, then copy and paste the entire thing. But if that error message is right, you're using an API or something that has borked os.pullEvent().
SuperN00b #23
Posted 23 October 2012 - 01:52 AM
rednet.open("right")
shell.run("clear")
while true do
print ("Press any key to send the command")
os.pullevent("key")
rednet.send(9,"forward")
end
Lyqyd #24
Posted 23 October 2012 - 02:03 AM
There's the problem. Line five should have a capital E. os.pullEvent(), not os.pullevent().
ChunLing #25
Posted 23 October 2012 - 02:03 AM
os.pullevent("key") should be os.pullEvent("key"). But that's good news, because it means that your API isn't borked up.

^_^/>/>
Edited on 23 October 2012 - 12:04 AM
SuperN00b #26
Posted 23 October 2012 - 03:02 AM
Thank you!!!!
Cookie #27
Posted 22 November 2013 - 11:03 PM
The code everyone has is good, but does anyone have some code that's not a giant if loop? eg

if message == "command"
    command
Does anyone have some that is similar to a peripheral.wrap() but for turtles? So that you could go turtleName.command()
Thanks!