timeout
[svn42.git] / sensor_graph / sample_sensor.lua
1 require('os')
2 require('string')
3
4 last_temp = 0.0
5 last_light = 0
6 last_movement = 0
7
8 function save_values()
9   os.execute(string.format("rrdtool update /home/sensordata.rrd -t temp:light:movement N:%f:%d:%d", last_temp, last_light, last_movement))
10   last_movement=0
11 end
12
13
14 function parse_value(str)
15   if string.find(str,"Temp C:") then
16     last_temp = tonumber(string.sub(str,8))
17     --print(string.format("t: %f Grad Celsius",last_temp))
18   end
19   if string.find(str,"Photo:") then
20     last_light = tonumber(string.sub(str,7))
21     --print(string.format("p: %d",last_light))
22   end
23   if string.find(str,"movement") then
24    last_movement=1
25    --print "something moved"
26   end
27
28 end
29
30
31
32
33 local socket = require("socket")
34 local client = assert(socket.connect("127.0.0.1",2010))
35 --socket.unix = require("socket.unix")
36 --local socket = assert(socket.unix())
37 --local client = assert(socket:connect("/var/run/power_sensor.socket"))
38 client:settimeout(30)
39
40
41
42
43 while 1 do
44   local line, err = client:receive()
45   if not err then 
46     parse_value(line) 
47   end
48   client:send("T")
49   line, err = client:receive()
50   if not err then 
51     parse_value(line)
52   end
53   client:send("P")
54   line, err = client:receive()
55   if not err then 
56     parse_value(line)
57   end
58   save_values()
59 end