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

Rednet sending a command. What am i missing?

Started by thedriver87, 02 February 2013 - 03:59 PM
thedriver87 #1
Posted 02 February 2013 - 04:59 PM
So i have the following code for the purpose of sending a redstone signal either solid state or pulsing out the bottom of a computer when it gets the message "[WAREHOUSE] Toggle RS" or "[WAREHOUSE] Pulse RS ON/OFF" from another computer.


local function findPeripheral( pType, errMsg )
  for _, v in pairs( rs.getSides() ) do
    if peripheral.isPresent( v ) and peripheral.getType( v ) == pType then
	  return v
    end
  end
  print( "Cannot find a "..errMsg or pType.." attached to this computer " )
  error()
end
local modemSide = findPeripheral( "modem" )
rednet.open( modemSide )
local pulseDuration = nil
local pulseTime = os.startTimer( duration or 0.5 )
local pulseActive = false
while true do
  local event = { os.pullEventRaw( ) }
  if event[1] == "rednet_message" then
    print( "Message received" )
    local msg = textutils.unserialize( event[3] )
    if msg["target"] == "[WAREHOUSE]" then
	  local action = msg["action"]
	  if action == "Toggle RS" then
	    rs.setOutput( "bottom", not rs.getInput( "bottom" ) )
	  elseif action == "Pulse RS ON" then
	    pulseDuration = tonumber( msg["duration"] )
	    pulseActive = true
	    pulseTime = os.startTimer( duration or 0.5 )
	  elseif action == "Pulse RS OFF" then
	    pulseActive = false
	    rs.setOutput( "bottom", false )
	  end
    end
  elseif event[1] == "timer" and event[2] == pulseTime and pulseActive then
    rs.setOutput( "bottom", not rs.getInput( "bottom" ) )
    pulseTime = os.startTimer( pulseDuration or 0.5 )
  end
end
rednet.close( modemSide )

however when i run another computer set the rednet and boradcast the following in the lua prompt: rednet.broadcast( "[WAREHOUSE] Toggle RS") and hit enter the computer returns true and the drone computer returns message received, but no redstone pulse or redstone solid state.. i'm pretty sure the rs.setOutput is written properly and im almost certain the message is being read properly…. what am i missing?
raineth #2
Posted 02 February 2013 - 05:22 PM
however when i run another computer set the rednet and boradcast the following in the lua prompt: rednet.broadcast( "[WAREHOUSE] Toggle RS") and hit enter the computer returns true and the drone computer returns message received, but no redstone pulse or redstone solid state.. i'm pretty sure the rs.setOutput is written properly and im almost certain the message is being read properly…. what am i missing?
Why are you sending plain text and expecting it to deserialize into a table? Is this your code, or someone else's that you're trying to send messages to?
Conn332 #3
Posted 03 February 2013 - 04:24 AM
instead of have os.pullEvent() equaling just event have it equal event, ID, message and just test if message is equal to [Warehouse] pulse or ON/OFF
theoriginalbit #4
Posted 03 February 2013 - 10:11 AM
Because when I wrote this code the message format was intended like so…


rednet.broadcast(textutils.serialize({target="[WAREHOUSE]", action="some action"}))


instead of have os.pullEvent() equaling just event have it equal event, ID, message and just test if message is equal to [Warehouse] pulse or ON/OFF
Wouldn't work… If you didn't notice the os.pullEvent() has {} around it meaning its returned data in a table. So
local event, ID, message = {os.pullEvent()}
Message AND ID would always be nil.
thedriver87 #5
Posted 03 February 2013 - 12:05 PM
and there is the answer.