initial commit with all the stuff
This commit is contained in:
289
3B/archive/root_12.bak/run_logi.lua
Normal file
289
3B/archive/root_12.bak/run_logi.lua
Normal file
@@ -0,0 +1,289 @@
|
||||
-- LUA default run file
|
||||
|
||||
-- LUA default run file
|
||||
|
||||
require "env"
|
||||
panel = {}
|
||||
require "panel"
|
||||
|
||||
socket = require('socket')
|
||||
PLC = require('vwago')
|
||||
Scheduler = require('scheduler')
|
||||
--I2CClass = require('periphery').I2C
|
||||
I2CDevice = nil;
|
||||
MQTT = require("mqtt")
|
||||
|
||||
daq = {};
|
||||
|
||||
local function errLog(msg)
|
||||
io.stderr:write(msg)
|
||||
end
|
||||
|
||||
function logPrint(msg)
|
||||
print(msg);
|
||||
end
|
||||
|
||||
local function pubPanelConfig()
|
||||
MQTT.publish(mqttRoot.."/panel/label/button/send",panel.LabelButtonSend[1])
|
||||
MQTT.publish(mqttRoot.."/panel/label/button/0",panel.LabelButton_1[1])
|
||||
MQTT.publish(mqttRoot.."/panel/label/button/1",panel.LabelButton_2[1])
|
||||
MQTT.publish(mqttRoot.."/panel/label/button/2",panel.LabelButton_3[1])
|
||||
MQTT.publish(mqttRoot.."/panel/label/button/3",panel.LabelButton_4[1])
|
||||
MQTT.publish(mqttRoot.."/panel/label/header",panel.LabelHeader[1])
|
||||
MQTT.publish(mqttRoot.."/panel/options/samples",panel.OptionsSamples[1])
|
||||
MQTT.publish(mqttRoot.."/panel/options/xaxis/max",panel.OptionsXaxisMax[1])
|
||||
MQTT.publish(mqttRoot.."/panel/options/xaxis/min",panel.OptionsXaxisMin[1])
|
||||
MQTT.publish(mqttRoot.."/panel/options/yaxis/0/max",panel.OptionsXaxis_1_Max[1])
|
||||
MQTT.publish(mqttRoot.."/panel/options/yaxis/1/max",panel.OptionsXaxis_2_Max[1])
|
||||
MQTT.publish(mqttRoot.."/panel/options/yaxis/1/min",panel.OptionsXaxis_2_Min[1])
|
||||
MQTT.publish(mqttRoot.."/panel/options/yaxis/0/min",panel.OptionsYaxis_1_Min[1])
|
||||
MQTT.publish(mqttRoot.."/panel/text/send/message", panel.TextSendMessage[1])
|
||||
MQTT.publish(mqttRoot.."/panel/text/send/topic",panel.TextSendTopic[1])
|
||||
MQTT.publish(mqttRoot.."/panel/debug",panel.Debug["alt"][panel.Debug["idx"]])
|
||||
end
|
||||
|
||||
MQTT.onConnect (
|
||||
function()
|
||||
errLog("MQTT:\t Connect\n");
|
||||
MQTT.publish(mqttRoot.."/info", "PIALU")
|
||||
MQTT.subscribe(mqttRoot.."/ctrl/#")
|
||||
end
|
||||
)
|
||||
|
||||
MQTT.onMessage (
|
||||
function(mid, topic, payload)
|
||||
logPrint("MQTT:\t Message "..topic, payload)
|
||||
|
||||
subTopic = string.match(topic,mqttRoot.."/ctrl/(%w+)")
|
||||
if subTopic then
|
||||
if subTopic == "cmd" then
|
||||
logPrint("MQTT:\t Command");
|
||||
analogChannel = string.match(topic,mqttRoot.."/ctrl/cmd/analog/(%w)")
|
||||
if analogChannel then
|
||||
|
||||
if analogChannel == "0" then
|
||||
logPrint("MQTT:\t Command Analog Channel "..analogChannel.." "..payload);
|
||||
if payload == "on" then
|
||||
channel_0 = true
|
||||
MQTT.publish(mqttRoot.."/status/analog/0", "on",true)
|
||||
else
|
||||
channel_0 = false
|
||||
MQTT.publish(mqttRoot.."/status/analog/0", "off",true)
|
||||
end
|
||||
elseif analogChannel == "1" then
|
||||
if payload == "on" then
|
||||
channel_1 = true
|
||||
MQTT.publish(mqttRoot.."/status/analog/1", "on",true)
|
||||
else
|
||||
channel_1 = false
|
||||
MQTT.publish(mqttRoot.."/status/analog/1", "off",true)
|
||||
end
|
||||
elseif analogChannel == "2" then
|
||||
if payload == "on" then
|
||||
channel_2 = true
|
||||
MQTT.publish(mqttRoot.."/status/analog/2", "on",true)
|
||||
else
|
||||
channel_2 = false
|
||||
MQTT.publish(mqttRoot.."/status/analog/2", "off",true)
|
||||
end
|
||||
elseif analogChannel == "3" then
|
||||
if payload == "on" then
|
||||
channel_3 = true
|
||||
MQTT.publish(mqttRoot.."/status/analog/3", "on",true)
|
||||
else
|
||||
channel_3 = false
|
||||
MQTT.publish(mqttRoot.."/status/analog/3", "off",true)
|
||||
end
|
||||
end
|
||||
elseif string.match(topic,mqttRoot.."/ctrl/cmd/panel/config") then
|
||||
if string.match(payload,"publish") then
|
||||
pubPanelConfig()
|
||||
elseif string.match(payload,"apply") then
|
||||
panel = {}
|
||||
dofile("panel.lua")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
|
||||
local function getInt32(bytes,offset)
|
||||
if (offset >= 0) and ((#bytes-offset) >= 4) then
|
||||
num = bytes[offset+1] + bit32.lshift(bytes[offset+2],8) + bit32.lshift(bytes[offset+3],16) + bit32.lshift(bytes[offset+4],24);
|
||||
if ( num > 0x80000000 ) then
|
||||
return num - 0x100000000;
|
||||
else
|
||||
return num;
|
||||
end
|
||||
end
|
||||
|
||||
return 9999999999;
|
||||
end
|
||||
|
||||
local function getInt16(bytes,offset)
|
||||
if (offset >= 0) and ((#bytes-offset) >= 2) then
|
||||
num = bytes[offset+1] + bit32.lshift(bytes[offset+2],8);
|
||||
end
|
||||
|
||||
if ( num > 0x8000 ) then
|
||||
return num - 0x10000;
|
||||
else
|
||||
return num;
|
||||
end
|
||||
|
||||
return 9999999999;
|
||||
end
|
||||
|
||||
local function getUint8(bytes,offset)
|
||||
if (offset >= 0) and ((#bytes-offset) >= 1) then
|
||||
return bytes[offset+1];
|
||||
end
|
||||
|
||||
return 999999999;
|
||||
end
|
||||
--[[
|
||||
local function i2cBegin()
|
||||
I2CDevice = I2CClass("/dev/i2c-1")
|
||||
end
|
||||
|
||||
local function i2cRecvMsg()
|
||||
local msg = {{
|
||||
0x00, -- uint8_t hartbeat;
|
||||
0x00, 0x00, 0x00, 0x00, -- int32_t data_32;
|
||||
0x00, 0x00, 0x00, 0x00, -- int32_t data_32;
|
||||
0x00, 0x00, 0x00, 0x00, -- int32_t data_32;
|
||||
0x00, 0x00, 0x00, 0x00, -- int32_t data_32;
|
||||
flags = I2CClass.I2C_M_RD
|
||||
}}
|
||||
I2CDevice:transfer(6, msg)
|
||||
-- print(table.unpack(msg[1]));
|
||||
return msg[1]
|
||||
end
|
||||
|
||||
local function i2cSendMsg(cmd)
|
||||
local msg = {{cmd, 0x00, 0x00, 0x00, 0x00, flags = 0}}
|
||||
I2CDevice:transfer(4, msg)
|
||||
end
|
||||
--]]
|
||||
local function recv()
|
||||
--[[
|
||||
daqData = i2cRecvMsg();
|
||||
-- print(table.unpack(daqData));
|
||||
daq.hartbeat = getUint8(daqData,0);
|
||||
daq.sum0 = getInt32(daqData,1);
|
||||
daq.sum1 = getInt32(daqData,5);
|
||||
daq.sum2 = getInt32(daqData,9);
|
||||
daq.sum3 = getInt32(daqData,13);
|
||||
--]]
|
||||
daq.hartbeat = 123;
|
||||
daq.sum0 = 3;
|
||||
daq.sum1 = 1.7;
|
||||
daq.sum2 = 9;
|
||||
daq.sum3 = 5.5;
|
||||
end
|
||||
|
||||
function setup()
|
||||
mqttBroker = app.mqttBroker[1]
|
||||
mqttRoot = app.mqttRoot[1]
|
||||
|
||||
instance = HMI.App.instance;
|
||||
layout = HMI.Layout(instance,32,24);
|
||||
|
||||
label = HMI.Label(layout,0,0,32,3,"Plot");
|
||||
label:setTextColor(HMI.Color.white);
|
||||
label:setBackground(HMI.Color.blue);
|
||||
|
||||
chart = HMI.Timechart(layout,0,4,32,16)
|
||||
chart:setBaseline(0)
|
||||
chart:setValuePerDivison(2)
|
||||
chart:setSamplesPerDivison(20)
|
||||
chart:setSamplesFormat("%d Samples/Div, Samplerate 0.5s <=> 10 Sekunden/Div")
|
||||
|
||||
txt0 = HMI.Label(layout, 1,20,6,2,"Ch 0");
|
||||
txt0:getTextObject():setAlign(HMI.ALIGN_LEFT);
|
||||
txt1 = HMI.Label(layout,17,20,6,2,"Ch 1");
|
||||
txt1:getTextObject():setAlign(HMI.ALIGN_LEFT);
|
||||
txt2 = HMI.Label(layout, 1,22,6,2,"Ch 2");
|
||||
txt2:getTextObject():setAlign(HMI.ALIGN_LEFT);
|
||||
txt3 = HMI.Label(layout,17,22,6,2,"Ch 3");
|
||||
txt3:getTextObject():setAlign(HMI.ALIGN_LEFT);
|
||||
|
||||
out0 = HMI.Output(layout, 7,20,6,2,"0000");
|
||||
out1 = HMI.Output(layout,23,20,6,2,"0000");
|
||||
out2 = HMI.Output(layout, 7,22,6,2,"0000");
|
||||
out3 = HMI.Output(layout,23,22,6,2,"0000");
|
||||
|
||||
updateTimer = Scheduler.Timer(500)
|
||||
|
||||
-- i2cBegin()
|
||||
|
||||
if mqttBroker then
|
||||
errLog("MQTT:\t Begin\n");
|
||||
MQTT.begin(mqttBroker)
|
||||
end
|
||||
|
||||
MQTT.loop()
|
||||
|
||||
channel_0 = true;
|
||||
channel_1 = true;
|
||||
channel_2 = true;
|
||||
channel_3 = true;
|
||||
|
||||
MQTT.publish(mqttRoot.."/status/analog/0", "on",true)
|
||||
MQTT.publish(mqttRoot.."/status/analog/1", "on",true)
|
||||
MQTT.publish(mqttRoot.."/status/analog/2", "on",true)
|
||||
MQTT.publish(mqttRoot.."/status/analog/3", "on",true)
|
||||
end
|
||||
|
||||
function loop()
|
||||
MQTT.loop()
|
||||
|
||||
if updateTimer.elapsed() then
|
||||
updateTimer.restart()
|
||||
recv()
|
||||
|
||||
val0 = daq.sum0*0.0000707750682632
|
||||
val1 = daq.sum1*0.0000707750682632
|
||||
val2 = daq.sum2*0.0000208350713273
|
||||
val3 = daq.sum3*0.0000208350713273
|
||||
|
||||
out0:setText(string.format("%5.2f",val0))
|
||||
out1:setText(string.format("%5.2f",val1))
|
||||
out2:setText(string.format("%5.2f",val2))
|
||||
out3:setText(string.format("%5.2f",val3))
|
||||
chart:plot(daq.sum0*0.0000707750682632)
|
||||
--chart:plot(daq.sum1*0.0000707750682632)
|
||||
--chart:plot(daq.sum2*0.0000208350713273)
|
||||
--chart:plot(daq.sum3*0.0000208350713273)
|
||||
|
||||
compound = ""
|
||||
|
||||
if channel_0 then
|
||||
MQTT.publish(mqttRoot.."/analog/0",val0)
|
||||
compound = compound..",0:"..val0
|
||||
end
|
||||
if channel_1 then
|
||||
MQTT.publish(mqttRoot.."/analog/1",val1)
|
||||
compound = compound..",1:"..val1
|
||||
end
|
||||
if channel_2 then
|
||||
MQTT.publish(mqttRoot.."/analog/2",val2)
|
||||
compound = compound..",2:"..val2
|
||||
end
|
||||
if channel_3 then
|
||||
MQTT.publish(mqttRoot.."/analog/3",val3)
|
||||
compound = compound..",3:"..val3
|
||||
end
|
||||
all = string.sub(compound,2)
|
||||
if string.len(all) > 0 then
|
||||
MQTT.publish(mqttRoot.."/analog/all",all)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
io.stderr:write("Run LUA\n");
|
||||
|
||||
--------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user