Initial commit
This commit is contained in:
commit
2e53e158cf
6 changed files with 114 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
credentials.lua
|
8
README.md
Normal file
8
README.md
Normal file
|
@ -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"
|
8
application.lua
Normal file
8
application.lua
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
pin = 4
|
||||||
|
ow.setup(pin)
|
||||||
|
|
||||||
|
counter=0
|
||||||
|
lasttemp=-999
|
||||||
|
|
||||||
|
dofile("dallas.lua")
|
||||||
|
dofile("server.lua")
|
51
dallas.lua
Normal file
51
dallas.lua
Normal file
|
@ -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
|
29
init.lua
Normal file
29
init.lua
Normal file
|
@ -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)
|
17
server.lua
Normal file
17
server.lua
Normal file
|
@ -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="<br>Value= " .. string.sub(payload, string.find(payload,"?myarg=")+7,string.find(payload,"HTTP")-2)
|
||||||
|
else
|
||||||
|
m=""
|
||||||
|
end
|
||||||
|
|
||||||
|
getTemp()
|
||||||
|
|
||||||
|
conn:send("<h1> Hello, this is Jeenas ESP8266.</h1><p>How are you today.<br> Count=" .. mycounter .. m .. "<br>Heap=" .. node.heap() .. "<br>Temp=" .. lasttemp)
|
||||||
|
end)
|
||||||
|
conn:on("sent", function(conn) conn:close() end)
|
||||||
|
end)
|
Loading…
Add table
Add a link
Reference in a new issue