This commit is contained in:
cqc
2021-07-26 19:19:08 -05:00
parent 5454e0d9e5
commit 4fbb3e0213
6 changed files with 163 additions and 11 deletions

7
lua/.nodemcutool Normal file
View File

@ -0,0 +1,7 @@
{
"baudrate": "115200",
"port": "/dev/ttyUSB0",
"minify": false,
"compile": false,
"keeppath": false
}

59
lua/8-mosfet.lua Normal file
View File

@ -0,0 +1,59 @@
sda = 23 -- pins as on Adafruit Huzzah32 silkscreen
scl = 22
id = i2c.HW0 -- hardware interface
speed = i2c.SLOW
-- values for TCA9534A
i2c_addr = 0x3F
-- initialize i2c software interface
i2c.setup(id, sda, scl, speed)
port_input = 0x00
port_output = 0x01
reg_polarity = 0x02
reg_config = 0x03
function write_reg(addr, reg, data, callback)
i2c.start(id)
i2c.address(id, dev_address, i2c.TRANSMITTER)
i2c.write(id, reg, data, true)
i2c.stop(id)
return i2c.transfer(id, callback, 5000)
end
function read_reg(dev_address, dev_register, callback)
i2c.start(id)
i2c.address(id, dev_address, i2c.TRANSMITTER)
i2c.write(id, dev_register)
i2c.start(id) -- repeated start condition
i2c.address(id, dev_address, i2c.RECEIVER)
i2c.read(id, 1)
i2c.stop(id)
return i2c.transfer(id, callback, 5000)
end
write_reg(i2c_addr, reg_config, 0x00, function(_,_) print("8-mosfet: reg_config done") end)
function set_mosfet(value)
write_reg(i2c_addr, reg_config, value, function(_,a)
print("set mosfet")
end)
end
mosfet_value = 0xFF
function publish_status(value)
mosfet_value = read_reg(i2c_addr, reg_output)
for i = 0,8 do
oskar_m:publish("/shannon/kontrol/relay/"..i, bit.band(bit.rshift(mosfet_value, i), 0x1), 0, 0)
end
end
oskar_topichandle["/shannon/kontrol/relay"] = function (client, topic, data)
n = tonumber(string.sub(topic, -1))
if n<8 and n>=0 then
if data then mosfet_value = bit.band(mosfet_value, bit.lshift(1, n))
else mosfet_value = bit.bor(mosfet_value, bit.bnot(bit.lshift(1, n))) end
set_mosfet(mosfet_value)
end
end

View File

@ -1,2 +1,5 @@
upload: init.lua
luatool.py --ip 192.168.1.174:2323 -vd -f init.lua
check: *.lua
./../nodemcu-firmware-esp32/build/luac_cross/luac.cross -p $? && touch $@

View File

@ -7,6 +7,8 @@ end
tryfile('display.lua')
tryfile('wifi.lua')
tryfile('kontrol.lua')
function reload() tryfile('init.lua') end
function ls() for k,v in pairs(file.list()) do print(k.."\t\tsize: "..v) end end

10
lua/kontrol.lua Normal file
View File

@ -0,0 +1,10 @@
mqtt = tryfile("mqtt.lua")
fet8 = tryfile("8-mosfet.lua")
mqtt:publish("/shannon/kontrol/status", "hello", 0, 0, function(client) print("sent hello") end)

View File

@ -1,29 +1,96 @@
-- mqtt.lua
local M = {} -- public interface
function M:on_connect(client, topic, message)
self.connected = true
end
function M:on_offline(client, topic, message)
self.connected = false
end
function M:on_message(client, topic, message)
end
function M:on_newconnection()
end
function M:subscribe(topic, qos)
self.subscriptions[topic] = qos
self.attempt_subscribes()
end
function attmpet_subscribes()
local uv_topic, uv_qos = next(self.subscriptions, nil) -- more upvals
m = mqtt.Client(hostname, 120)
local function subscribe_cb(client)
print("subscribed to topic", uv_topic)
uv_topic, uv_qos = next(uv_topics, uv_topic)
if uv_topic ~= nil and uv_qos ~= nil then
client:subscribe(uv_topic, uv_qos, subscribe_cb)
else
uv_cb(client)
end
end
m:lwt("/lwt", "offline", 0, 0)
m:on("connect", function(client) print ("connected") end)
m:on("offline", function(client) print ("offline") end)
client:subscribe(uv_topic, uv_qos, subscribe_cb)
end
m:on("message", function(client, topic, data)
function M:connect(host, port)
self.m:connect(host, port, 0,
function(client)
self.connected = true
self.on_newconnection()
end,
function(client, reason)
print("mqtt "..name.." failed to connect: "..reason)
tmr.create():alarm(10*1000, tmr.ALARM_SINGLE, self.connect(host, port))
end
)
end
setmetatable(
M, { __call = function (cls, name)
local self = setmetatable({}, MyClass)
self.m = mqtt.Client(name, 120)
self.name = name
self.connected = false
self.m:on("connect", M.on_connect)
self.m:on("offline", M.on_offline)
self.m:on("message", M.on_message)
return self
end,
})
oskar_m:lwt("/lwt", "offline", 0, 0)
oskar_m:on("connect", function(client) print ("connected") end)
oskar_m:on("offline", function(client) print ("offline") end)
oskar_topicsub = {
}
oskar_topichandle = {
status=(function (client, topic, message)
print("Handle status") end),
}
oskar_m:on("message", function(client, topic, data)
print("MQTT: msg recieved: "..topic)
if data ~= nil then
print(data)
end
oskar_topichandle[topic](client,topic,message)
end)
-- for TLS: m:connect("192.168.11.118", secure-port, 1)
m:connect("departmentofinter.net", 1883, 0, function(client)
oskar_m:connect("172.105.7.171", 1883, 0, function(client)
print("connected")
-- Calling subscribe/publish only makes sense once the connection
-- was successfully established. You can do that either here in the
-- 'connect' callback or you need to otherwise make sure the
-- connection was established (e.g. tracking connection status or in
-- m:on("connect", function)).
-- subscribe topic with qos = 0
client:subscribe("/topic", 0, function(client) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
@ -33,5 +100,9 @@ function(client, reason)
print("failed reason: " .. reason)
end)
m:close();
-- you can call m:connect again
return M