V tom článku si ukážeme praktické použití. Uděláme teploměr s čidlem
DS18B20, který bude v pravidelných intervalech odesílat data na
thingspeak.com. Thingspeak je otevřená cloud platforma pro různá zařízení co sbírají data. Výhodu je, že odeslání dat je velmi jednoduché, není třeba žádná složitá autentizace. Z dat se automaticky vytváří graf. Je možné je pak exportovat. Nejdříve si musíte na
thingspeak.com založit účet a vytvořit kanál kam budeme odesílat data. K danému kanálu je třeba získat API klíč pro zápis.
Zapojení je velmi jednoduché:
Dále pomocí IDE nahrajeme dva subory ds1820.lua a init.lua.
Soubor ds1820.lua čte teplotu z čidla a odesílá metodou GET data na server thingspeak.com. Zde nastavte váš API klíč pro zápis a periodu jak často chcete data odesílat. Výchozí hodnota je 1 minuta.
-- ds1820.lua
-- Measure temperature and post data to thingspeak.com
-- 2014 OK1CDJ
--- Temp sensor DS18B20 is conntected to GPIO0
pin = 3
ow.setup(pin)
counter=0
lasttemp=-999
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
--- Get temperature from DS18B20
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
--- Get temp and send data to thingspeak.com
function sendData()
getTemp()
t1 = lasttemp / 10000
t2 = (lasttemp >= 0 and lasttemp % 10000) or (10000 - lasttemp % 10000)
print("Temp:"..t1 .. "."..t2.." C\n")
-- conection to thingspeak.com
print("Sending data to thingspeak.com")
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
-- api.thingspeak.com 184.106.153.149
conn:connect(80,'184.106.153.149')
conn:send("GET /update?key=YOURKEY&field1="..t1.."."..t2.." HTTP/1.1\r\n")
conn:send("Host: api.thingspeak.com\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")
conn:on("sent",function(conn)
print("Closing connection")
conn:close()
end)
conn:on("disconnection", function(conn)
print("Got disconnection...")
end)
end
-- send data every X ms senconds to thing speak
tmr.alarm(0, 60000, 1, function() sendData() end )
Soubor initi.lua - se spustí automaticky po restartu. Připojí se k nastavené WIFI síti a počká na přidělení IP adresy a pak spustí skript ds1820.lua. Ve skriptu je třeba nastavit vaši síť.
--init.lua
print("Setting up WIFI...")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("SSDID","PASSWORD")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavailable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
dofile("ds1820.lua")
end
end)
Zdrojové kódy je možné také sthnout zde:
https://github.com/ok1cdj/ESP8266-LUA/tree/master/Thermometer-DS18B20-Thingspeak
Jak je vidět ESP8266 s LUA firmwarem je použitelné pro různé aplikace. Není problém v něm napsat jednoduchý klient i server, připojit různé periférie (momentálně 1-wire nebo i2c).