Mining Turtles Complete Message through rednet

UK_Lone_Wolf

New Member
Jul 29, 2019
5
0
0
Hi slowly learning computer craft at the min but this has got me stumped

I got six turtles running quarry's and they are placed really far from my home, base, hq
the plan is that when they have finished they send a "Mining Complete" message the thing is getting the
computer in my home, base, hq, to keep a log or list them as they complete their task while keeping their ID so I can tell them apart from each other.

This is what I have got working so far just to monitor one but the challenge is to monitor 6 with capability's of adding more or less when needed:

Turtle:
print ("Moving to complete position")
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.forward()
rednet.open("right")
rednet.send(03, DIGGE01)
rednet.close("right")
shell.run ("Menu")

Computer:

shell.run ("clear")
print ("Turtle Monitor")
rednet.open ("right")
rednet.receive()
print ("Turtle01 has finished")
print ("Press any key to continue")
os.pullEvent("key")
shell.run ("reboot")

I think the answer is staring me in the face (if message == DIGGE01 then) is what i am thinking but as its 3am my eyes are going fuzzy and nearly enough cant see the screen. :)
Any help will be appreciated.

Thanks
UK_Lone_Wolf

PS: Sorry for any grammar mistakes.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
The rednet.open() function needs a string as its second argument. Just put speech marks around "DIGGE01" and the turtle code should be fine. Unless DIGGE01 is the name of a variable and that variable is a string.

You will need to save the return of rednet.receive as a variable in order to store its message. In the example code I am showing you I am going to save it as a table shown by curly brackets {}. A table is a type of variable that stores multiple pieces of data within itself. rednet.receive() returns 3 pieces of data, the second one being the message and the first id the sender id.

local message = {rednet.receive()}

This will run the rednet.receive command and then save it all to a table. To access each point in the table you need to write the name followed by the key in square brackets - the key is a number starting from 1. Like I said before the string message is number 2. To print out the turtle info you could do this

print("Turtle ID:"..message[1].." - "..message[2])

which will print this - Turtle ID:1 - DIGGE01

With that variable you can do more advanced things like print it to a sheet of paper, display it on a monitor or save it in the computers directory.
 

Hydra

New Member
Jul 29, 2019
1,869
0
0
What I personally do is just use os.Pullevent() without any filters in a loop. Since recieving messages also gives events you simply handle events based on event types, like this:

Code:
while true do
  local event,param1, param2, param3 = os.pullEvent()
 
  if(event == "key") then
    handleKeyPress(param1)
  elseif(event == "rednetSometing")
    handleMessage(param1,param2,param3)
  else
    print("Unknown eventtype:" .. event)
  end
end

This also allows you to for example add a timer event so your loop wakes up every now and then.
 
  • Like
Reactions: UK_Lone_Wolf

UK_Lone_Wolf

New Member
Jul 29, 2019
5
0
0
Thanks for the reply casilleroatr and Hydra I am getting closer I got it to list but just for one turtle and after a while the bios:140: Too long without yielding error kicks in.
So defiantly closer as it list "Turtle ID:1 - DIGGE01 Complete" but just 100's of times on a loop.


Here is the code so far:

Turtle:

turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.forward()
rednet.open("right")
rednet.send(03, "DIGGE01 Complete")
rednet.close("right")
shell.run ("Menu")

Computer:

rednet.open("right")
local message = {rednet.receive(20)}
while true do
if message then
print("Turtle ID:"..message[1].."-"..message[2])
end
end
 

angelnc

New Member
Jul 29, 2019
232
0
0
You have a loop because you don't pull the event inside the loop. You receive the message before. So it just prints the message over and over again.
Try using Hydras code instead to pull the event and handle the message.
 
  • Like
Reactions: UK_Lone_Wolf

UK_Lone_Wolf

New Member
Jul 29, 2019
5
0
0
Solved it! Probably not the ideal way to do it but it works, just added a shutdown command on the turtle.
Working code is below for everyone to have a play, use or make better.
Thankyou to casilleroatr Hydra and angelnc it may not seem like it but you have helped me alot.


###########
Turtle: (put this as a separate program for easiness of use, just put the move commands before the rednet command)
###########


turtle.turnLeft()
turtle.forward()
turtle.turnRight()
turtle.forward()

rednet.open("right")
local serverId = 3 -- Change to your server ID --


-- Do not change anything below this point --
local name = os.getComputerLabel()
local done = false
local myId = os.computerID()

while done == false do
rednet.send(serverId,name)
senderId,message,distance = rednet.receive(5)
if senderId == serverId then
if message == "Valid" then
print("Accepted")
rednet.close("right")
shell.run ("shutdown")
else
print("ID / Name Mismatch")
shell.run ("shutdown")
end
end
end

##############
Computer:
##############

local myid = os.computerID()
local turtlelist = { 0, 5, 6 } -- Enter the ID of all your Mining Turtles Here --
local turtlename = { "DIGGE01", "DIGGE02", "DIGGE03" } -- Enter the Name/ Label you have given your turtle here --

mon=peripheral.wrap("left")
print("Turtle Monitor Terminal")
rednet.open("top") -- Edit to what ever side you modem is on --


print("Computer id for Monitor Terminal is "..tostring(myid))

function findIndexForId(id)
for i,v in ipairs(turtlelist) do
if id == v then
return i
end
end
return 0
end

function checkTurtleForName(id,tlabel)
local i = findIndexForId(id)
if i == 0 then
return -1
end
if turtlename == tlabel then
return 1
else
return 0
end
end

local isValid = 0

while true do
local timeString = textutils.formatTime(os.time(),false)
senderId, message, distance = rednet.receive()
isValid = checkTurtleForName(senderId, message)

if isValid == -1 then
print("A Turtle"..senderId.." sent us a request but is not in our list")
elseif isValid == 1 then
rednet.send(senderId, "Valid")
mon.scroll(1)
mon.setCursorPos(1,5)
mon.write("Mining Complete: "..senderId.." "..message.." at "..timeString)
else
rednet.send(senderId, "Not Valid")
mon.scroll(1)
mon.setCursorPos(1,5)
mon.write("Error from "..senderId.." "..message.." at "..timeString)
end
end



Here is the result :
2013-09-12_011909.png