commit 2e53e158cfde31b75887476b10e78f186a3d033c Author: Jeena Date: Sun Nov 20 05:15:49 2016 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b72a232 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +credentials.lua diff --git a/README.md b/README.md new file mode 100644 index 0000000..4b717a3 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# esp8266-temperature + +This is my first stab at a wireless termometer. + +It expects a credentials.lua file with content like this: + + SSID = "myssid" + PASSWORD = "mypasswd" \ No newline at end of file diff --git a/application.lua b/application.lua new file mode 100644 index 0000000..bdd4381 --- /dev/null +++ b/application.lua @@ -0,0 +1,8 @@ +pin = 4 +ow.setup(pin) + +counter=0 +lasttemp=-999 + +dofile("dallas.lua") +dofile("server.lua") \ No newline at end of file diff --git a/dallas.lua b/dallas.lua new file mode 100644 index 0000000..84d109b --- /dev/null +++ b/dallas.lua @@ -0,0 +1,51 @@ +function bxor(a,b) + local r = 0 + for i = 0, 31 do + if ( a % 2 + b % 2 == 1 ) then + r = r + 2^i + end + a = a / 2 + b = b / 2 + end + return r +end + +function getTemp() + addr = ow.reset_search(pin) + repeat + tmr.wdclr() + + if (addr ~= nil) then + 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(1000000) + present = ow.reset(pin) + ow.select(pin, addr) + ow.write(pin,0xBE, 1) + data = nil + 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 + t = (data:byte(1) + data:byte(2) * 256) + if (t > 32768) then + t = (bxor(t, 0xffff)) + 1 + t = (-1) * t + end + t = t * 625 + lasttemp = t + print("Last temp: " .. lasttemp) + end + tmr.wdclr() + end + end + end + addr = ow.search(pin) + until(addr == nil) +end diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..0558cc3 --- /dev/null +++ b/init.lua @@ -0,0 +1,29 @@ +-- load credentials, 'SSID' and 'PASSWORD' declared and initialize in there +dofile("credentials.lua") + +function startup() + if file.open("init.lua") == nil then + print("init.lua deleted or renamed") + else + print("Running") + file.close("init.lua") + -- the actual application is stored in 'application.lua' + dofile("application.lua") + end +end + +print("Connecting to WiFi access point...") +wifi.setmode(wifi.STATION) +wifi.sta.config(SSID, PASSWORD) +-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default +tmr.alarm(1, 1000, 1, function() + if wifi.sta.getip() == nil then + print("Waiting for IP address...") + else + tmr.stop(1) + print("WiFi connection established, IP address: " .. wifi.sta.getip()) + print("You have 3 seconds to abort") + print("Waiting...") + tmr.alarm(0, 3000, 0, startup) + end +end) \ No newline at end of file diff --git a/server.lua b/server.lua new file mode 100644 index 0000000..d71dfdc --- /dev/null +++ b/server.lua @@ -0,0 +1,17 @@ +mycounter=0 +srv=net.createServer(net.TCP) +srv:listen(80, function(conn) + conn:on("receive", function(conn, payload) + if string.find(payload,"?myarg=") then + mycounter=mycounter+1 + m="
Value= " .. string.sub(payload, string.find(payload,"?myarg=")+7,string.find(payload,"HTTP")-2) + else + m="" + end + + getTemp() + + conn:send("

Hello, this is Jeenas ESP8266.

How are you today.
Count=" .. mycounter .. m .. "
Heap=" .. node.heap() .. "
Temp=" .. lasttemp) + end) + conn:on("sent", function(conn) conn:close() end) +end)