Advertisement
DanFrmSpace

solar_monitor_wireless

Jun 3rd, 2025 (edited)
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.75 KB | None | 0 0
  1. -- Simple Solar Monitor - Shows actual available data
  2. local modem = peripheral.find("modem")
  3. local monitor = peripheral.find("monitor")
  4.  
  5. if not modem then
  6.     print("No modem found!")
  7.     return
  8. end
  9.  
  10. if monitor then
  11.     monitor.setTextScale(0.5)
  12.     monitor.clear()
  13.     monitor.setBackgroundColor(colors.black)
  14.     monitor.setTextColor(colors.white)
  15. end
  16.  
  17. modem.open(101)
  18. local solarData = {}
  19. local lastUpdate = {}
  20.  
  21. function formatNumber(num)
  22.     if not num then return "0" end
  23.     if num >= 1000000 then
  24.         return string.format("%.1fM", num / 1000000)
  25.     elseif num >= 1000 then
  26.         return string.format("%.1fK", num / 1000)
  27.     else
  28.         return tostring(math.floor(num))
  29.     end
  30. end
  31.  
  32. function requestData()
  33.     modem.transmit(100, 101, "REQUEST_DATA")
  34. end
  35.  
  36. function updateDisplay()
  37.     if not monitor then return end
  38.    
  39.     monitor.clear()
  40.     local w, h = monitor.getSize()
  41.    
  42.     -- Header
  43.     monitor.setCursorPos(1, 1)
  44.     monitor.setTextColor(colors.yellow)
  45.     monitor.write("SOLAR PANEL MONITORING")
  46.    
  47.     monitor.setCursorPos(1, 2)
  48.     monitor.setTextColor(colors.white)
  49.     monitor.write("Time: " .. textutils.formatTime(os.time(), false))
  50.     monitor.write("  |  Active Panels: " .. #solarData)
  51.    
  52.     monitor.setCursorPos(1, 3)
  53.     monitor.write(string.rep("-", w))
  54.    
  55.     -- Calculate totals
  56.     local totalEnergy = 0
  57.     local totalCapacity = 0
  58.     local totalNeeded = 0
  59.    
  60.     for _, data in pairs(solarData) do
  61.         totalEnergy = totalEnergy + (data.energy or 0)
  62.         totalCapacity = totalCapacity + (data.maxEnergy or 0)
  63.         totalNeeded = totalNeeded + (data.energyNeeded or 0)
  64.     end
  65.    
  66.     -- System totals
  67.     monitor.setCursorPos(1, 5)
  68.     monitor.setTextColor(colors.lightBlue)
  69.     monitor.write("SYSTEM TOTALS:")
  70.    
  71.     monitor.setCursorPos(1, 6)
  72.     monitor.setTextColor(colors.white)
  73.     monitor.write("Total Energy Stored: " .. formatNumber(totalEnergy) .. " FE")
  74.    
  75.     monitor.setCursorPos(1, 7)
  76.     monitor.write("Total Capacity: " .. formatNumber(totalCapacity) .. " FE")
  77.    
  78.     monitor.setCursorPos(1, 8)
  79.     monitor.write("Total Space Available: " .. formatNumber(totalNeeded) .. " FE")
  80.    
  81.     if totalCapacity > 0 then
  82.         local percent = (totalEnergy / totalCapacity) * 100
  83.         monitor.setCursorPos(1, 9)
  84.         monitor.write("System Full: " .. string.format("%.1f%%", percent))
  85.     end
  86.    
  87.     -- Individual panels
  88.     monitor.setCursorPos(1, 11)
  89.     monitor.setTextColor(colors.lightBlue)
  90.     monitor.write("INDIVIDUAL PANELS:")
  91.    
  92.     monitor.setCursorPos(1, 12)
  93.     monitor.setTextColor(colors.white)
  94.     monitor.write("ID   Energy Stored    Capacity     Available    Full%")
  95.     monitor.setCursorPos(1, 13)
  96.     monitor.write(string.rep("-", w))
  97.    
  98.     -- Sort panels by ID
  99.     local sortedPanels = {}
  100.     for id, data in pairs(solarData) do
  101.         table.insert(sortedPanels, {id = id, data = data})
  102.     end
  103.     table.sort(sortedPanels, function(a, b) return a.id < b.id end)
  104.    
  105.     local line = 14
  106.     for _, panel in pairs(sortedPanels) do
  107.         local id = panel.id
  108.         local data = panel.data
  109.        
  110.         monitor.setCursorPos(1, line)
  111.         monitor.setTextColor(colors.white)
  112.        
  113.         local energy = data.energy or 0
  114.         local maxEnergy = data.maxEnergy or 0
  115.         local needed = data.energyNeeded or 0
  116.         local percent = maxEnergy > 0 and (energy / maxEnergy) * 100 or 0
  117.        
  118.         -- Color code based on fullness
  119.         if percent >= 95 then
  120.             monitor.setTextColor(colors.red)
  121.         elseif percent >= 80 then
  122.             monitor.setTextColor(colors.orange)
  123.         else
  124.             monitor.setTextColor(colors.lime)
  125.         end
  126.        
  127.         local line_text = string.format("%-4s %-15s %-12s %-12s %.1f%%",
  128.             id,
  129.             formatNumber(energy) .. " FE",
  130.             formatNumber(maxEnergy) .. " FE",
  131.             formatNumber(needed) .. " FE",
  132.             percent)
  133.        
  134.         monitor.write(line_text)
  135.         line = line + 1
  136.     end
  137.    
  138.     monitor.setTextColor(colors.white)
  139. end
  140.  
  141. -- Main loop
  142. print("Starting Solar Monitor...")
  143.  
  144. while true do
  145.     requestData()
  146.    
  147.     -- Listen for responses
  148.     local timeout = os.startTimer(2)
  149.    
  150.     while true do
  151.         local event, p1, p2, p3, p4 = os.pullEvent()
  152.        
  153.         if event == "timer" and p1 == timeout then
  154.             break
  155.         elseif event == "modem_message" then
  156.             local channel, data = p2, p4
  157.             if channel == 101 and type(data) == "table" and data.id then
  158.                 solarData[data.id] = data
  159.                 lastUpdate[data.id] = os.time()
  160.             end
  161.         end
  162.     end
  163.    
  164.     updateDisplay()
  165.     sleep(3)
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement