diff --git a/battery.lua b/battery.lua new file mode 100644 index 0000000..c4ec638 --- /dev/null +++ b/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 diff --git a/ds18b20.lua b/ds18b20.lua new file mode 100644 index 0000000..0b039eb --- /dev/null +++ b/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 "" + diff --git a/esp8266_server.py b/esp8266_server.py index 7eb68da..ca3b122 100755 --- a/esp8266_server.py +++ b/esp8266_server.py @@ -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 @@ -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 diff --git a/init.lua b/init.lua index fe10554..b7471c0 100644 --- a/init.lua +++ b/init.lua @@ -1,3 +1,6 @@ +battery = dofile("battery.lua") +collectgarbage() + deep_sleep_time = 600 max_connection_attempts = 20 temp = "" @@ -5,13 +8,12 @@ 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]) @@ -21,7 +23,6 @@ function run() btemp = dofile("ds18b20.lua") collectgarbage() - print(temp, hum, btemp) end run_cnt = run_cnt + 1 @@ -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 diff --git a/send.lua b/send.lua index f79d1c2..f8ab471 100644 --- a/send.lua +++ b/send.lua @@ -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 diff --git a/sht1x_v2.lua b/sht1x_v2.lua index 0736ae2..c6e79d9 100644 --- a/sht1x_v2.lua +++ b/sht1x_v2.lua @@ -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() @@ -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