Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyRzn committed Mar 25, 2015
1 parent 89806b7 commit 0dc9e19
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 42 deletions.
17 changes: 17 additions & 0 deletions battery.lua
@@ -0,0 +1,17 @@
gpio.mode(5, gpio.OUTPUT)
gpio.write(5, gpio.HIGH)

gpio.mode(6, gpio.OUTPUT)
gpio.write(6, gpio.LOW)
tmr.delay(100000)
gpio.write(5, gpio.LOW)
gpio.mode(6, gpio.INPUT)
local cnt = 0
while (gpio.read(6) == 0) do
cnt = cnt + 1
if cnt > 4000 then
break
end
tmr.delay(1000)
end
return cnt
56 changes: 56 additions & 0 deletions ds18b20.lua
@@ -0,0 +1,56 @@
local pin = 7

local function read()
local count = 0
local addr

ow.setup(pin)

ow.reset_search(pin)
repeat
count = count + 1
addr = ow.search(pin)
tmr.wdclr()
until((addr ~= nil) or (count > 100))
ow.reset_search(pin)

if(addr == nil) then
return result
end
local crc = ow.crc8(string.sub(addr,1,7))
if (crc == addr:byte(8)) then
if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
ow.reset(pin)
ow.select(pin, addr)
ow.write(pin, 0x44, 1)
tmr.delay(10000)
present = ow.reset(pin)
ow.select(pin, addr)
ow.write(pin,0xBE,1)
local data = string.char(ow.read(pin))
for i = 1, 8 do
data = data .. string.char(ow.read(pin))
end
crc = ow.crc8(string.sub(data,1,8))
if (crc == data:byte(9)) then
local t = (data:byte(1) + data:byte(2) * 256) * 625
local t1 = t / 10000
local t2 = t % 10000
if t1 < -50 or t1 > 50 then return nil end
return t1.."."..string.format("%04u", t2)
end
tmr.wdclr()
end
end
return nil
end


local res
local i
for i = 1,20 do
res = read()
if res ~= nil then return res end
end
return ""

52 changes: 18 additions & 34 deletions esp8266_server.py
Expand Up @@ -4,34 +4,12 @@
import socket, time


def calcData(data):
D1 = -39.7 # for 14 Bit @ 3.3V
D2 = 0.01 # for 14 Bit DEGC

C1 = -2.0468 # for 12 Bit
C2 = 0.0367 # for 12 Bit
C3 = -0.0000015955 # for 12 Bit
T1 = 0.01 # for 14 Bit @ 3.3V
T2 = 0.00008 # for 14 Bit @ 3.3V

rawTemp, rawHum = data
rawTemp = float(rawTemp)
rawHum = float(rawHum)

temp = rawTemp * D2 + D1

linHum = C1 + C2 * rawHum + C3 * rawHum * rawHum
hum = (temp - 25.0) * (T1 + T2 * rawHum) + linHum

return (temp, hum)


addr = ('192.168.0.66', 8266)
addr = ('192.168.0.115', 8266)

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(addr)

DEEP_SLEEP_TIME = 3
DEEP_SLEEP_TIME = 60
MAX_CONNECTION_ATTEMPTS = 20


Expand All @@ -40,19 +18,25 @@ def calcData(data):
s, addr = sock.recvfrom(1024)

data = s.split(',')
if len(data) != 4:
if len(data) != 7:
print 'Incorrect data: "%s"' % (s)
continue

temp, hum, ds_time, max_conn = data
temp, hum = calcData((temp, hum))
ds_time, max_conn = int(ds_time), int(max_conn)

print '%d) %s T=%.2f, H=%.2f%% (ds=%d, mc=%d)' % (cnt, time.strftime("%H:%M:%S"), temp, hum, ds_time, max_conn)

if ds_time != DEEP_SLEEP_TIME or max_conn != MAX_CONNECTION_ATTEMPTS:
sock.sendto('%d,%d' % (DEEP_SLEEP_TIME, MAX_CONNECTION_ATTEMPTS), addr)
print '\tSent new settings: ds=%d, mc=%d' % (DEEP_SLEEP_TIME, MAX_CONNECTION_ATTEMPTS)
temp, hum, btemp, battery, ds_time, max_conn, conn_time = data
if not temp:
temp = -666
if not hum:
hum = -666
if not btemp:
btemp = -666
temp, hum, btemp = float(temp), float(hum), float(btemp)
battery, ds_time, max_conn, conn_time = int(battery), int(ds_time), int(max_conn), int(conn_time)

print '%d) %s T=%.2f, H=%.2f%%, BT=%.2f (bat=%d, ds=%d, mc=%d, ct=%d)' % (cnt, time.strftime("%H:%M:%S"), temp, hum, btemp, battery, ds_time, max_conn, conn_time)

#if ds_time != DEEP_SLEEP_TIME or max_conn != MAX_CONNECTION_ATTEMPTS:
#sock.sendto('%d,%d' % (DEEP_SLEEP_TIME, MAX_CONNECTION_ATTEMPTS), addr)
#print '\tSent new settings: ds=%d, mc=%d' % (DEEP_SLEEP_TIME, MAX_CONNECTION_ATTEMPTS)

cnt += 1

Expand Down
11 changes: 7 additions & 4 deletions init.lua
@@ -1,17 +1,19 @@
battery = dofile("battery.lua")
collectgarbage()

deep_sleep_time = 600
max_connection_attempts = 20
temp = ""
hum = ""
btemp = ""
run_cnt = 0


local function sleep()
print("sleep")
node.dsleep(deep_sleep_time * 1000000)
end

function run()
print("run")
if run_cnt == 0 then
local data = dofile("sht1x_v2.lua")
temp = (data[1])
Expand All @@ -21,7 +23,6 @@ function run()

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

run_cnt = run_cnt + 1
Expand All @@ -33,8 +34,10 @@ function run()

tmr.stop(0)
collectgarbage()
print("send")
dofile("send.lua")
temp = nil
hum = nil
btemp = nil
tmr.alarm(0, 100, 0, sleep)
end

Expand Down
3 changes: 2 additions & 1 deletion send.lua
Expand Up @@ -3,5 +3,6 @@ local server_port = 8266

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:send(temp..","..hum..","..btemp..","..battery..","..deep_sleep_time..","..max_connection_attempts..","..run_cnt)
conn:close()
conn = nil
5 changes: 2 additions & 3 deletions sht1x_v2.lua
Expand Up @@ -56,7 +56,6 @@ end
local function init()
gpio.mode(sda, gpio.INPUT)
gpio.mode(scl, gpio.OUTPUT)
print("SHT1x init done")
end

local function wait()
Expand Down Expand Up @@ -119,8 +118,8 @@ local function num_to_str(val, mult)
sign = "-"
end
local v1 = val/mult
local v2 = val%mult
local res = string.format("%d.%04d", v1, v2)
local v2 = (val/(mult/1000))%1000
local res = string.format("%d.%03d", v1, v2)
return sign..res
end

Expand Down

0 comments on commit 0dc9e19

Please sign in to comment.