Jump to content




Automatic running programs

computer media peripheral

10 replies to this topic

#1 xTheChosen0ne

  • Members
  • 12 posts

Posted 18 June 2015 - 07:07 PM

Hey guys. I made a program, which sends per RedNet a message to all computers when a mfe have less than 10000 EU. Now my question: What i have to do, that the programs (sending message and receiving message) work automatically?

#2 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 18 June 2015 - 07:38 PM

name the programs startup on each computer and they will automatically start running when the computers boot

#3 xTheChosen0ne

  • Members
  • 12 posts

Posted 18 June 2015 - 08:15 PM

View PostDog, on 18 June 2015 - 07:38 PM, said:

name the programs startup on each computer and they will automatically start running when the computers boot
This dont works, because it should use a loop. so its needed to reboot everytime. but thats the problem: if i make rs.setOutput("left", true) for example, the redstone signal goes off in the time, when the pc is rebooting

#4 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 18 June 2015 - 08:31 PM

View PostxTheChosen0ne, on 18 June 2015 - 08:15 PM, said:

This dont works...
Why does naming the programs 'startup' not work? Are they not starting automatically?

View PostxTheChosen0ne, on 18 June 2015 - 08:15 PM, said:

because it should use a loop. so its needed to reboot everytime. but thats the problem: if i make rs.setOutput("left", true) for example, the redstone signal goes off in the time, when the pc is rebooting
If your program requires a loop, then that's what you should use. You shouldn't need to reboot each time. It sounds like the problem is how you've structured your code. Post your code and I (or somebody) will help you rework the code to use a loop instead of requiring a reboot each time.

Edited by Dog, 18 June 2015 - 08:37 PM.


#5 xTheChosen0ne

  • Members
  • 12 posts

Posted 18 June 2015 - 09:13 PM

View PostDog, on 18 June 2015 - 08:31 PM, said:

View PostxTheChosen0ne, on 18 June 2015 - 08:15 PM, said:

This dont works...
Why does naming the programs 'startup' not work? Are they not starting automatically?

View PostxTheChosen0ne, on 18 June 2015 - 08:15 PM, said:

because it should use a loop. so its needed to reboot everytime. but thats the problem: if i make rs.setOutput("left", true) for example, the redstone signal goes off in the time, when the pc is rebooting
If your program requires a loop, then that's what you should use. You shouldn't need to reboot each time. It sounds like the problem is how you've structured your code. Post your code and I (or somebody) will help you rework the code to use a loop instead of requiring a reboot each time.

When i simply make "shell.run(program name)" at the end of the loop, it works fine first, but after a few minutes there is a java problem. I "solved" the problem like this, with reboot:
Computer at MFE:
os.loadAPI("ocs/apis/sensor")
mySensor = sensor.wrap("top")
position = "0,0,-1"
details = mySensor.getTargetDetails(position)
kapazitaet = details.Capacity
stand = details.Stored
grenze = 30000
schleife = 1
while schleife > 0 do
  rednet.open("right")
  if stand <= grenze then
	rednet. broadcast("alarm")
  elseif stand > grenze then
	rednet.broadcast("ok")
  end
sleep(1)
shell.run("reboot")
end

My Computer, which receive the messages and react on:

rednet.open("bottom")
id, message = rednet.receive()
write ("Status:  ")
if id == 15 and message == "alarm" then
  rs.setOutput("back", true)
  print("Kritisch. Eigenversorgung aktiviert.")
end
if id == 15 and message == "ok" then
  rs.setOutput("back", false)
  print("OK")
end
sleep(3)
shell.run("reboot")

Both programs are made as startup

Edited by xTheChosen0ne, 18 June 2015 - 09:21 PM.


#6 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 18 June 2015 - 09:57 PM

OK, so what you need to do is encapsulate your 'repeating' code in an infinite loop for each program so you don't need to reboot. You have an infinite loop in your MFE program (while schleife > 0 do), but it's in the wrong place and not really helping.

Let's start with the MFE program. First, you don't need to specify a value and compare that value to create an infinite loop. Instead of this...
schleife = 1
while schleife > 0 do
  --# code...
end

You can just do this
while true do
  --# code
end

Next all the code that needs to repeat should be moved inside that loop. Think about which lines of code need to only be run once, and which ones need to repeat over and over. You want to put the non-repeating code above the loop and all the repeating code inside the loop. Try making those changes to your MFE program (we'll get to the second program after we finish the first) and post your results. We'll go from there.

Edited by Dog, 18 June 2015 - 10:01 PM.


#7 xTheChosen0ne

  • Members
  • 12 posts

Posted 19 June 2015 - 08:53 AM

View PostDog, on 18 June 2015 - 09:57 PM, said:

OK, so what you need to do is encapsulate your 'repeating' code in an infinite loop for each program so you don't need to reboot. You have an infinite loop in your MFE program (while schleife > 0 do), but it's in the wrong place and not really helping.

Let's start with the MFE program. First, you don't need to specify a value and compare that value to create an infinite loop. Instead of this...
schleife = 1
while schleife > 0 do
  --# code...
end

You can just do this
while true do
  --# code
end

Next all the code that needs to repeat should be moved inside that loop. Think about which lines of code need to only be run once, and which ones need to repeat over and over. You want to put the non-repeating code above the loop and all the repeating code inside the loop. Try making those changes to your MFE program (we'll get to the second program after we finish the first) and post your results. We'll go from there.

Well, first, it seemed to work. But there is following problem: He just test the status, when i activate the program. So, when the MFE has more than 30000 EU at the moment, when i start the program, it ALWAYS send "ok" (also, when he gets under 30000 meanwhile). And when the MFE has equal or less than 30000 EU at the moment, when i start the program, it ALWAYS send "alarm" (also, when he gets 30000+ meanwhile).
That's not what i want to reach. The loop itself seems to work.
My code:
os.loadAPI("ocs/apis/sensor")
mySensor = sensor.wrap("top")
position = "0,0,-1"
details = mySensor.getTargetDetails(position)
kapazitaet = details.Capacity
stand = details.Stored
grenze = 30000
rednet.open("right")
while true do
  rednet.open("right")
  if stand <= grenze then
	 rednet. broadcast("alarm")
	 sleep(1)
  end
  if stand > grenze then
	rednet.broadcast("ok")
	sleep(1)
  end
end

Edited by xTheChosen0ne, 19 June 2015 - 09:31 AM.


#8 xTheChosen0ne

  • Members
  • 12 posts

Posted 19 June 2015 - 10:25 AM

I founded the problem! I started the infinite loop at the wrong place, so he just checked 1 time, how much EU was Stored. Sorry for beeing noob in LUA :D And i want to study IT... that will be fun lol.
However, thats the working code for the MFE-Computer now!:

os.loadAPI("ocs/apis/sensor")
mySensor = sensor.wrap("top")
position = "0,0,-1"
while true do
  details = mySensor.getTargetDetails(position)
  kapazitaet = details.Capacity
  stand = details.Stored
  grenze = 30000
  rednet.open("right")
  if stand <= grenze then
		 rednet. broadcast("alarm")
		 sleep(1)
  end
  if stand > grenze then
		rednet.broadcast("ok")
		sleep(1)
  end
end

How's about the 2nd program now? :)

Edited by xTheChosen0ne, 19 June 2015 - 11:00 AM.


#9 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 19 June 2015 - 02:49 PM

Great job! A couple of small changes: move rednet.open("right") above the loop - you only need to do that once. You could also move grenze = 300000 above the loop since the value never changes.

Now, the same applies to the second program. You don't want it to reboot, but you want it to run forever. So, think about which code needs to run once and which code needs to be in the loop. You'll use the same thing (while true do) you did last time.

#10 xTheChosen0ne

  • Members
  • 12 posts

Posted 19 June 2015 - 05:32 PM

View PostDog, on 19 June 2015 - 02:49 PM, said:

Great job! A couple of small changes: move rednet.open("right") above the loop - you only need to do that once. You could also move grenze = 300000 above the loop since the value never changes.

Now, the same applies to the second program. You don't want it to reboot, but you want it to run forever. So, think about which code needs to run once and which code needs to be in the loop. You'll use the same thing (while true do) you did last time.

Alright, big thank you to you :D All is working like it should, now, thank you :)

#11 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 19 June 2015 - 06:37 PM

You're very welcome - glad to help :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users