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

rednet Broadcast - transmitting Variables?

Started by Nopsi, 02 November 2012 - 11:26 PM
Nopsi #1
Posted 03 November 2012 - 12:26 AM
Hiya,

my goal is it to control several mining turtles via Wireless (rednet)

so i made these two two code parts:

Computer:


term.clear()
t1 = 0
tv1 = 0
rednet.open("right")
print ("Willkommen im Mining Program Version 1.1")
print ("Erstellt von Nopsi99")
print ("")
print ("")
write ("Tunnel Länge: ")
t1 = io.read()
write ("Tunnel versatz nach unten: ")
tv1 = io.read()
term.clear()
term.setCursorPos(1,1)
print ("Danke")
print ("")
print ("Starte Tunnel mit einer Länge von: " ..t1)
print ("Versatz nach unten ist: " ..tv1)
rednet.broadcast("tunnel")
rednet.broadcast(t1)
rednet.broadcast("tver")
rednet.broadcast(tv1)

It asks how long the tunnel should be (t1 variable) and if the tunnel should be moved down (tv1 variable)

so i can place the turtles, tell them to make a tunnel 20 long, come back, drop the items and wait. the next start will have a moving from 2 blocks down. so they mine a new tunnel, right below the former tunnel.

the question i have is: how can i transmit via rednet broadcast a variable?

the turtles are all named and have this startup file:


rednet.open('right')
while true do
  local id,msg,dist=rednet.receive()
  if msg=='starttunnel' then
    shell.run('tunnel',50)
  end
end

atm they wait to get the command "starttunnel" and they will start digging a tunnel 50 long.

my idea was to transmit a message ("tunnel") so the programm knows, now comes the variable for the tunnel lenght and then comes the broadcast with the variable t1
the next broadcast will be ("tver") and then comes the moving pattern.

is there any other options? any hints?

thank you very much :D/>/>
remiX #2
Posted 03 November 2012 - 01:11 AM
I think this could work… I honestly don't know… I'll try it out now


rednet.open('right')
while true do
  e, id, msg, dist = os.pullEvent("rednet_message")
  msg = {...}
  if msg[1] =='starttunnel' then
	length = msg[2]
	shell.run('tunnel',length)
  end
end

Then for the computer to transmit:


rednet.broadcast("tunnel "..t1)

edit: Nah, it doesn't seem to work. Another way is it transmit it into one message like I did and then make length = the last 2 characters in the string. Looking how to do it now.

edit #2: Here this works.. Computer code doesn't need to be edited at all

rednet.open('right')
term.clear()
term.setCursorPos(1,1)
while true do
  print("Awaiting message...")
  e, id, msg, dist = os.pullEvent("rednet_message")
  print("Got: "..msg)
  if msg =='tunnel' then
    e1, id1, msg1, dist1 = os.pullEvent("rednet_message")
    print("Tunneling: "..msg1)
    shell.run('tunnel',msg1)
  end
end
ChunLing #3
Posted 03 November 2012 - 04:19 AM
I prefer to use string functions to strip a single rednet message down to components rather than sending multiple rednet messages. But either way works.