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

Redstone and Rednet Server Using Files (Direwolf20 1.7)

Started by QuiveringT, 20 June 2016 - 12:43 AM
QuiveringT #1
Posted 20 June 2016 - 02:43 AM
I am building a server that interprets redstone and rednet signal from two computers and
then returns a redstone/rednet signal to these computers. I am using Bundled Cables as
well as Wireless Redstone, but they are not directly necessary.

There are two files named 'initLeft' and 'initRight' in the server that are used to store the value of the redstone or rednet signal coming from the left or right computers respectively. For some reason the functions pertaining to the right side of the system writes the text 'true' into 'initRight' when being sent 'whiteon' or by typing the text directly into the file, and therefore produces the 'text sucks.' message. I need the file to have either 'whiteon' or 'whiteoff' written in it in order for the rest of the system to work and be read by the functions.

Note 1: This is intended to be running many more functions similar to these at all times, so I can control machines that are l located in my house, from a far away location (including different dimensions).

Note 2: This also functions as a file initialization system which counteracts the fact that computers turn off and forget their redstone signal when the Minecraft Server or singleplayer world is shutdown. This is essential to my build.

Thanks in advance.

Spoiler
function receiveLeft()
  dir = 'initLeft'
  while true do
	 os.pullEvent('redstone')
	 state = rs.testBundledInput('back', colors.white)
	 file = fs.open(dir, 'w')
	 file.write(state)
	 file.close()
	 sleep(0)
   end  
end

function setLeft()
dir = 'initLeft'
  while true do
	file = fs.open(dir, 'r')
	text = file.readAll()
	if text == 'true' then
	  rs.setBundledOutput('back',2)
	  elseif text == 'false' then
	  rs.setBundledOutput('back',0)
	end
	sleep(0)
  end
end

function receiveRight()
  dir = '/initRight'
  rednet.open('top')
  while true do
	event,id,message = os.pullEvent('rednet_message')
	file = fs.open(dir, 'w')
	file.write(message)
	file.close()
	sleep(0)
  end
end

function setRight()
  while true do
	dir = '/initRight'
	file = fs.open(dir,'r')
	text = file.readAll()
	file.close()
	if text == 'whiteon' then
	  rs.setBundledOutput('back',2)
	  elseif text == 'whiteoff' then
	  rs.setBundledOutput('back',0)
	  else print('text sucks.')
	end
	sleep(0)
  end
end


–while true do
– receiveRight()
– parallel.waitForAll(setRight, checkLeft)
–end
parallel.waitForAll(receiveRight, receiveLeft, setRight, setLeft)


Code for Left Computer
SpoilerMain

parallel.waitForAll(function() return shell.run('redSend') end, function() shell.run('redReceive') end)

redSend

while true do
  input = ''
  input = read()
  state = rs.getBundledOutput('back', colors.white)
  if input == 'send' and state == 0 then
	redstone.setBundledOutput('back', 1)
	print('yay')
	elseif input == 'send' and state == 1 then
	redstone.setBundledOutput('back',0)
  end
  input = ''
end


redReceive

while true do  
  state = rs.testBundledInput('back', 2)
  if state == false then
	--print(state)
	rs.setBundledOutput('back',0)
	else
	--print(state)
	rs.setBundledOutput('back', 1)
  end
  sleep(1)
end


The code for the right computer is a simple rednet message sender, sending either 'whiteon' or 'whiteoff'.
Edited on 20 June 2016 - 04:43 AM
Emma #2
Posted 20 June 2016 - 03:15 AM
Can you please post the code for the left/right computers. Also, put your code in spoilers please.
Do spoilers like so

[spoiler]
--Your Code Here
[/spoiler]
Bomb Bloke #3
Posted 20 June 2016 - 08:40 AM
I still don't see any code here which sends the text "whiteon" via rednet.

Your receiveLeft() / setLeft() functions are going to end up writing to and checking "/initRight", as your receiveRight() / setRight() are assigning that value to an unlocalised "dir" variable. Take a look into scope.

There's no obvious reason not to merge your set functions into your receive functions. Sure, write to disk whenever redstone input changes; but don't bother reading it back unless the script has just restarted!

Your use of sleep(0) in your receive functions may prevent you from detecting all redstone state changes / rednet messages. Ditch it.

rs.getBundledOutput() only requires one argument, and ignores any extras you choose to add.
QuiveringT #4
Posted 21 June 2016 - 07:40 AM
I still don't see any code here which sends the text "whiteon" via rednet.

Your receiveLeft() / setLeft() functions are going to end up writing to and checking "/initRight", as your receiveRight() / setRight() are assigning that value to an unlocalised "dir" variable. Take a look into scope.

There's no obvious reason not to merge your set functions into your receive functions. Sure, write to disk whenever redstone input changes; but don't bother reading it back unless the script has just restarted!

Your use of sleep(0) in your receive functions may prevent you from detecting all redstone state changes / rednet messages. Ditch it.

rs.getBundledOutput() only requires one argument, and ignores any extras you choose to add.

Turns out it was my unlocalised variables that were the issue. I've written a few CC programs but never had to use them before, thanks. I might have some more problems as I develop the system but I appreciate the help so far.