ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: curlycue104 on Dec 08, 2019, 04:45 PM

Title: How to activate a turtle wirelessly
Post by: curlycue104 on Dec 08, 2019, 04:45 PM
Apologies for the newbie question - but I've been all over the wiki and I can't figure this out. I want to know how to send a command to a turtle using a wireless modem. So far I've been able to wrap the modem on both the computer and the turtle, open the same channel on both, and send a message over the channel. But I cannot seem to figure out how to use that "message" to enter a command and start a program. I must be missing something obvious. Help?
Title: How to activate a turtle wirelessly
Post by: Lupus590 on Dec 08, 2019, 07:15 PM
you probably want to look at the shell api, specifically the run method

https://computercraft.info/wiki/Shell.run
Title: How to activate a turtle wirelessly
Post by: curlycue104 on Dec 09, 2019, 02:43 PM
I'm not sure what you mean. I tried this:

modem.transmit(1, 1, shell.run("dance"))

And it told me that no such program existed. The dance program worked fine on the turtle when I tried it directly. Can you elaborate on how I should use shell.run? Thank you!

Edit: I don't know why that turned into a hyperlink but it doesn't seem to go anywhere so shrug.
Title: How to activate a turtle wirelessly
Post by: Lupus590 on Dec 09, 2019, 08:15 PM
why are you using the modem API when the rednet one is easier to use in nearly every use case?

you also want to have the receiving computer put the received instruction into the shell.run call, with the sending machine transmitting those commands


--# sender
while true do
  rednet.send(remoteId, read())
end

--# receiver
while true do
  local _, command = rednet.recieve()
  shell.run(command)
end

you might want to check out nsh (http://www.computercraft.info/forums2/index.php?/topic/6472-nsh-now-with-previous-session-resume/) or vncd (http://www.computercraft.info/forums2/index.php?/topic/17229-vncd-an-nsh-compatible-vnc-server/) in case they do what you are trying to achieve
Title: How to activate a turtle wirelessly
Post by: curlycue104 on Dec 10, 2019, 02:57 PM
Oh! I think I'm beginning to understand. The receiving computer has to interpret the message as a command instead of just a message. I feel like that should have been obvious. Thank you for the help!
Title: How to activate a turtle wirelessly
Post by: Lupus590 on Dec 10, 2019, 07:39 PM
be warned, there is no security on my code snippet so anyone can run any program on the receiving machine

the discarded value _ returned by rednet.recive is the sender's id, you can use this to whitelist safe senders.
Title: How to activate a turtle wirelessly
Post by: curlycue104 on Dec 15, 2019, 03:32 PM
The security isn't an issue, I play on a server with 4 people who are most certainly not interested in griefing me.
So I thought it did it correctly, but when I send the command to the receiving turtle it simply says that no such program exists although I checked the spelling of the short test program I wrote several times and even tried sending "dance" which is already on the turtle. Does the same program have to be on the sending computer? Here is the receiving program I wrote for the turtle.

rednet.open("right")
local ready = rednet.isOpen()
while ready == true do
   local id, command, protocol = rednet.receive()
   shell.run("command")
end

For the sending computer I wrote this.

rednet.open("back")
rednet.broadcast("dance")

I'm not sure what I did wrong.
Title: How to activate a turtle wirelessly
Post by: Lupus590 on Dec 15, 2019, 05:41 PM
remove the quotes around the word command in the shell.run

shell.run(command)
-- not
shell.run("command")

Title: How to activate a turtle wirelessly
Post by: curlycue104 on Dec 15, 2019, 06:32 PM
I removed the quotes but I'm still getting the "no such program" message.
Title: How to activate a turtle wirelessly
Post by: Lupus590 on Dec 15, 2019, 08:51 PM
is it possible that other people are sending rednet messages which your turtle is hearing and trying to use as a command?

maybe you should whitelist by the id or the protocol

-- reciver code
local whitelistedSender = {
  1 = true, -- computer with id of 1 is allowed to send
  5 = true, -- computer with id of 5 is also allowed to send
}

local whitelistedProtocol = "curlycue104's turtle remote"

rednet.open("right")
local ready = rednet.isOpen() -- NOTE: ready never changes, did you mean to put the resnet.isOpen call into the loop?
while ready == true do
   local id, command, protocol = rednet.receive()
   if whitelistedSenders[id] and protocol == whitelistedProtocol then
      shell.run("command")
   end
end

the sender will need to have it's id in the whitelist and be sending on the correct protocol. Be warned that this is not proper security as rednet ID's can be spoofed.
Title: How to activate a turtle wirelessly
Post by: curlycue104 on Dec 18, 2019, 06:54 PM
Hi! I'm sorry it has taken me a couple of days to respond. There's no chance of anyone else using rednet because I was testing the code in a singleplayer game.
Title: How to activate a turtle wirelessly
Post by: Lupus590 on Dec 18, 2019, 10:21 PM
check your spelling? perhaps try running the command on the turtle directly (I assume that you don't want to be able to get the turtle to dance by remote

you might also want to try the nsh (http://www.computercraft.info/forums2/index.php?/topic/6472-nsh-now-with-previous-session-resume/) or vncd (http://www.computercraft.info/forums2/index.php?/topic/17229-vncd-an-nsh-compatible-vnc-server/) programs