Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyRzn committed Feb 15, 2015
1 parent ccf8c2e commit 89806b7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 54 deletions.
43 changes: 38 additions & 5 deletions init.lua
@@ -1,8 +1,41 @@
require "main2"
deep_sleep_time = 600
max_connection_attempts = 20
temp = ""
hum = ""
btemp = ""
run_cnt = 0

wifi.setmode(wifi.STATION)
wifi.sta.config("lolcat2", "1234567890")
wifi.sta.connect()
local function sleep()
print("sleep")
node.dsleep(deep_sleep_time * 1000000)
end

tmr.alarm(0, 1000, 1, run)
function run()
print("run")
if run_cnt == 0 then
local data = dofile("sht1x_v2.lua")
temp = (data[1])
hum = (data[2])
data = nil
collectgarbage()

btemp = dofile("ds18b20.lua")
collectgarbage()
print(temp, hum, btemp)
end

run_cnt = run_cnt + 1

if wifi.sta.status() ~= 5 then -- not got IP
if run_cnt > max_connection_attempts then sleep() end
return
end

tmr.stop(0)
collectgarbage()
print("send")
dofile("send.lua")
tmr.alarm(0, 100, 0, sleep)
end

tmr.alarm(0, 1000, 1, run)
36 changes: 5 additions & 31 deletions send.lua
@@ -1,33 +1,7 @@
local server_ip = "192.168.0.66"
local server_ip = "192.168.0.147"
local server_port = 8266


local function receive(conn, data)
local i = 1
local val
local vals = {}

for val in string.gmatch(data, "%d+") do
vals[i] = tonumber(val)
i = i + 1
end

if vals[1] == nil or vals[2] == nil then return end
deep_sleep_time = (vals[1])
max_connection_attempts = (vals[2])

end

local function send()
local conn = net.createConnection(net.UDP)
conn:on("receive", receive)
conn:connect(server_port, server_ip)
-- local str = string.format("%d,%d,%d,%d", temp, hum, deep_sleep_time, max_connection_attempts)

-- conn:send(str)
-- str = nil
conn:close()
end

send()

local conn = net.createConnection(net.UDP)
conn:connect(server_port, server_ip)
conn:send(temp..","..hum..","..btemp..","..deep_sleep_time..","..max_connection_attempts..","..run_cnt)
conn:close()
40 changes: 22 additions & 18 deletions sht1x_v2.lua
Expand Up @@ -113,9 +113,15 @@ local function read_cmd(cmd)
end

local function num_to_str(val, mult)
local sign = ""
if val < 0 then
val = -val
sign = "-"
end
local v1 = val/mult
local v2 = val/(mult/100) - v1*100
return string.format("%d.%02d", v1, v2)
local v2 = val%mult
local res = string.format("%d.%04d", v1, v2)
return sign..res
end

-- get temperature in raw format
Expand All @@ -129,34 +135,32 @@ local function get_raw_humidity()
end

-- get temperature in in degrees Celsius
local function get_temperature_C()
local raw_temp = get_raw_temperature()
if raw_temp == nil then return nil end
temp = raw_temp * D2C + D1C
local function temperature_C(raw_temp)
if raw_temp == nil then return "" end
local temp = raw_temp * D2C + D1C
return num_to_str(temp, TM)
end

-- get temperature in in degrees Fahrenheit
local function get_temperature_F()
local raw_temp = get_raw_temperature()
if raw_temp == nil then return nil end
temp = raw_temp * D2F + D1F
local function temperature_F(raw_temp)
if raw_temp == nil then return "" end
local temp = raw_temp * D2F + D1F
return num_to_str(temp, TM)
end

-- get relative humidity in percents
local function get_humidity()
local raw_temp = get_raw_temperature()
local function get_humidity(raw_temp)
if raw_temp == nil then return "" end
local raw_hum = get_raw_humidity()
temp = raw_temp * D2C + D1C
lin_hum = C1 + C2 * raw_hum + C3 * raw_hum * raw_hum / HM
hum = (temp - T0) * (T1 + T2 * raw_hum)/TM + lin_hum
local temp = raw_temp * D2C + D1C
local lin_hum = C1 + C2 * raw_hum + C3 * raw_hum * raw_hum / HM
local hum = (temp - T0) * (T1 + T2 * raw_hum)/TM + lin_hum
return num_to_str(hum, HM)
end

init()

local temp = get_temperature_C()
local hum = get_humidity()
local raw_temp = get_raw_temperature()
local temp = temperature_C(raw_temp)
local hum = get_humidity(raw_temp)

return {temp, hum}

0 comments on commit 89806b7

Please sign in to comment.