mirror of https://github.com/iotcat/wIoT
Compare commits
2 Commits
244fd920c6
...
197d16cc13
Author | SHA1 | Date |
---|---|---|
IoTcat | 197d16cc13 | 3 years ago |
IoTcat | 90ceb57392 | 3 years ago |
15 changed files with 487 additions and 74 deletions
@ -0,0 +1,32 @@ |
||||
//import wiot compiler
|
||||
const wiot = require('../../src/cli/compiler.js'); |
||||
|
||||
//node ID array
|
||||
let nids = ['7534c', 'b840a', '1d17a', '9f163', '8908c', '39747', '308b4', 'd689e', '30e3f']; |
||||
|
||||
//node object array constructed from the above node IDs
|
||||
let nodes = nids.map(nid=>new wiot.node.nodemcu(nid)); |
||||
//create a virtual wire
|
||||
let w_all = new wiot.wire(); |
||||
|
||||
//for each node in the nodes array, do
|
||||
nodes.forEach((node, index) => { |
||||
//if(!index){//if is the first node (index=0, nid='7534c'), do
|
||||
//create a gpio input listener on D3 pin and connect to the virtual wire w
|
||||
//D3 pin is connected to an on-board button on NodeMCU
|
||||
|
||||
//}else{//if is not the first node (index!=0), do
|
||||
//create another virtual wire
|
||||
let w = new wiot.wire(); |
||||
wiot.buffer(w_all/*output wire*/, w/*input wire*/, node); |
||||
wiot.gpio(wiot.INPUT, node.D3, w/*output wire*/, node); |
||||
//implement a buffer module so only when signal in wire has changed and held
|
||||
//for 0.2*index seconds, the w_delay will be updated with the value in the w
|
||||
|
||||
//create a gpio output module on D4 pin, controlled by the w_delayed virtual wire
|
||||
//D4 is connected to an on-board LED on NodeMCU
|
||||
wiot.gpio(wiot.OUTPUT, node.D4, w_all, node); |
||||
//}
|
||||
}); |
||||
|
||||
|
@ -0,0 +1,30 @@ |
||||
//import wiot compiler
|
||||
const wiot = require('../../src/cli/compiler.js'); |
||||
|
||||
//node ID array
|
||||
let nids = ['7534c', 'b840a', '1d17a', '9f163', '8908c', '39747', '308b4', 'd689e', '30e3f']; |
||||
|
||||
//node object array constructed from the above node IDs
|
||||
let nodes = nids.map(nid=>new wiot.node.nodemcu(nid)); |
||||
//create a virtual wire
|
||||
let w = new wiot.wire(); |
||||
|
||||
//for each node in the nodes array, do
|
||||
nodes.forEach((node, index) => { |
||||
if(!index){//if is the first node (index=0, nid='7534c'), do
|
||||
//create a gpio input listener on D3 pin and connect to the virtual wire w
|
||||
//D3 pin is connected to an on-board button on NodeMCU
|
||||
wiot.gpio(wiot.INPUT, node.D3, w/*output wire*/, node); |
||||
}else{//if is not the first node (index!=0), do
|
||||
//create another virtual wire
|
||||
let w_delayed = new wiot.wire(); |
||||
//implement a buffer module so only when signal in wire has changed and held
|
||||
//for 0.2*index seconds, the w_delay will be updated with the value in the w
|
||||
wiot.buffer(w_delayed/*output wire*/, w/*input wire*/, node, .2*index/*delay*/); |
||||
//create a gpio output module on D4 pin, controlled by the w_delayed virtual wire
|
||||
//D4 is connected to an on-board LED on NodeMCU
|
||||
wiot.gpio(wiot.OUTPUT, node.D4, w_delayed, node); |
||||
} |
||||
}); |
||||
|
||||
|
@ -0,0 +1,29 @@ |
||||
//import wiot compiler
|
||||
const wiot = require('../../src/cli/compiler.js'); |
||||
|
||||
//node ID array
|
||||
let nids = ['7534c', 'b840a', '1d17a', '9f163', '8908c',
|
||||
'39747', '308b4', 'd689e', '30e3f', '0d672']; |
||||
|
||||
//node object array constructed from the above node IDs
|
||||
let nodes = nids.map(nid=>new wiot.node.nodemcu(nid)); |
||||
//create a virtual wire
|
||||
let w = new wiot.wire(), |
||||
w1 = new wiot.wire(), |
||||
w2 = new wiot.wire(); |
||||
|
||||
let node = nodes[0]; |
||||
let node1 = nodes[1]; |
||||
wiot.gpio(wiot.INPUT, node.D3, w1, node); |
||||
wiot.gpio(wiot.INPUT, node1.D3, w2, node1); |
||||
|
||||
//or ($0+$1)%2+math.floor(($0+$1)/2)
|
||||
//and math.floor(($0+$1)/2)
|
||||
//xor ($0+$1)%2
|
||||
//not ($0+1)%2
|
||||
wiot.operate('(($0+$1)%2+1)%2', node, w, w1, w2); |
||||
|
||||
wiot.gpio(wiot.OUTPUT, node.D4, w, node); |
||||
|
||||
//wiot.pwm(nodes[0].D4, new wiot.wire(800), new wiot.wire(500), nodes[0]);
|
||||
|
@ -0,0 +1,36 @@ |
||||
//import wiot compiler
|
||||
const wiot = require('../../src/cli/compiler.js'); |
||||
|
||||
//node ID array
|
||||
let nids = ['7534c', 'b840a', '1d17a', '9f163', '8908c',
|
||||
'39747', '308b4', 'd689e', '30e3f', '0d672']; |
||||
|
||||
//node object array constructed from the above node IDs
|
||||
let nodes = nids.map(nid=>new wiot.node.nodemcu(nid)); |
||||
//create a virtual wire
|
||||
let w = new wiot.wire(0), |
||||
w1 = new wiot.wire(), |
||||
w2 = new wiot.wire(); |
||||
|
||||
let node = nodes[0]; |
||||
let node1 = nodes[1]; |
||||
/* wiot.gpio(wiot.INPUT, node.D3, w1, node); |
||||
wiot.gpio(wiot.INPUT, node1.D3, w2, node1); |
||||
|
||||
//or ($0+$1)%2+math.floor(($0+$1)/2)
|
||||
//and math.floor(($0+$1)/2)
|
||||
//xor ($0+$1)%2
|
||||
//not ($0+1)%2
|
||||
wiot.operate('(($0+$1)%2+1)%2', node, w, w1, w2); |
||||
|
||||
wiot.gpio(wiot.OUTPUT, node.D4, w, node); |
||||
*/ |
||||
|
||||
//wiot.gpio(wiot.INPUT, node1.D3, w2, node1);
|
||||
wiot.adc(w1, node); |
||||
//wiot.gpio(wiot.OUTPUT, node.D4, w, node);
|
||||
//wiot.operate('math.floor($0/600)', node, w, w1);
|
||||
wiot.operate('($0/2)+300', node, w, w1); |
||||
|
||||
wiot.pwm(nodes[0].D4, w, new wiot.wire(500), nodes[0]); |
||||
|
@ -0,0 +1 @@ |
||||
data/ |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,72 @@ |
||||
local M |
||||
do |
||||
-- const |
||||
local REDIS_PORT = 6379 |
||||
-- cache |
||||
local pairs, tonumber = pairs, tonumber |
||||
-- |
||||
local publish = function(self, chn, s) |
||||
self._fd:send(("*3\r\n$7\r\npublish\r\n$%d\r\n%s\r\n$%d\r\n%s\r\n"):format( |
||||
#chn, chn, #s, s |
||||
)) |
||||
-- TODO: confirmation? then queue of answers needed |
||||
end |
||||
local subscribe = function(self, chn, handler) |
||||
-- TODO: subscription to all channels, with single handler |
||||
self._fd:send(("*2\r\n$9\r\nsubscribe\r\n$%d\r\n%s\r\n"):format( |
||||
#chn, chn |
||||
)) |
||||
self._handlers[chn] = handler |
||||
-- TODO: confirmation? then queue of answers needed |
||||
end |
||||
local unsubscribe = function(self, chn) |
||||
self._handlers[chn] = false |
||||
end |
||||
-- NB: pity we can not just augment what net.createConnection returns |
||||
local close = function(self) |
||||
self._fd:close() |
||||
end |
||||
local connect = function(host, port) |
||||
local _fd = net.createConnection(net.TCP, 0) |
||||
local self = { |
||||
_fd = _fd, |
||||
_handlers = { }, |
||||
-- TODO: consider metatables? |
||||
close = close, |
||||
publish = publish, |
||||
subscribe = subscribe, |
||||
unsubscribe = unsubscribe, |
||||
} |
||||
_fd:on("connection", function() |
||||
--print("+FD") |
||||
end) |
||||
_fd:on("disconnection", function() |
||||
-- FIXME: this suddenly occurs. timeout? |
||||
--print("-FD") |
||||
end) |
||||
_fd:on("receive", function(fd, s) --luacheck: no unused |
||||
--print("IN", s) |
||||
-- TODO: subscription to all channels |
||||
-- lookup message pattern to determine channel and payload |
||||
-- NB: pairs() iteration gives no fixed order! |
||||
for chn, handler in pairs(self._handlers) do |
||||
local p = ("*3\r\n$7\r\nmessage\r\n$%d\r\n%s\r\n$"):format(#chn, chn) |
||||
if s:find(p, 1, true) then |
||||
-- extract and check message length |
||||
-- NB: only the first TCP packet considered! |
||||
local _, start, len = s:find("(%d-)\r\n", #p) |
||||
if start and tonumber(len) == #s - start - 2 and handler then |
||||
handler(chn, s:sub(start + 1, -2)) -- ends with \r\n |
||||
end |
||||
end |
||||
end |
||||
end) |
||||
_fd:connect(port or REDIS_PORT, host) |
||||
return self |
||||
end |
||||
-- expose |
||||
M = { |
||||
connect = connect, |
||||
} |
||||
end |
||||
return M |
Loading…
Reference in new issue