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

Topic Hijack Rednet Question

Started by duskie, 29 October 2013 - 04:33 AM
duskie #1
Posted 29 October 2013 - 05:33 AM
Hey, sorry I can't make a topic so I put this here;

I'm new to lua and computer craft, and I've spent the last two days making my first programs, but now I'd like to run them from a computer with a wireless modem.

I did attach a modem to my computer, my turtles are wireless too.I used rednet.open(side) to turn them on, and I tried to send messages with rednet.send(9, message) while my turtle was in receive mode (at least so I think).

but I don't know how to make them communicate properly, i'd just like to rexecute a program installed on a specific turtle from a computer. the command list in the wiki doesn't mention anything like this, tutorials on youtube are like "so I run this floppy and it works" so… help?
Lyqyd #2
Posted 29 October 2013 - 09:23 AM
Split into new topic.

Go read the announcement post titled Forum Guidelines.
sens #3
Posted 29 October 2013 - 09:46 AM
I did attach a modem to my computer, my turtles are wireless too.I used rednet.open(side) to turn them on, and I tried to send messages with rednet.send(9, message) while my turtle was in receive mode (at least so I think).

By "receive mode", do you mean that the turtle calls rednet.receive()? If so, use the message that was received to decide what to do:

local senderId, message, distance = rednet.receive()
if message == "run_my_program" then
  shell.run("my_program")
else
  print("Ignoring message: "..message)
end
duskie #4
Posted 29 October 2013 - 12:26 PM
this worked, thanks!

-but-

I it a bit frustrating since every time I want to make a turtle work I first got to run the receive program on it, it kills a bit the wireless… I could as well run the right program manually instead of running a program to enable a program…

is there no way to let them constantly ready to recieve new orders?
sens #5
Posted 29 October 2013 - 12:50 PM
I'm not sure I understood your question. If you want to keep receiving new orders after "my_program" finishes running, simply wrap the routine in an infinite loop:


while true do
  local senderId, message, distance = rednet.receive()
  if message == "run_my_program" then
	shell.run("my_program")
  elseif message == "do_something_else" then
	shell.run("something_else")
  else
	print("Ignoring message: "..message)
  end
end
spdkils #6
Posted 29 October 2013 - 02:22 PM
I do this in a similar fasion… but I did it a bit more generic.

startup
while true do
local tEvent = {os.pullEvent('rednet_message')}
local tProg = textutils.unserialize(tEvent[3])
shell.run(unpack(tProg))
end

Then on the sender computer I just package commands to broadcast.

b
local tArgs = { ... }
local data = textutils.serialize(tArgs)
rednet.broadcast(data)

I save that as b… so I can just type


b go fd 23

all my turtles run go fd 23….

Then you can also pastebin to all turtles, replace there startup, and reboot remotely, etc, etc, etc.
spdkils #7
Posted 29 October 2013 - 05:41 PM
Doing it this way, means it's all or nothing on the turtles…

So a different way, would to abandon rednet, and wrap a modem. Then you can have some open different channels, so you can have 10 you remote command for digging, and 2 for floorng, etc etc.

If you want to have them able to take commands…. make a loop, and eat all events, if you get a char event, build the command, and if you get the enter, you have to queue and event. (You can't react to your own generated events, so you have to os.queueEvent(blah) to have the code execute local.

I have a pretty terrible implimentation if you want to see it. But I am slowly adding features etc.

http://pastebin.com/qgJg32s4
Lyqyd #8
Posted 29 October 2013 - 05:44 PM
Or you'd use rednet.send instead of rednet.broadcast.
spdkils #9
Posted 29 October 2013 - 09:28 PM
I like to control multiple turtles, so I use send if I want to talk to 1 turtle, but I don't like to build an array and send mutiple times, so I thought the modem would be easier because I can designate a "command" channel, and an informational broadcast channel, and then you can group turtles without arrays. Just have them open the channel you want them to use, and open any other channels you may want. (You can see my pastebin for my hack job..) Sorting the output is in there, but I don't how to handle getting all the data and only painting once. But, yeah. Lots of ways of doing it.
duskie #10
Posted 30 October 2013 - 10:27 AM
thanks for all your answer, i'll try them out

I'm not sure, what does wrapping a modem?
spdkils #11
Posted 30 October 2013 - 02:59 PM
You can't listen to multiple channels unless you abandon rednet, and move to a peripherial. Then you use the wrap function to nab the modem.

If you're talking a turtle… modem is on the right.

local modem = periphrial.wrap('right')
modem.open(channel)
modem.transmit(channel,rchannel,message)

http://computercraft.info/wiki/Peripheral_%28API%29

http://computercraft.info/wiki/Modem_%28API%29