1
0
mirror of https://github.com/DigitalDevices/octonet.git synced 2023-10-10 13:36:52 +02:00

Added temperature monitor and fan control

This commit is contained in:
mvoelkel 2015-09-05 18:40:13 +02:00
parent 72d183c3e6
commit b22b47c0b0
4 changed files with 403 additions and 2 deletions

View File

@ -0,0 +1,131 @@
#!/usr/bin/lua
local socket = require("socket")
local devicepath = "/sys/class/ddbridge/ddbridge0"
local interval = 30
local nValues = 2880
local LogFile = "/tmp/Temperatur.log"
local HighTemp = 50
local LowTemp = 45
function ReadTemp(sensor)
local temp = 0
local file = io.open(devicepath.."/temp"..sensor)
if file then
temp = file:read()
if temp == "no sensor" then
temp = 0
else
temp = math.floor(tonumber(temp)/1000)
end
file:close()
end
return temp
end
function sleep(sec)
socket.select(nil,nil,sec)
end
function SetFan(value)
local gpio = io.open("/sys/class/gpio/gpio106/value","w");
if gpio then
gpio:write(value)
gpio:close()
end
end
local gpio = io.open("/sys/class/gpio/export","w");
if gpio then
gpio:write("106")
gpio:close()
end
local NumSensors = 0
local i
local temps
local temp
local Sensor = {}
local TempList = {}
local StartIndex = 1
local count = interval
local fanstate = 1
--~ sleep(30)
for i = 0,4,1 do
temp = ReadTemp(i)
if temp > 0 then
Sensor[NumSensors] = i
NumSensors = NumSensors + 1
end
end
if NumSensors == 0 then
return
end
SetFan(0)
while true do
sleep(1)
temps = ""
for i = 0, NumSensors - 1, 1 do
temp = ReadTemp(Sensor[i])
if temp == 0 then
print(" fanControl Error ")
return
end
if i == 0 then
temps = temp
else
temps = temps .. "," .. temp
end
if fanstate == 0 and temp >= HighTemp then
SetFan(1)
fanstate = 1
elseif fanstate == 1 and temp < LowTemp then
SetFan(0)
fanstate = 0
end
end
count = count - 1
if count == 0 then
count = interval
if #TempList < nValues then
TempList[#TempList+1] = temps
else
TempList[StartIndex] = temps
StartIndex = StartIndex + 1
if StartIndex > nValues then StartIndex = 1 end
end
TmpLogFile = os.tmpname()
local fh = io.open(TmpLogFile,"w")
if fh then
fh:write(NumSensors..","..interval..","..fanstate.."\n")
for i = StartIndex-1, 1, -1 do
fh:write(TempList[i].."\n")
end
for i = #TempList, StartIndex, -1 do
fh:write(TempList[i].."\n")
end
fh:close()
os.remove(LogFile)
os.rename(TmpLogFile,LogFile)
end
end
collectgarbage()
end

View File

@ -48,8 +48,12 @@ MenuItems[10].Text = "Reboot";
MenuItems[10].Link = "reboot.html"; MenuItems[10].Link = "reboot.html";
MenuItems[11] = new Object(); MenuItems[11] = new Object();
MenuItems[11].Text = "Licenses"; MenuItems[11].Text = "Hardware Monitor";
MenuItems[11].Link = "licenses.html"; MenuItems[11].Link = "monitor.html";
MenuItems[12] = new Object();
MenuItems[12].Text = "Licenses";
MenuItems[12].Link = "licenses.html";
// add additional items here // add additional items here

View File

@ -0,0 +1,166 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OctopusNet</title>
<link rel="stylesheet" type="text/css" href="/style.css">
<script type="text/javascript" src="/menu.js"></script>
<!-- Add included scripts here -->
<!-- Add page scripts here -->
<script type="text/javascript">
var xmlhttp = new XMLHttpRequest();
var url = "/monitor.lua";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
function Reload()
{
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function myFunction(response) {
var Sensor = JSON.parse(response);
if( Sensor.NumSensors > 0 )
{
var pts = document.getElementById("pts").points;
var svgRoot = document.getElementById("svg");
var i;
var v = Sensor.SensorData[0];
var n = v.length;
var i0 = 0;
if( n > 120 )
{
i0 = n - 120;
n = 120;
}
pts.clear();
for(i = 0; i < n; i += 1)
{
var pt = svgRoot.createSVGPoint();
pt.x = (i+1) * 20;
pt.y = v[i+i0] * 10;
pts.appendItem(pt);
}
window.setTimeout(Reload,10000);
}
}
function OnLoad()
{
window.setTimeout(Reload,1000);
}
</script>
</head>
<body onload="OnLoad()">
<table class="maintable" align="center">
<colgroup>
<col width="182px"/>
<col width="728px"/>
</colgroup>
<tr><td class="maintd" colspan="2">
<a href="http://www.digitaldevices.de"><img src="/BannerDD.jpg" alt="DD" width="910" height="130" /></a>
</td></tr>
<tr><td class="maintd" colspan="2">&nbsp;</td></tr>
<tr>
<td class="maintd"><script type="text/javascript">CreateMenu();</script></td>
<td class="content">
<div>
<!-- Begin Content -->
<!--
<table>
<td colspan="2" align="right">
<div style="width: 300px">
<form action="">
<input type="Button" value="Reload" onclick="Reload()" >
</form>
</div>
</td>
</table>
-->
<h3 align="center">Frontend Temperature</h3>
<div align="center">
<svg Id="svg" width="720px" height="330px">
<desc>Temperature</desc>
<g transform="scale(0.277)">
<!-- Now Draw the main X and Y axis -->
<rect width="2400" height="1000" x="100" y="100" fill="#FFFFFF" />
<g style="stroke-width:5; stroke:black">
<!-- X Axis -->
<path d="M 100 1100 L 2500 1100 Z"/>
<!-- Y Axis -->
<path d="M 100 100 L 100 1100 Z"/>
</g>
<!-- Now add some dashes in as a guide -->
<g style="fill:none; stroke:#B0B0B0; stroke-width:2; stroke-dasharray:2 4;text-anchor:end;" font-size="35">
<path d="M 102 1100 L 2500 1100 Z"/>
<text style="fill:black; stroke:none" x="90" y="1030">10</text>
<path d="M 102 1000 L 2500 1000 Z"/>
<text style="fill:black; stroke:none" x="90" y="930">20</text>
<path d="M 102 900 L 2500 900 Z"/>
<text style="fill:black; stroke:none" x="90" y="830">30</text>
<path d="M 102 800 L 2500 800 Z"/>
<text style="fill:black; stroke:none" x="90" y="730">40</text>
<path d="M 102 700 L 2500 700 Z"/>
<text style="fill:black; stroke:none" x="90" y="630">50</text>
<path d="M 102 600 L 2500 600 Z"/>
<text style="fill:black; stroke:none" x="90" y="530">60</text>
<path d="M 102 500 L 2500 500 Z"/>
<text style="fill:black; stroke:none" x="90" y="430">70</text>
<path d="M 102 400 L 2500 400 Z"/>
<text style="fill:black; stroke:none" x="90" y="330">80</text>
<path d="M 102 300 L 2500 300 Z"/>
<text style="fill:black; stroke:none" x="90" y="230">90</text>
<path d="M 102 200 L 2500 200 Z"/>
<text style="fill:black; stroke:none" x="90" y="130">100</text>
<text style="fill:black; stroke:none" x="100" y="60">&deg;C</text>
<path d="M 102 100 L 2500 100 Z"/>
</g>
<g style="fill:none; stroke:#B0B0B0; stroke-width:2; stroke-dasharray:2 4">
<path d="M 2500 1100 L 2500 100 Z"/>
<!--<text style="fill:black; stroke:none" x="2500" y="1125">0</text>-->
<path d="M 2100 1100 L 2100 100 Z"/>
<!--<text style="fill:black; stroke:none" x="2100" y="1125">-10</text>-->
<path d="M 1700 1100 L 1700 100 Z"/>
<!--<text style="fill:black; stroke:none" x="1700" y="1125">-20</text>-->
<path d="M 1300 1100 L 1300 100 Z"/>
<!--<text style="fill:black; stroke:none" x="1300" y="1125">-30</text>-->
<path d="M 900 1100 L 900 100 Z"/>
<!--<text style="fill:black; stroke:none" x="900" y="1125">-40</text>-->
<path d="M 500 1100 L 500 100 Z"/>
<!--<text style="fill:black; stroke:none" x="500" y="1125">-50</text>-->
<!--<text style="fill:black; stroke:none" x="1100" y="1140">Minutes</text>-->
</g>
<svg x="100" y="100" width="2400" height="1000">
<g transform="scale(1.0,-1.0) translate(0.0,-1000)">
<polyline Id="pts"
points="0 0,"
style="stroke:red; stroke-width: 3; fill : none;"/>
</g>
</svg>
</g>
</svg>
</div>
<!-- End Content -->
</div>
</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
</table>
</body>
</html>

View File

@ -0,0 +1,100 @@
#!/usr/bin/lua
local host = os.getenv("HTTP_HOST")
local proto = os.getenv("SERVER_PROTOCOL")
local query = os.getenv("QUERY_STRING")
local method = os.getenv("REQUEST_METHOD")
local clength = os.getenv("CONTENT_LENGTH")
local ctype = os.getenv("CONTENT_TYPE")
function http_print(s)
if s then
io.stdout:write(tostring(s).."\r\n")
else
io.stdout:write("\r\n")
end
end
function SendError(err,desc)
http_print(proto.." "..err)
http_print()
local file = io.open("e404.html")
if file then
local tmp = file:read("*a")
tmp = string.gsub(tmp,"404 Not Found",err .. " " .. desc)
http_print(tmp)
file:close()
end
end
function ReadLine(file)
local tmp = file:read("*l")
if tmp then
local values = {}
local value
for value in string.gmatch(tmp,"(%d+)") do
table.insert(values,tonumber(value))
end
return values
else
return nil
end
end
if method == "GET" then
data = ""
local file = io.open("/tmp/Temperatur.log")
if file then
local NumSensors,Interval,FanState,d = unpack(ReadLine(file))
local Values = {}
while true do
local tmp = ReadLine(file)
if tmp then
table.insert(Values,tmp)
else
break
end
end
file:close()
data = "{\n"
data = data .. "\"NumSensors\":\""..NumSensors.."\",\n"
data = data .. "\"Interval\":\""..Interval.."\",\n"
data = data .. "\"FanState\":\""..FanState.."\",\n"
data = data .. "\"SensorData\": [\n"
local i,j
for i = 1,NumSensors,1 do
data = data .. "["
for j = #Values,1,-1 do
local tmp = Values[j]
data = data .. "\"" .. tmp[i] .. "\""
if j > 1 then
data = data .. ","
end
end
if i < NumSensors then
data = data .. "],\n"
else
data = data .. "]\n"
end
end
data = data .. "]\n"
data = data .. "}\n"
end
http_print(proto.." 200" )
http_print("Pragma: no-cache")
http_print("Content-Type: application/json; charset=UTF-8")
http_print(string.format("Content-Length: %d",#data))
http_print()
http_print(data)
else
SendError("500","What")
end