From 5d62624e084f9de15e2c6408a8c007a7b4fabf60 Mon Sep 17 00:00:00 2001
From: Vladimir Dronnikov <dronnikov@gmail.com>
Date: Mon, 19 Jan 2015 23:32:16 +0300
Subject: [PATCH 1/3] cleanup http module

---
 lua_modules/http/http-example.lua | 6 ++----
 lua_modules/http/http.lua         | 6 +-----
 2 files changed, 3 insertions(+), 9 deletions(-)

diff --git a/lua_modules/http/http-example.lua b/lua_modules/http/http-example.lua
index bd7df187..5d3788e8 100644
--- a/lua_modules/http/http-example.lua
+++ b/lua_modules/http/http-example.lua
@@ -6,9 +6,8 @@
 ------------------------------------------------------------------------------
 require("http").createServer(80, function(req, res)
   -- analyse method and url
-  print("+R", req.method, req.url)
+  print("+R", req.method, req.url, node.heap())
   -- setup handler of headers, if any
---[[
   req.onheader = function(self, name, value)
     -- print("+H", name, value)
     -- E.g. look for "content-type" header,
@@ -34,8 +33,7 @@ require("http").createServer(80, function(req, res)
       res:finish()
     end
   end
-]]
   -- or just do something not waiting till body (if any) comes
   --res:finish("Hello, world!")
-  res:finish("Salut, monde!")
+  --res:finish("Salut, monde!")
 end)
diff --git a/lua_modules/http/http.lua b/lua_modules/http/http.lua
index 9c52ce45..9f1e3f25 100644
--- a/lua_modules/http/http.lua
+++ b/lua_modules/http/http.lua
@@ -87,9 +87,7 @@ do
     return res
   end
 
-  tmr.wdclr()
-
-  ------------------------------------------------------------------------------
+    ------------------------------------------------------------------------------
   -- HTTP parser
   ------------------------------------------------------------------------------
   local http_handler = function(handler)
@@ -190,8 +188,6 @@ do
     end
   end
 
-  tmr.wdclr()
-
   ------------------------------------------------------------------------------
   -- HTTP server
   ------------------------------------------------------------------------------

From c89686e1f77b481ff6a84e9b51a3d1009142a6c2 Mon Sep 17 00:00:00 2001
From: Vladimir Dronnikov <dronnikov@gmail.com>
Date: Mon, 26 Jan 2015 14:42:07 +0300
Subject: [PATCH 2/3] yet another ds18b20 module added

---
 lua_examples/yet-another-ds18b20.lua | 65 ++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100644 lua_examples/yet-another-ds18b20.lua

diff --git a/lua_examples/yet-another-ds18b20.lua b/lua_examples/yet-another-ds18b20.lua
new file mode 100644
index 00000000..edcbe2d1
--- /dev/null
+++ b/lua_examples/yet-another-ds18b20.lua
@@ -0,0 +1,65 @@
+------------------------------------------------------------------------------
+-- DS18B20 query module
+--
+-- LICENCE: http://opensource.org/licenses/MIT
+-- Vladimir Dronnikov <dronnikov@gmail.com>
+--
+-- Example:
+-- for k, v in pairs(require("ds18b20").read(4)) do print(k, v) end
+------------------------------------------------------------------------------
+local M
+do
+  local format_addr = function(a)
+    return ("%02x-%02x%02x%02x%02x%02x%02x"):format(
+        a:byte(1),
+        a:byte(7), a:byte(6), a:byte(5),
+        a:byte(4), a:byte(3), a:byte(2)
+      )
+  end
+  local read = function(pin, delay)
+    local ow = require("ow")
+    -- get list of relevant devices
+    local d = { }
+    ow.setup(pin)
+    ow.reset_search(pin)
+    while true do
+      tmr.wdclr()
+      local a = ow.search(pin)
+      if not a then break end
+      if ow.crc8(a) == 0 and
+        (a:byte(1) == 0x10 or a:byte(1) == 0x28)
+      then
+        d[#d + 1] = a
+      end
+    end
+    -- conversion command for all
+    ow.reset(pin)
+    ow.skip(pin)
+    ow.write(pin, 0x44, 1)
+    -- wait a bit
+    tmr.delay(delay or 100000)
+    -- iterate over devices
+    local r = { }
+    for i = 1, #d do
+      tmr.wdclr()
+      -- read rom command
+      ow.reset(pin)
+      ow.select(pin, d[i])
+      ow.write(pin, 0xBE, 1)
+      -- read data
+      local x = ow.read_bytes(pin, 9)
+      if ow.crc8(x) == 0 then
+        local t = (x:byte(1) + x:byte(2) * 256) * 625
+        -- NB: temperature in Celcius * 10^4
+        r[format_addr(d[i])] = t
+        d[i] = nil
+      end
+    end
+    return r
+  end
+  -- expose
+  M = {
+    read = read,
+  }
+end
+return M

From 96ab811a55b2bc774e687a1623c734d82850d892 Mon Sep 17 00:00:00 2001
From: Vladimir Dronnikov <dronnikov@gmail.com>
Date: Mon, 26 Jan 2015 14:42:20 +0300
Subject: [PATCH 3/3] yet another dht11/22 module added

---
 lua_examples/yet-another-dht22.lua | 84 ++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)
 create mode 100644 lua_examples/yet-another-dht22.lua

diff --git a/lua_examples/yet-another-dht22.lua b/lua_examples/yet-another-dht22.lua
new file mode 100644
index 00000000..1bf65458
--- /dev/null
+++ b/lua_examples/yet-another-dht22.lua
@@ -0,0 +1,84 @@
+------------------------------------------------------------------------------
+-- DHT11/22 query module
+--
+-- LICENCE: http://opensource.org/licenses/MIT
+-- Vladimir Dronnikov <dronnikov@gmail.com>
+--
+-- Example:
+-- print("DHT11", require("dht22").read(4))
+-- print("DHT22", require("dht22").read(4, true))
+-- NB: the very first read sometimes fails
+------------------------------------------------------------------------------
+local M
+do
+  -- cache
+  local gpio = gpio
+  local val = gpio.read
+  local waitus = tmr.delay
+  --
+  local read = function(pin, dht22)
+    -- wait for pin value
+    local w = function(v)
+      local c = 255
+      while c > 0 and val(pin) ~= v do c = c - 1 end
+      return c
+    end
+    -- NB: we preallocate incoming data buffer
+    --     or precise timing in reader gets broken
+    local b = { 0, 0, 0, 0, 0 }
+
+    -- kick the device
+    gpio.mode(pin, gpio.INPUT, gpio.PULLUP)
+    gpio.write(pin, 1)
+    waitus(10)
+    gpio.mode(pin, gpio.OUTPUT)
+    gpio.write(pin, 0)
+    waitus(20000)
+    gpio.write(pin, 1)
+    gpio.mode(pin, gpio.INPUT, gpio.PULLUP)
+    -- wait for device presense
+    if w(0) == 0 or w(1) == 0 or w(0) == 0 then
+      return nil, 0
+    end
+    -- receive 5 octets of data, msb first
+    for i = 1, 5 do
+      local x = 0
+      for j = 1, 8 do
+        x = x + x
+        if w(1) == 0 then return nil, 1 end
+        -- 70us for 1, 27 us for 0
+        waitus(30)
+        if val(pin) == 1 then
+          x = x + 1
+          if w(0) == 0 then return nil, 2 end
+        end
+      end
+      b[i] = x
+    end
+    -- check crc. NB: calculating in receiver loop breaks timings
+    local crc = 0
+    for i = 1, 4 do
+      crc = (crc + b[i]) % 256
+    end
+    if crc ~= b[5] then return nil, 3 end
+    -- convert
+    local t, h
+    -- DHT22: values in tenths of unit, temperature can be negative
+    if dht22 then
+      h = b[1] * 256 + b[2]
+      t = b[3] * 256 + b[4]
+      if t > 0x8000 then t = -(t - 0x8000) end
+    -- DHT11: no negative temperatures, only integers
+    -- NB: return in 0.1 Celsius
+    else
+      h = 10 * b[1]
+      t = 10 * b[3]
+    end
+    return t, h
+  end
+  -- expose interface
+  M = {
+    read = read,
+  }
+end
+return M