Advertisement
Shurhaian

ComputerCraft remote fluid sensor script

Feb 27th, 2013
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.54 KB | None | 0 0
  1. local ccomp=4 -- Controlling computer
  2. local modempos="left" -- Modem placement
  3. local senspos="right" -- Sensor placement
  4. local tankpos="-1,1,-1" -- Tank placement, relative to sensor
  5. local reqstring="DREQ" -- Data request string
  6. local dbgr=false -- Debug mode
  7. local version="1.7" -- Version number, as string in case of finer point releases
  8.  
  9. print ("Remote Fluid Sensor v"..version.."\nBy Shurhaian\n")
  10.  
  11. os.loadAPI("ocs/apis/sensor") -- Load the sensors API
  12. if dbgr then
  13.   print ("Debug mode active\nSensors API loaded")
  14.   sleep (0.5)
  15. end
  16. tank=sensor.wrap(senspos) -- ...and the tank sensor
  17. if dbgr then
  18.   print ("Sensor wrapped")
  19.   sleep (0.5)
  20. end
  21.  
  22. rednet.open(modempos) -- Open Rednet
  23. if dbgr then
  24.   print ("Channel open")
  25.   sleep (0.5)
  26. end
  27.  
  28. local compID=os.getComputerID()
  29. print ("Modem position: "..modempos.."\nSensor position: "..senspos.."\nTank location: "..tankpos.."\n")
  30. print ("Controlling computer: #"..ccomp.."\nRunning on computer: #"..compID.."\n")
  31.  
  32. local tankrep=nil -- Tank details
  33. local fluid=0 -- Fluid amount, declared in advance
  34.  
  35. if dbgr then
  36.   print ("Fetching tank details...\n")
  37.   tankrep=tank.getTargetDetails(tankpos)
  38.   print ("Details:\n-----\n"..textutils.serialize(tankrep).."\n-----")
  39.   fluid=tankrep.Tanks[1].Amount
  40.   print ("fluid amount: "..fluid)
  41. else
  42.   print ("Now active and listening...")
  43. end
  44.  
  45. while not dbgr do
  46.   message,id=myNet.receive()
  47.   if id==ccomp and message==reqstring then
  48.     tankrep=tank.getTargetDetails(tankpos)
  49.     fluid=tankrep.Tanks[1].Amount
  50.     myNet.send(fluid,ccomp)
  51.   end
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement