Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lua/bh1750.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local M = {}
local ADDR = 0x23
local COMMAND = 0x20
local i2c = i2c
local ID = 0

function M.init(sda, scl)
i2c.setup(ID, sda, scl, i2c.SLOW)
end

local function read_data_from_bh1750(address)
i2c.start(ID)
i2c.address(ID, address, i2c.TRANSMITTER)
i2c.write(ID, COMMAND)
i2c.stop(ID)
i2c.start(ID)
i2c.address(ID, address,i2c.RECEIVER)
tmr.delay(185000)
data = i2c.read(ID, 2)
i2c.stop(ID)
return data
end

function M.read_lux()
data_string = read_data_from_bh1750(ADDR)
data = data_string:byte(1) * 256 + data_string:byte(2)
lux = (data / 1.2)
return(lux)
end

return M
23 changes: 23 additions & 0 deletions lua/bme280.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
alt=17 -- altitude of the measurement place

sda, scl = 1, 2
i2c.setup(0, sda, scl, i2c.SLOW) -- call i2c.setup() only once
print(bme280.setup())
print(bme280.read())
print(bme280.humi())

T, P, H, QNH = bme280.read(alt)
local Tsgn = (T < 0 and -1 or 1); T = Tsgn*T
print(string.format("T=%s%d.%02d", Tsgn<0 and "-" or "", T/100, T%100))
print(string.format("QFE=%d.%03d", P/1000, P%1000))
print(string.format("QNH=%d.%03d", QNH/1000, QNH%1000))
print(string.format("humidity=%d.%03d%%", H/1000, H%1000))
D = bme280.dewpoint(H, T)
local Dsgn = (D < 0 and -1 or 1); D = Dsgn*D
print(string.format("dew_point=%s%d.%02d", Dsgn<0 and "-" or "", D/100, D%100))

-- altimeter function - calculate altitude based on current sea level pressure (QNH) and measure pressure
P = bme280.baro()
curAlt = bme280.altitude(P, QNH)
local curAltsgn = (curAlt < 0 and -1 or 1); curAlt = curAltsgn*curAlt
print(string.format("altitude=%s%d.%02d", curAltsgn<0 and "-" or "", curAlt/100, curAlt%100))
1 change: 1 addition & 0 deletions lua/counter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ tmr.alarm(0, 60000, 1, function()
print("sent")
end)
end)

8 changes: 4 additions & 4 deletions lua/dht22_mqtt_kasvari.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ lux = bh1750.read_lux()
print("lux: " .. lux)

m = mqtt.Client("esp4", 120)
m:on("connect", function(con)
print ("Connected")
m:on("connect", function(con)
print ("Connected")
m:subscribe("lampo",0, function(conn) print("subscribe success") end)
end)

m:on("offline", function(con)
m:on("offline", function(con)
print ("Offline")
node.restart()
end)
Expand All @@ -40,7 +40,7 @@ tmr.alarm(0, 60000, 1, function()
if ( status == dht.OK ) then
output = output .. temp .. " " .. humi .. " " .. lux
print(output)
m:publish("kasvarimittaukset", output, 2, 0, function(conn)
m:publish("kasvarimittaukset", output, 2, 0, function(conn)
print("sent")
end)
else
Expand Down
28 changes: 28 additions & 0 deletions lua/dht22_mqtt_kasvari.lua.20171019
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

dht=require("dht")
status,temp,humi,temp_decimial,humi_decimial = dht.read(2)
print(temp)
print(humi)

mqtt = mqtt.Client("esp4", 120)
mqtt:on("connect", function(con) print ("Connected") end)
mqtt:on("offline", function(con)
print ("Offline")
node.restart()
end)
mqtt:connect("192.168.0.1", 1883, 0)

tmr.alarm(0, 60000, 1, function()
print('Measuring..')
output ="esp4: "
status,temp,humi,temp_decimial,humi_decimial = dht.read(2)
if ( status == dht.OK ) then
output = output .. temp .. " " .. humi
print(output)
mqtt:publish("kasvarimittaukset", output, 2, 0, function(conn)
print("sent")
end)
else
print("Failed to read DHT22!")
end
end)
5 changes: 5 additions & 0 deletions lua/ds1820-mittaus.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
t=require('ds18b20')
t.setup(3)
addrs=t.addrs()
for i=1,table.getn(addrs) do print(t.read(addrs[i])) end
tmr.alarm(0, 1000, 1, function() print('*'); for i=1,table.getn(addrs) do print(t.read(addrs[i])) end end )
21 changes: 19 additions & 2 deletions lua/ds1820_mqtt.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
sda, scl = 5, 6 -- gpio012, gpio014
DHT_PIN = 2 -- gpio04
status,temp,humi,temp_decimial,humi_decimial = dht.read(DHT_PIN) -- gpio4
print(temp)
print(humi)

t = require('ds18b20')
t.setup(3)
i2c.setup(0, sda, scl, i2c.SLOW)
bme280.setup()
print(bme280.read())

t = require('modds18b20')
t.setup(4) -- gpio2
addrs = t.addrs()
for i=1, table.getn(addrs) do print(t.read(addrs[i])) end

Expand All @@ -18,6 +27,14 @@ tmr.alarm(0, 60000, 1, function()
for i=1, table.getn(addrs) do
output = output .. t.read(addrs[i]) .. " "
end
status,temp,humi,temp_decimial,humi_decimial = dht.read(DHT_PIN)
if ( status == dht.OK ) then
output = output .. temp .. " " .. humi
else
output = output .. temp .. " U U"
end
T, P, H = bme280.read()
output = output .. " " .. T/100 .. " " .. P/1000 .. " " .. H/1000
print(output)
mqtt:publish("mittaukset", output, 2, 0, function(conn)
print("sent")
Expand Down
2 changes: 1 addition & 1 deletion lua/ds1820_mqtt_mokki.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function start_timer()
buffer:set(i, 100, 100, 100)
end
ws2812.write(buffer)
tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()
tmr.alarm(1, 60000, tmr.ALARM_AUTO, function()
print('ON timer round ' .. timer_round)
for i=1, 15 do
if i == timer_round then
Expand Down
143 changes: 143 additions & 0 deletions lua/ds18b20.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
--------------------------------------------------------------------------------
-- DS18B20 one wire module for NODEMCU
-- NODEMCU TEAM
-- LICENCE: http://opensource.org/licenses/MIT
-- Vowstar <vowstar@nodemcu.com>
-- 2015/02/14 sza2 <sza2trash@gmail.com> Fix for negative values
--------------------------------------------------------------------------------

-- Set module name as parameter of require
local modname = ...
local M = {}
_G[modname] = M
--------------------------------------------------------------------------------
-- Local used variables
--------------------------------------------------------------------------------
-- DS18B20 dq pin
local pin = nil
-- DS18B20 default pin
local defaultPin = 9
--------------------------------------------------------------------------------
-- Local used modules
--------------------------------------------------------------------------------
-- Table module
local table = table
-- String module
local string = string
-- One wire module
local ow = ow
-- Timer module
local tmr = tmr
-- Limited to local environment
setfenv(1,M)
--------------------------------------------------------------------------------
-- Implementation
--------------------------------------------------------------------------------
C = 'C'
F = 'F'
K = 'K'
function setup(dq)
pin = dq
if(pin == nil) then
pin = defaultPin
end
ow.setup(pin)
end

function addrs()
setup(pin)
tbl = {}
ow.reset_search(pin)
repeat
addr = ow.search(pin)
if(addr ~= nil) then
table.insert(tbl, addr)
end
tmr.wdclr()
until (addr == nil)
ow.reset_search(pin)
return tbl
end

function readNumber(addr, unit)
result = nil
setup(pin)
flag = false
if(addr == nil) then
ow.reset_search(pin)
count = 0
repeat
count = count + 1
addr = ow.search(pin)
tmr.wdclr()
until((addr ~= nil) or (count > 100))
ow.reset_search(pin)
end
if(addr == nil) then
return result
end
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
-- print("Device is a DS18S20 family device.")
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)
-- print("P="..present)
data = nil
data = string.char(ow.read(pin))
for i = 1, 8 do
data = data .. string.char(ow.read(pin))
end
-- print(data:byte(1,9))
crc = ow.crc8(string.sub(data,1,8))
-- print("CRC="..crc)
if (crc == data:byte(9)) then
t = (data:byte(1) + data:byte(2) * 256)
if (t > 32767) then
t = t - 65536
end

if (addr:byte(1) == 0x28) then
t = t * 625 -- DS18B20, 4 fractional bits
else
t = t * 5000 -- DS18S20, 1 fractional bit
end

if(unit == nil or unit == 'C') then
-- do nothing
elseif(unit == 'F') then
t = t * 1.8 + 320000
elseif(unit == 'K') then
t = t + 2731500
else
return nil
end
t = t / 10000
return t
end
tmr.wdclr()
else
-- print("Device family is not recognized.")
end
else
-- print("CRC is not valid!")
end
return result
end

function read(addr, unit)
t = readNumber(addr, unit)
if (t == nil) then
return nil
else
return t
end
end

-- Return module table
return M
60 changes: 60 additions & 0 deletions lua/init_bme280.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
ABORT_PIN = 2 -- gpio14
sda, scl = 6, 7 -- gpio012, gpio013

i2c.setup(0, sda, scl, i2c.SLOW)
bme280.setup()

P, T = bme280.baro()
print(P)
print(T)

function startup()
counter = 0
-- wifi.sta.setip({ip="192.168.2.14", netmask="255.255.255.0", gateway="192.168.2.1"})
uart.setup(0, 9600, 8, 0, 1, 1)
gpio.mode(ABORT_PIN, gpio.INPUT, gpio.PULLUP)
if gpio.read(ABORT_PIN) == 0 then
print('Start aborted!')
return
end
print('Start.')
if adc.force_init_mode(adc.INIT_VDD33)
then
node.restart()
return
end
vcc = adc.readvdd33(0)
tmr.alarm(1, 300, 1, function()
if wifi.sta.getip() == nil then
print("Waiting for IP address...")
counter = counter + 1
if counter > 30 then
tmr.stop(1)
print("Failed to get IP address!")
node.dsleep(60 * 1000000)
end
else
print("IP address is " .. wifi.sta.getip())
tmr.stop(1)
remote_control()
end
end)
end

function remote_control()
P, T = bme280.baro()
output = "temp=" .. T/100 .. "&pressure=" .. P/1000 .. "&vcc=" .. vcc
print(output)
http.post("http://api.t-ocdn.com/ulkopaine", "Content-type: application/x-www-form-urlencoded\r\n", output,
function(status_code, body, headers)
if ( status_code == -1) then
print("Failed")
else
print("Sent")
end
print("Waiting...")
tmr.alarm(1, 60 * 1000, 0, remote_control)
end)
end

startup()
13 changes: 13 additions & 0 deletions lua/init_empty.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function startup()
if abort == true then
print('Start aborted!')
return
end
print('Start.')
-- dofile('counter.lua')
end

uart.setup(0, 9600, 8, 0, 1, 1)

abort = false
tmr.alarm(0,10000,0,startup)
Loading