NodeMCU custom build by frightanic.com
branch: master
commit: 7b83bbb2ea134cd85ac9d63108603cc02c4e20f7
SSL: true
modules: dht,file,gpio,http,i2c,net,node,rtctime,sntp,tmr,uart,wifi
build built on: 2016-11-15 12:39
powered by Lua 5.1.4 on SDK 1.5.4.1(39cb9a32)
Gambar 1. Pin Sensor DHT11
Setiap seperlima menit (12 detik), akan dilakukan pengukuran suhu dan temperatur dan akan dikalkulasi ulang dengan menggunakan exponensial smooting alpha = 20%, dan hasil perhitungan akan di Push ke firebase realtime database yang telah dipersiapkan sebelumnya. Awalnya sistem akan melakukan sinkronisasi waktu software RTC dengan time server melalui protocol NTP.
credentials.lua
Dapat diambil dari tutorial sebelumnyainit.lua
Dapat diambil dari tutorial sebelumnyaapp.lua
pinDHT11 = 1 -- pin D1 pada board WeMos D1 mini
lastTemp = nil --exponential smoothing Temperature
lastHumi = nil --exponential smoothing Humidity
database = "https://friendlychat-6ae71.firebaseio.com/"
node = "dht11.json"
dbSecret = "kia43oq8Nn76kTJQhLIshyRpSYDHoEwjGTI*****"
url = database .. node .. "?auth=" .. dbSecret
print(url)
payload = ""
function readFromDHT11()
status, temp, humi, temp_dec, humi_dec = dht.read11(pinDHT11)
if status == dht.OK then
if lastTemp == nil then
lastTemp = temp
else
lastTemp = math.floor((0.2*temp + 0.8*lastTemp)*100)/100 -- exp snoothing
end
if lastHumi == nil then
lastHumi = humi
else
lastHumi = math.floor((0.2*humi + 0.8*lastHumi)*100)/100 -- exp snoothing
end
wib = rtctime.epoch2cal(rtctime.get()+7*60*60) -- tambah 7 WEST INDONESIA TIME
timeStr = string.format("%04d/%02d/%02d %02d:%02d:%02d", wib["year"], wib["mon"], wib["day"], wib["hour"], wib["min"], wib["sec"])
payload = "{\"time\":\"" .. timeStr .."\",\"temperature\":" .. lastTemp .. ",\"humidity\":" .. lastHumi .. "}"
print(payload)
elseif status == dht.ERROR_CHECKSUM then
print( "DHT Checksum error." )
elseif status == dht.ERROR_TIMEOUT then
print( "DHT timed out." )
end
end
function postToFirebase()
http.request(url,
"POST",
"Content-Type: application/json\r\n",
payload,
function(code, data)
if (code ~= 200) then
print("Failed : HTTP error code : " .. code)
else
print("Succeed : " .. code, data)
end
end)
end
sntp.sync("1.id.pool.ntp.org",
function(sec,usec,server)
print('setting time to:', sec, usec, "from: " .. server)
rtctime.set(sec, usec)
end,
function()
print('failed!')
end
)
tmr.alarm(0,12000,1,readFromDHT11) -- setiap seperlima menit baca dari sensir DHT11
tmr.alarm(1,60000,1,postToFirebase) -- setiap menit catat ke firebase realtime database
Tidak ada komentar:
Posting Komentar