From 88db73980e76c586b6840ec702798fe54d0f5218 Mon Sep 17 00:00:00 2001 From: Janne Date: Sat, 15 Sep 2018 19:29:52 +0300 Subject: [PATCH] Stuff --- lua/bh1750.lua | 31 ++++++ lua/bme280.lua | 23 +++++ lua/counter.lua | 1 + lua/dht22_mqtt_kasvari.lua | 8 +- lua/dht22_mqtt_kasvari.lua.20171019 | 28 ++++++ lua/ds1820-mittaus.lua | 5 + lua/ds1820_mqtt.lua | 21 +++- lua/ds1820_mqtt_mokki.lua | 2 +- lua/ds18b20.lua | 143 ++++++++++++++++++++++++++++ lua/init_bme280.lua | 60 ++++++++++++ lua/init_empty.lua | 13 +++ lua/init_gc.lua | 37 +++++++ lua/init_komposti_ds1820.lua | 10 +- lua/init_mobiili.lua | 64 +++++++++++++ lua/init_mobiili_b.lua | 59 ++++++++++++ lua/init_mobiili_haa-a.lua | 65 +++++++++++++ lua/init_mobiili_haa-b.lua | 60 ++++++++++++ lua/init_rgb-pwm-joulu.lua | 22 +++++ lua/init_ws2812.lua | 5 +- lua/init_ws2812_haa.lua | 13 +++ lua/joulu.lua | 79 +++++++++++++++ lua/modds18b20.lua | 143 ++++++++++++++++++++++++++++ lua/modules/telnet.lua | 36 +++++++ lua/read_lux.lua | 9 ++ lua/rgb-pwm-joulu.lua | 79 +++++++++++++++ lua/rgb-pwm.lua | 6 +- lua/wc.lua | 77 +++++++++++++++ lua/ws2812_mqtt_dual.lua | 18 +++- lua/ws2812_mqtt_dual_orig.lua | 30 ++++++ lua/ws2812_mqtt_haa.lua | 25 +++++ 30 files changed, 1155 insertions(+), 17 deletions(-) create mode 100644 lua/bh1750.lua create mode 100644 lua/bme280.lua create mode 100644 lua/dht22_mqtt_kasvari.lua.20171019 create mode 100644 lua/ds1820-mittaus.lua create mode 100644 lua/ds18b20.lua create mode 100644 lua/init_bme280.lua create mode 100644 lua/init_empty.lua create mode 100644 lua/init_gc.lua create mode 100644 lua/init_mobiili.lua create mode 100644 lua/init_mobiili_b.lua create mode 100644 lua/init_mobiili_haa-a.lua create mode 100644 lua/init_mobiili_haa-b.lua create mode 100644 lua/init_rgb-pwm-joulu.lua create mode 100644 lua/init_ws2812_haa.lua create mode 100644 lua/joulu.lua create mode 100644 lua/modds18b20.lua create mode 100644 lua/modules/telnet.lua create mode 100644 lua/read_lux.lua create mode 100644 lua/rgb-pwm-joulu.lua create mode 100644 lua/wc.lua create mode 100644 lua/ws2812_mqtt_dual_orig.lua create mode 100644 lua/ws2812_mqtt_haa.lua diff --git a/lua/bh1750.lua b/lua/bh1750.lua new file mode 100644 index 0000000..020c0dc --- /dev/null +++ b/lua/bh1750.lua @@ -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 diff --git a/lua/bme280.lua b/lua/bme280.lua new file mode 100644 index 0000000..23c1338 --- /dev/null +++ b/lua/bme280.lua @@ -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)) diff --git a/lua/counter.lua b/lua/counter.lua index 03b67de..5794f6a 100644 --- a/lua/counter.lua +++ b/lua/counter.lua @@ -45,3 +45,4 @@ tmr.alarm(0, 60000, 1, function() print("sent") end) end) + diff --git a/lua/dht22_mqtt_kasvari.lua b/lua/dht22_mqtt_kasvari.lua index 65be693..86aae84 100644 --- a/lua/dht22_mqtt_kasvari.lua +++ b/lua/dht22_mqtt_kasvari.lua @@ -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) @@ -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 diff --git a/lua/dht22_mqtt_kasvari.lua.20171019 b/lua/dht22_mqtt_kasvari.lua.20171019 new file mode 100644 index 0000000..38cd9d8 --- /dev/null +++ b/lua/dht22_mqtt_kasvari.lua.20171019 @@ -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) diff --git a/lua/ds1820-mittaus.lua b/lua/ds1820-mittaus.lua new file mode 100644 index 0000000..bfc1f55 --- /dev/null +++ b/lua/ds1820-mittaus.lua @@ -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 ) diff --git a/lua/ds1820_mqtt.lua b/lua/ds1820_mqtt.lua index faa4ad4..665d030 100644 --- a/lua/ds1820_mqtt.lua +++ b/lua/ds1820_mqtt.lua @@ -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 @@ -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") diff --git a/lua/ds1820_mqtt_mokki.lua b/lua/ds1820_mqtt_mokki.lua index bff277a..512844e 100644 --- a/lua/ds1820_mqtt_mokki.lua +++ b/lua/ds1820_mqtt_mokki.lua @@ -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 diff --git a/lua/ds18b20.lua b/lua/ds18b20.lua new file mode 100644 index 0000000..de2d869 --- /dev/null +++ b/lua/ds18b20.lua @@ -0,0 +1,143 @@ +-------------------------------------------------------------------------------- +-- DS18B20 one wire module for NODEMCU +-- NODEMCU TEAM +-- LICENCE: http://opensource.org/licenses/MIT +-- Vowstar +-- 2015/02/14 sza2 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 diff --git a/lua/init_bme280.lua b/lua/init_bme280.lua new file mode 100644 index 0000000..86ae473 --- /dev/null +++ b/lua/init_bme280.lua @@ -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() diff --git a/lua/init_empty.lua b/lua/init_empty.lua new file mode 100644 index 0000000..241fcdc --- /dev/null +++ b/lua/init_empty.lua @@ -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) diff --git a/lua/init_gc.lua b/lua/init_gc.lua new file mode 100644 index 0000000..fafc8bb --- /dev/null +++ b/lua/init_gc.lua @@ -0,0 +1,37 @@ +PWM_PINS = {2, 6, 7} -- gpio4, gpio13, gpio14 +INPUT_PIN = 5 -- gpio12 +POWER_PIN = 1 -- gpio5 + +function shutdown() + print("Stop.") + pwm.setduty(PWM_PINS[1], 110) + for i=1, 8000 do + tmr.delay(100) + end + gpio.write(POWER_PIN, gpio.LOW) +end + +function startup() + if abort == true then + print('Start aborted!') + return + end + print('Start.') + pwm.setduty(PWM_PINS[1], 28) + tmr.alarm(0,5000,0,shutdown) +end + +uart.setup(0, 9600, 8, 0, 1, 1) + +gpio.mode(POWER_PIN, gpio.OUTPUT) +gpio.write(POWER_PIN, gpio.HIGH) +gpio.mode(INPUT_PIN, gpio.INPUT) +for i=1, 3 do + gpio.mode(PWM_PINS[i], gpio.OUTPUT) + gpio.write(PWM_PINS[i], gpio.LOW) +end +pwm.setup(PWM_PINS[1],50,85) +pwm.setduty(PWM_PINS[1], 110) + +abort = false +tmr.alarm(0,10000,0,startup) diff --git a/lua/init_komposti_ds1820.lua b/lua/init_komposti_ds1820.lua index deb634e..d38acbf 100644 --- a/lua/init_komposti_ds1820.lua +++ b/lua/init_komposti_ds1820.lua @@ -1,12 +1,11 @@ ABORT_PIN = 5 -- gpio14 DS_PIN = 4 -- gpio2 -t = require('ds18b20') +t = require('modds18b20') t.setup(DS_PIN) addrs = t.addrs() for i=1, table.getn(addrs) do print(t.read(addrs[i])) end - function startup() counter = 0 -- wifi.sta.setip({ip="192.168.2.14", netmask="255.255.255.0", gateway="192.168.2.1"}) @@ -17,6 +16,12 @@ function startup() return end print('Start.') + if adc.force_init_mode(adc.INIT_VDD33) + then + node.restart() + return -- don't bother continuing, the restart is scheduled + end + tmr.alarm(1, 300, 1, function() if wifi.sta.getip() == nil then print("Waiting for IP address...") @@ -44,6 +49,7 @@ function remote_control() for i=1, table.getn(addrs) do output = output .. t.read(addrs[i]) .. " " end + output = output .. adc.readvdd33(0) print(output) m:publish("komposti", output, 2, 0, function(conn) print("Sent") diff --git a/lua/init_mobiili.lua b/lua/init_mobiili.lua new file mode 100644 index 0000000..39334f1 --- /dev/null +++ b/lua/init_mobiili.lua @@ -0,0 +1,64 @@ +ABORT_PIN = 5 -- gpio14 +D_PIN = 4 -- gpio2 +t = require('modds18b20') + +t.setup(D_PIN) +addrs = t.addrs() +for i=1, table.getn(addrs) do print(t.read(addrs[i])) end + +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() + -- status,temp,humi,temp_decimial,humi_decimial = dht.read(D_PIN) + temp = t.read(addrs[1]) + if ( temp ~= 85 ) then + humi = 0 + output = "temp=" .. temp .. "&humi=" .. humi .. "&vcc=" .. vcc + print(output) + http.post("http://api.t-ocdn.com/mobiili", "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 + tmr.alarm(1, 100, 0, function() + print("Sleeping...") + node.dsleep(60 * 1000000) + end) + end) + end +end + +startup() diff --git a/lua/init_mobiili_b.lua b/lua/init_mobiili_b.lua new file mode 100644 index 0000000..170f0e8 --- /dev/null +++ b/lua/init_mobiili_b.lua @@ -0,0 +1,59 @@ +ABORT_PIN = 5 -- gpio14 + +sda, scl = 1, 2 -- gpio05, gpio04 +i2c.setup(0, sda, scl, i2c.SLOW) +bme280.setup() +print(bme280.read()) + +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() + T, P, H = bme280.read() + output = "temp=" .. T/100 .. "&humi=" .. H/1000 .. "&vcc=" .. vcc .. "&press=" .. P/1000 + print(output) + http.post("http://api.t-ocdn.com/mobiili", "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 + tmr.alarm(1, 100, 0, function() + print("Sleeping...") + node.dsleep(60 * 1000000) + end) + end) +end + +startup() diff --git a/lua/init_mobiili_haa-a.lua b/lua/init_mobiili_haa-a.lua new file mode 100644 index 0000000..9888bf7 --- /dev/null +++ b/lua/init_mobiili_haa-a.lua @@ -0,0 +1,65 @@ +ABORT_PIN = 5 -- gpio14 +D_PIN = 4 -- gpio2 +t = require('modds18b20') + +t.setup(D_PIN) +addrs = t.addrs() +for i=1, table.getn(addrs) do print(t.read(addrs[i])) end + +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() + -- status,temp,humi,temp_decimial,humi_decimial = dht.read(D_PIN) + temp = t.read(addrs[1]) + if ( temp ~= 85 ) then + humi = 0 + R = wifi.sta.getrssi() + output = "temp=" .. temp .. "&humi=" .. humi .. "&vcc=" .. vcc .. "&rssi=" .. R + print(output) + http.post("http://172.25.0.1/m.php?a=1", "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 + tmr.alarm(1, 100, 0, function() + print("Sleeping...") + node.dsleep(60 * 1000000) + end) + end) + end +end + +startup() diff --git a/lua/init_mobiili_haa-b.lua b/lua/init_mobiili_haa-b.lua new file mode 100644 index 0000000..ea80730 --- /dev/null +++ b/lua/init_mobiili_haa-b.lua @@ -0,0 +1,60 @@ +ABORT_PIN = 5 -- gpio14 + +sda, scl = 1, 2 -- gpio05, gpio04 +i2c.setup(0, sda, scl, i2c.SLOW) +bme280.setup() +print(bme280.read()) + +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() + T, P, H = bme280.read() + R = wifi.sta.getrssi() + output = "temp=" .. T/100 .. "&humi=" .. H/1000 .. "&vcc=" .. vcc .. "&press=" .. P/1000 .. "&rssi=" .. R + print(output) + http.post("http://172.25.0.1/m.php?a=2", "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 + tmr.alarm(1, 100, 0, function() + print("Sleeping...") + node.dsleep(60 * 1000000) + end) + end) +end + +startup() diff --git a/lua/init_rgb-pwm-joulu.lua b/lua/init_rgb-pwm-joulu.lua new file mode 100644 index 0000000..7f6d0cd --- /dev/null +++ b/lua/init_rgb-pwm-joulu.lua @@ -0,0 +1,22 @@ +PWM_PINS = {5, 6, 7} -- gpio12, gpio13, gpio14 +INPUT_PIN = 2 -- gpio04 + +function startup() + if abort == true then + print('Start aborted!') + return + end + print('Start.') + dofile('rgb-pwm-joulu.lua') +end + +uart.setup(0, 9600, 8, 0, 1, 1) + +gpio.mode(INPUT_PIN, gpio.INPUT) +for i=1, 3 do + gpio.mode(PWM_PINS[i], gpio.OUTPUT) + gpio.write(PWM_PINS[i], gpio.LOW) +end + +abort = false +tmr.alarm(0,10000,0,startup) diff --git a/lua/init_ws2812.lua b/lua/init_ws2812.lua index 5fe782e..379d5d4 100644 --- a/lua/init_ws2812.lua +++ b/lua/init_ws2812.lua @@ -2,10 +2,11 @@ function startup() if abort == true then print('Start aborted!') return - end + end print('Start.') + dofile("telnet.lua") dofile('ws2812_mqtt_dual.lua') - end +end uart.setup(0, 9600, 8, 0, 1, 1) abort = false diff --git a/lua/init_ws2812_haa.lua b/lua/init_ws2812_haa.lua new file mode 100644 index 0000000..6585f2f --- /dev/null +++ b/lua/init_ws2812_haa.lua @@ -0,0 +1,13 @@ +function startup() + if abort == true then + print('Start aborted!') + return + end + print('Start.') + dofile("telnet.lua") + dofile('ws2812_mqtt_haa.lua') +end + +uart.setup(0, 9600, 8, 0, 1, 1) +abort = false +tmr.alarm(0,10000,0,startup) diff --git a/lua/joulu.lua b/lua/joulu.lua new file mode 100644 index 0000000..5c99abd --- /dev/null +++ b/lua/joulu.lua @@ -0,0 +1,79 @@ +RED = 6 +GREEN = 5 +BLUE = 7 +FREQ = 1000 +red_value = 150 +green_value = 55 +blue_value = 25 +mode = "0" -- 0 = trigger from pin, 1 = always on +lit_time = 60 +trigger_on = 0 +m = mqtt.Client("esppwmjoulu", 120) + +function split(input) + local arr={}; i = 1 + arr[4] = "0" + arr[5] = lit_time + for str in string.gmatch(input, "%S+") do + arr[i] = str + i = i + 1 + end + return arr[1], arr[2], arr[3], arr[4], arr[5] +end + +function led(r, g, b) + pwm.setduty(RED, r) + pwm.setduty(GREEN, g) + pwm.setduty(BLUE, b) +end + + +function trigger_change() + local rv = pwm.getduty(RED) + local gv = pwm.getduty(GREEN) + local bv = pwm.getduty(BLUE) + local rs = (red_value-rv)/200 + local gs = (green_value-gv)/200 + local bs = (blue_value-bv)/200 + print("Setting leds to new state") + for i=1, 200 do + rv = rv + rs + gv = gv + gs + bv = bv + bs + led(rv, gv, bv) + tmr.delay(10) + end + tmr.alarm(0, lit_time * 1000, 0, trigger_change) +end + +m:on("offline", function(con) + print ("Offline") + node.restart() +end) + +m:on("message", function(conn, topic, data) + print(topic .. " : " .. data) + if data ~= nil then + red_value, green_value, blue_value, mode, lit_time = split(data) + if mode == "1" then + led(red_value, green_value, blue_value) + end + end +end) + +m:connect("192.168.0.1", 1883, 0, 1, + function(client) + print("Connected to MQTT") + m:subscribe("joulupwm", 0, function(conn) print("joulupwm subscribe success") end) + end, + function(client, reason) + print("Failed to connect, reason: " .. reason) + node.restart() + end) + +pwm.setup(RED, FREQ, 0) +pwm.setup(GREEN, FREQ, 0) +pwm.setup(BLUE, FREQ, 0) +led(10, 10, 10) +print("Startup done") +trigger_change() diff --git a/lua/modds18b20.lua b/lua/modds18b20.lua new file mode 100644 index 0000000..de2d869 --- /dev/null +++ b/lua/modds18b20.lua @@ -0,0 +1,143 @@ +-------------------------------------------------------------------------------- +-- DS18B20 one wire module for NODEMCU +-- NODEMCU TEAM +-- LICENCE: http://opensource.org/licenses/MIT +-- Vowstar +-- 2015/02/14 sza2 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 diff --git a/lua/modules/telnet.lua b/lua/modules/telnet.lua new file mode 100644 index 0000000..88eea34 --- /dev/null +++ b/lua/modules/telnet.lua @@ -0,0 +1,36 @@ +-- a simple telnet server + +telnet_srv = net.createServer(net.TCP, 180) +telnet_srv:listen(2323, function(socket) + local fifo = {} + local fifo_drained = true + + local function sender(c) + if #fifo > 0 then + c:send(table.remove(fifo, 1)) + else + fifo_drained = true + end + end + + local function s_output(str) + table.insert(fifo, str) + if socket ~= nil and fifo_drained then + fifo_drained = false + sender(socket) + end + end + + node.output(s_output, 0) -- re-direct output to function s_ouput. + + socket:on("receive", function(c, l) + node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line + end) + socket:on("disconnection", function(c) + node.output(nil) -- un-regist the redirect output function, output goes to serial + end) + socket:on("sent", sender) + + print("Welcome to NodeMCU world.") +end) + diff --git a/lua/read_lux.lua b/lua/read_lux.lua new file mode 100644 index 0000000..8e54617 --- /dev/null +++ b/lua/read_lux.lua @@ -0,0 +1,9 @@ +SDA_PIN = 6 -- sda pin, GPIO12 +SCL_PIN = 5 -- scl pin, GPIO14 +bh1750 = require("bh1750") +bh1750.init(SDA_PIN, SCL_PIN) + +tmr.alarm(0, 1000, 1, function() + l = bh1750.read_lux() + print("lux: " .. l) +end) diff --git a/lua/rgb-pwm-joulu.lua b/lua/rgb-pwm-joulu.lua new file mode 100644 index 0000000..5c99abd --- /dev/null +++ b/lua/rgb-pwm-joulu.lua @@ -0,0 +1,79 @@ +RED = 6 +GREEN = 5 +BLUE = 7 +FREQ = 1000 +red_value = 150 +green_value = 55 +blue_value = 25 +mode = "0" -- 0 = trigger from pin, 1 = always on +lit_time = 60 +trigger_on = 0 +m = mqtt.Client("esppwmjoulu", 120) + +function split(input) + local arr={}; i = 1 + arr[4] = "0" + arr[5] = lit_time + for str in string.gmatch(input, "%S+") do + arr[i] = str + i = i + 1 + end + return arr[1], arr[2], arr[3], arr[4], arr[5] +end + +function led(r, g, b) + pwm.setduty(RED, r) + pwm.setduty(GREEN, g) + pwm.setduty(BLUE, b) +end + + +function trigger_change() + local rv = pwm.getduty(RED) + local gv = pwm.getduty(GREEN) + local bv = pwm.getduty(BLUE) + local rs = (red_value-rv)/200 + local gs = (green_value-gv)/200 + local bs = (blue_value-bv)/200 + print("Setting leds to new state") + for i=1, 200 do + rv = rv + rs + gv = gv + gs + bv = bv + bs + led(rv, gv, bv) + tmr.delay(10) + end + tmr.alarm(0, lit_time * 1000, 0, trigger_change) +end + +m:on("offline", function(con) + print ("Offline") + node.restart() +end) + +m:on("message", function(conn, topic, data) + print(topic .. " : " .. data) + if data ~= nil then + red_value, green_value, blue_value, mode, lit_time = split(data) + if mode == "1" then + led(red_value, green_value, blue_value) + end + end +end) + +m:connect("192.168.0.1", 1883, 0, 1, + function(client) + print("Connected to MQTT") + m:subscribe("joulupwm", 0, function(conn) print("joulupwm subscribe success") end) + end, + function(client, reason) + print("Failed to connect, reason: " .. reason) + node.restart() + end) + +pwm.setup(RED, FREQ, 0) +pwm.setup(GREEN, FREQ, 0) +pwm.setup(BLUE, FREQ, 0) +led(10, 10, 10) +print("Startup done") +trigger_change() diff --git a/lua/rgb-pwm.lua b/lua/rgb-pwm.lua index eef475b..f1af496 100644 --- a/lua/rgb-pwm.lua +++ b/lua/rgb-pwm.lua @@ -5,7 +5,7 @@ FREQ = 1000 red_value = 150 green_value = 55 blue_value = 25 -mode = "0" -- 0 = trigger from pin, 1 = always on +mode = "0" lit_time = 120 trigger_on = 0 m = mqtt.Client("esppwm", 120) @@ -55,9 +55,7 @@ function trigger_up() m:publish("kaytava_pir", "1", 2, 0, function(conn) print("Movement detected!") end) - if mode == "0" then - tmr.alarm(0, lit_time * 1000, 0, trigger_down) - end + tmr.alarm(0, lit_time * 1000, 0, trigger_down) if mode == "0" and trigger_on == 0 then trigger_on = 1 print("Setting leds on") diff --git a/lua/wc.lua b/lua/wc.lua new file mode 100644 index 0000000..cabbbe1 --- /dev/null +++ b/lua/wc.lua @@ -0,0 +1,77 @@ +PIR_PIN = 6 -- gpio12 +SW_PIN = 7 -- gpio13 +ABORT_PIN = 2 -- gpio04 +CHIPID = node.chipid() +counter = 0 + +function communicate(output) + http.post("http://api.t-ocdn.com/esp-kikkare", "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 + end) +end + +function startup() + if gpio.read(ABORT_PIN) == 0 then + print('Start aborted!') + return + end + print('Start.') + 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) + output = "start=1&node=" .. CHIPID + communicate(output) + end + end) +end + +function trigger_pir() + print("Movement detected!") + if closed == true and occupied == false then + print("Occupied!") + occupied = true + output = "occupied=1&node=" .. CHIPID + communicate(output) + end +end + +function trigger_switch() + state = gpio.read(SW_PIN) + print("Switch triggered to " .. state) + if state == gpio.HIGH then + closed = false + else + closed = true + end + if closed == false and occupied == true then + print("Freed") + occupied = false + output = "occupied=0&node=" .. CHIPID + communicate(output) + end +end + +gpio.mode(PIR_PIN, gpio.INPUT) +gpio.mode(SW_PIN, gpio.INPUT, gpio.PULLUP) +gpio.mode(ABORT_PIN, gpio.INPUT, gpio.PULLUP) +gpio.trig(PIR_PIN, "up", trigger_pir) +gpio.trig(SW_PIN, "both", trigger_switch) + +uart.setup(0, 9600, 8, 0, 1, 1) +closed = false +occupied = false +startup() diff --git a/lua/ws2812_mqtt_dual.lua b/lua/ws2812_mqtt_dual.lua index 198ceb2..6108c05 100644 --- a/lua/ws2812_mqtt_dual.lua +++ b/lua/ws2812_mqtt_dual.lua @@ -1,3 +1,16 @@ +tmr.alarm(5, 60000, 1, function() + lux = bh1750.read_lux() + output = "sisabh: " .. lux + m:publish("sisabh", output, 2, 0) +end) + +SDA_PIN = 6 -- sda pin, GPIO12 +SCL_PIN = 5 -- scl pin, GPIO14 +bh1750 = require("bh1750") +bh1750.init(SDA_PIN, SCL_PIN) +lux = bh1750.read_lux() +print("lux: " .. lux) + ws2812.init(ws2812.MODE_DUAL) ring = string.char(25, 0, 0) bar = string.char(0, 25, 0) @@ -13,8 +26,8 @@ m:on("connect", function(con) end) m:on("offline", function(con) - print ("Offline") - node.restart() + print("Offline, reconnecting in 10 s") + tmr.alarm(0,10000,0, function() m:connect("192.168.0.1", 1883, 0) end) end) m:on("message", function(conn, topic, data) @@ -29,3 +42,4 @@ m:on("message", function(conn, topic, data) end) m:connect("192.168.0.1", 1883, 0) + diff --git a/lua/ws2812_mqtt_dual_orig.lua b/lua/ws2812_mqtt_dual_orig.lua new file mode 100644 index 0000000..0636021 --- /dev/null +++ b/lua/ws2812_mqtt_dual_orig.lua @@ -0,0 +1,30 @@ +ws2812.init(ws2812.MODE_DUAL) +ring = string.char(25, 0, 0) +bar = string.char(0, 25, 0) +ws2812.write(ring, bar) + +m = mqtt.Client("dualespws", 120) +m:on("connect", function(con) + print ("Connected") + m:subscribe("lampos",0, function(conn) print("lampos subscribe success") end) + tmr.alarm(0, 1000, 0, function () + m:subscribe("rinkula",0, function(conn) print("rinkula subscribe success") end) + end) +end) + +m:on("offline", function(con) + m:connect("192.168.0.1", 1883, 0) +end) + +m:on("message", function(conn, topic, data) + if data ~= nil then + if topic == "rinkula" then + ring = data + else + bar = data + end + ws2812.write(ring, bar) + end +end) + +m:connect("192.168.0.1", 1883, 0) diff --git a/lua/ws2812_mqtt_haa.lua b/lua/ws2812_mqtt_haa.lua new file mode 100644 index 0000000..8a3bc70 --- /dev/null +++ b/lua/ws2812_mqtt_haa.lua @@ -0,0 +1,25 @@ +ws2812.init() +ring = string.char(25, 0, 0) +ws2812.write(ring) + +m = mqtt.Client("dualring", 120) +m:on("connect", function(con) + print ("Connected") + m:subscribe("rinkula",0, function(conn) print("rinkula subscribe success") end) +end) + +m:on("offline", function(con) + print("Offline, reconnecting in 10 s") + tmr.alarm(0,10000,0, function() m:connect("172.25.0.1", 1883, 0) end) +end) + +m:on("message", function(conn, topic, data) + if data ~= nil then + if topic == "rinkula" then + ring = data + end + ws2812.write(ring) + end +end) + +m:connect("172.25.0.1", 1883, 0)