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

Networked Power Monitor

Started by logit667, 06 July 2015 - 05:44 AM
logit667 #1
Posted 06 July 2015 - 07:44 AM
V.1

I have been working on this for a while and figured I would put it here.
This power monitor consist of three programs
[1] Main controller (working)
[2] Power Probe (working)
[3] Display (In progress)

[1] The Main Controller
Spoiler
  • looks for new Power Probes and adds them to a array and removes them if they don't respond
  • reset the power net (with T-flipflop)
  • transmit power stats to off sit monitor (In progress)
[2] Power Probe
Spoiler
  • Gathers power info from power cell
  • Transmit info to main controller
[3] Display (In progress)
  • Will display transmitted info from Main controller
Just to let you know this is a rough draft of the programs the structure might be crappy but it runs.
I will be going through and refining the code as i have time.

Items You will need:
  • Computer
  • Disk Drive
  • Disk
  • Wireless Modem
  • Monitor
  • Power-Energy Cell (Thermal expansion) [only tested with this device so far]
  • To set up Gps sat
So here is the code

Gps:
Build Right above Main Computer
code is here(http://www.computerc...osition-system/)

Main:
V.1
Disk Startup code:
shell.run("disk/main")

Spoiler


function csvToTable(s)
  local tab = {}
  for match in string.gmatch(s, "[ t]*([^,]+)") do tab[#tab + 1] = match end
  return tab
end
function fromCSV (s)
s = s .. ','		-- ending comma
local t = {}		-- table to collect fields
local fieldstart = 1
repeat
-- next field is quoted? (start with `"'?)
  if string.find(s, '^"', fieldstart) then
   local a, c
   local i  = fieldstart
   repeat
	-- find closing quote
	a, i, c = string.find(s, '"("?)', i+1)
   until c ~= '"'	-- quote not followed by quote?
   if not i then error('unmatched "') end
   local f = string.sub(s, fieldstart+1, i-1)
   table.insert(t, (string.gsub(f, '""', '"')))
   fieldstart = string.find(s, ',', i) + 1
  else				-- unquoted; find next comma
   local nexti = string.find(s, ',', fieldstart)
   table.insert(t, string.sub(s, fieldstart, nexti-1))
   fieldstart = nexti + 1
  end
until fieldstart > string.len(s)
return t
end
os.sleep(5)
Computers = {}
rebootnet = false
tx,ty,tz=0
x,y,z= gps.locate(2)
while (true) do
rednet.open("top")
term.clear()
temp={}
term.setCursorPos(1,1)
print("Running...."..x..","..y..","..z)
senderId, message = rednet.receive(2)

if (senderId~=nil) then
  print("building table....")
  temp = fromCSV(message)
  print()
  print("building table....done")
  if (#Computers==nil) then
   print("com nil")
   Computers[0] = {}
   Computers[0].Id = senderId
   Computers[0].Io = message
   Computers[0].Cp = temp[1]
   Computers[0].Cs = temp[2]
   Computers[0].Cm = temp[3]
   Computers[0].Ix = 0
   Computers[0].Iy = 0
   Computers[0].Iz = 0
   rednet.close("top")
  else
   print("com check")
   intable = false
   for k, v in pairs(Computers) do
	if(v.Id==senderId) then
	 print("com Found")
	 intable = true
	 print("com info Update")
	 v.Io = message
	 v.Cp = temp[1]
	 v.Cs = temp[2]
	 v.Cm = temp[3]
	 v.Ix = tonumber(temp[4])-x
	 v.Iy = tonumber(temp[5])-y
	 v.Iz = tonumber(temp[6])-z
	end
	--print (k, v)
   end -- for loop
   if(intable==false) then
	print("com Not Found:"..temp[2]..","..x)
	write(type(temp[4]))
	tx=tonumber(temp[4])
				print(temp)
	ty=temp[5]-y
	tz=temp[6]-z
	print("com Not Found:"..tx..","..x)
	table.insert(Computers,{Id = senderId,Io = message,Cp = temp[1],Cs = temp[2],Cm = temp[3],Ix = tx,Iy = ty ,Iz = tz})
	print("com Added")
	rednet.broadcast(senderId..",OK")
	rednet.close("top")
   end
  end
else
  print("No Com Traffic")
  rednet.close("top")
end
os.sleep(0.1)
term.clear()
term.setCursorPos(1,1)


--table.sort (Computers,function (v1, v2) return v1.Id < v2.Id end )
noreply = false
Maxpower = 0
MaxpowerStored = 0
Cores = 0
for k, v in pairs(Computers) do
  Cores = Cores + 1
  term.clear()
  term.setCursorPos(1,1)
  rednet.open("top")
  print("Sent Update Request to:"..v.Id)
  for i = 1,10 do
   temp2={}
   rednet.broadcast(v.Id..",Update")
   --os.sleep(0.1)
   Rid , Mess = rednet.receive(2)
  
   if(Rid==v.Id)then
	temp2 = csvToTable(Mess)
	if (temp2[1]~= nil)then
	 print("Received Update From:"..v.Id)
	 print("Original message:"..v.Io)
	 print(temp2[1])
	 print("X="..v.Ix)
	 print("Y="..v.Iy)
	 print("Z="..v.Iz)
	 v.Cp = temp2[1]
	 v.Cs = temp2[2]
	 v.Cm = temp2 [3]
	 v.Io = Mess
	 Maxpower = Maxpower + tonumber(v.Cm)
	 MaxpowerStored = MaxpowerStored + tonumber(v.Cs)
	 print("New Message:"..v.Io)
	 print("Max Power = " .. Maxpower)
	 print("Max Stored Power = " .. MaxpowerStored)
	 os.sleep(0.2)
	 rednet.broadcast(v.Id..",OK")
	 rednet.close("top")
	 break
	else
	 print("Waiting for reply:"..i.."/10")
	end
   else
	print("Waiting for reply:"..i.."/10")
	if (i==10)then
	 print("Removing Node:"..v.Id)
	 table.remove(Computers,k)
	 noreply = true
	end
   end
   os.sleep(0.1)
  end
  if (noreply==true)then
   break
  end
  --os.sleep(0.5)
end

if (redstone.getInput("left") == true) then

  redstone.setOutput("right", true)
  os.sleep(1)
  redstone.setOutput("right", false)
  rebootnet = true
end

comcount = #Computers
if (rebootnet == true) then
  rednet.open("top")
  while ( #Computers~=0)do
   for k, v in pairs(Computers) do
	if (k==comcount)then
	 for i = 1,10 do
	  rednet.broadcast(v.Id..",Reboot")
	  Rid1 , Mess1 = rednet.receive(2)
	
	  if(Rid1==v.Id)then
	   print(Rid1.."/"..v.Id)
	   print("Rebooting com, "..Rid1)
	   --print(Mess)
	   --v.Io = Mess
	   rebootnet = false
	   print("Removing Node:"..v.Id)
	   table.remove(Computers,k)
	   --print("New Message:"..v.Io)
	   break
	  --else
	   --print("Waiting for reply:"..i.."/10")
	  
	  end
	
	  os.sleep(0.5)
	 end
	
	 break
	end
  
   end
  
   comcount = comcount-1
  end

end
rednet.close("top")

os.sleep(0.1)
end



Power Probe:
V.1

Disk Startup code:
os.sleep(10)
shell.run("disk/probe")

Spoiler

x,y,z= gps.locate(5)
function RANDBI()
NUM1 = math.random(6)
return NUM1
end
function netcheck()
while (DR==false) do
  term.clear()
  term.setCursorPos(1,1)
  rednet.open("top")
  rednet.broadcast("0"..",".."0"..",".."0"..","..x..","..y..","..z)
  print("Sent Announcement")
  print("Waiting")
  senderID, message = rednet.receive(5)
  if(senderID==nil)then
   DR = false
   print("No Reply Trying Again")
  else
   if(message==ID..",OK") then
	DR = true
	print("Reply Received From: "..senderID.." Message: "..message)
	controllerid = senderID
   else
	DR = false
	print("Reply But Not Me. Trying Again")
   end
  end
  rednet.close("top")
  os.sleep(RANDBI())
end
end
MSide = "top"
CSide = "back"
SChannel = 20
RChannel = 21
controllerid = 0
--Modem = peripheral.wrap(MSide)
Cell = peripheral.wrap(CSide)
CellFull = Cell.getMaxEnergyStored()
x,y,z= gps.locate(2)
ID = os.getComputerID()
DR = false
--x,y,z= gps.locate(2)
print("Its OK")
CounterCT=0
while (true) do
if (DR~=true)then
  netcheck()
end
rednet.open("top")
term.clear()
term.setCursorPos(1,1)
print("Waiting-Tries:"..CounterCT)
senderID, message = rednet.receive(1)
if (senderID ~= nil)then
  if (senderID==controllerid)then
   print("My Traffic")
   if(message==ID..",Update"==true)then
	print("Send Update")
	CellStored = Cell.getEnergyStored()
	CellPercent = math.floor(100*CellStored/CellFull)
	print("Sent:"..CellPercent.." "..CellStored.."/"..CellFull)
	rednet.broadcast(CellPercent..","..CellStored..","..CellFull)
	--print(CellFull)
	Rid, Mess = rednet.receive(2)
	rednet.close("top")
	CounterCT=0
	os.sleep(0.5)
   elseif(message==ID..",Reboot"==true)then
	print("Rebooting")
	rednet.broadcast("RemoveNow")
	Rid, Mess = rednet.receive(2)
	--rednet.send(controllerid,"RemoveNow")
	os.sleep(1)
	rednet.close("top")
	os.sleep(2)
	os.reboot()
   else
  
   end
  else
   print("Not My Traffic")
  end
else
  print("No Com Traffic: "..CounterCT)
  if (CounterCT == 25)then
   DR=false
   CounterCT=0
   rednet.close("top")
  end
  CounterCT=CounterCT+1
end

--os.sleep(0.1)
end

Display:
V.1 (In progress)

if you have any ideas or code fixes
Lyqyd #2
Posted 06 July 2015 - 08:36 AM
Just so you know, you can hook peripherals up via wired networking cables, so you wouldn't need quite so many computers.
logit667 #3
Posted 06 July 2015 - 05:49 PM
Thanks ill look in to that
logit667 #4
Posted 08 July 2015 - 12:48 AM
I am currently redoing the structure of peripherals and code via
Lua Liquidator
recommendations. i will post the updates soon