How to activate a turtle wirelessly

Started by curlycue104, Dec 08, 2019, 04:45 PM

Previous topic - Next topic

curlycue104

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?

Lupus590

you probably want to look at the shell api, specifically the run method

https://computercraft.info/wiki/Shell.run

curlycue104

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.

Lupus590

#3
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 or vncd in case they do what you are trying to achieve

curlycue104

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!

Lupus590

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.

curlycue104

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.

Lupus590

remove the quotes around the word command in the shell.run

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


curlycue104

I removed the quotes but I'm still getting the "no such program" message.

Lupus590

#9
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.

curlycue104

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.

Lupus590

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 or vncd programs