Lua: publish different sensors data simultaneously to topic - lua

I am able to publish a data of different sensors (like temperature value and motion detection as zero or one) one at a time on a specific topic ( topic/temp/motion) using Lua code and subscribe in on my android app however i am not able to publish it simultaneously on same topic or sub-topic. Some ideas or examples would be great.
Below are some major bits of Lua code used.
orgID = "quickstart" -- IoT Foundation organization ID
broker = "test.mosquitto.org" --orgID..".messaging.internetofthings.ibmcloud.com" -- IP or hostname of IoTF service
mqttPort = 1883 -- MQTT port (default 1883: non-secure)
userID = "" -- blank for quickstart
userPWD = "" -- blank for quickstart
macID = "18fe34e1b007" -- unique Device ID or Ethernet Mac Address <==== Modify this!
clientID = ":esp8266:18fe34e1b007" -- Client ID
count = 0 -- Test number of mqtt_do cycles
mqttState = 0 -- State control
topic = "topic/temp/motion"
led = 4
--gpio.mode(led,gpio.OUTPUT)
--dht sensor settings------------------------------
pin = 1
-- PIR initialization section
pir = 2
x= 0 -- variable for sending motion detection information in "0 or 1"
function DHT_do()
status, temp, humi, temp_dec, humi_dec = dht.read(pin)
--gpio.write(led, gpio.LOW)
end
function mqtt_do()
count = count + 1 -- tmr.alarm counter
------------------------------------------- pir conditional code
if gpio.read(pir) ~= last_state then
last_state = gpio.read(pir)
if last_state == 1 then
print("ON")
x = 1
gpio.write(led,gpio.HIGH)
else
print("OFF")
x = 0
gpio.write(led,gpio.LOW)
m:publish(topic,x, 0, 0,
function(conn)
print(x)
print("temp_data:"..temp)

Related

/Lua/ How to do this (idk how to call that lol)

I need to make a trolleybus number, which won't repeat for game. For example, there is a number "101" and there musn't be more "101". How to do that? I have a code, but I know, he won't work and I won't test it lol
function giveNumber()
local number = math.random(100, 199)
local takedNumbers = {}
local i = 0
local massiv = i+1
script.Parent.pered.SurfaceGui.TextLabel.Text = number
script.Parent.zad.SurfaceGui.TextLabel.Text = number
script.Parent.levo.SurfaceGui.TextLabel.Text = number
script.Parent.pravo.SurfaceGui.TextLabel.Text = number
takedNumbers[massiv] = {number}
end
script.Parent.Script:giveNumber() // what I wrote here? idk...
if number == takedNumbers[massiv] then
giveNumber()
end
i didn't test it, because I think it won't work because this code is something bad
I think this will serve your needs.
In the function generateUniqueNumber, the script loops until it found a number that is not yet in the array. (in other words, that it hasn't given out yet)
Once it found that number, it will insert it into the table to remember that it has given it out, and then it will return the number.
Then on the bottom of the script we just give the numbers to the buses :-)
--[[
Goal: Give all buses a unique number
]]
-- Variables
local takenNumbers = {};
-- This function returns a random number in the range [100, 199] that has not been taken yet
function generateUniqueNumber()
local foundNumber = false;
while not foundNumber do
randomNumber = math.random(100, 199);
if not table.find(takenNumbers, randomNumber) then
table.insert(takenNumbers, randomNumber);
return randomNumber;
end
end
end
-- This function sets the number of the bus
script.Parent.pered.SurfaceGui.TextLabel.Text = tostring(generateUniqueNumber());
script.Parent.zad.SurfaceGui.TextLabel.Text = tostring(generateUniqueNumber());
script.Parent.levo.SurfaceGui.TextLabel.Text = tostring(generateUniqueNumber());
script.Parent.pravo.SurfaceGui.TextLabel.Text = tostring(generateUniqueNumber());
2 things:
I didn't test this code as Roblox is not installed on the pc I'm currently on.
Please try formatting your code nicely next time. It greatly improves the readability! For example, you can use this website:
https://codebeautify.org/lua-beautifier
Simpler
Fill a table with free numbers...
local freenumbers = {}
for i = 1, 99 do freenumbers[i] = i + 100 end
...for every new takennumbers use table.remove() on freenumbers
local takennumbers = {}
if #freenumbers > 0 then
takennumbers[#takennumbers + 1] = table.remove(freenumbers, math.random(1, #freenumbers))
end

Roblox money distribution system not working

enter image description hereI was trying to make a money collection system for the person on the stone grey team, but it won't work. What it should do is take the wealth value from the province, multiply by 0.02 and add that onto the total amount of money for the person. The leaderboard has the 'Finances' on there, but the value stays at 0 no matter what. The output bar is also blank.
local map = game.Workspace.map
local rp = game:GetService("ReplicatedStorage")
local provinces = {
map.province1,
map.province2,
map.province3,
map.province4,
map.province5,
map.province6,
map.province7,
map.province8,
map.province9
}
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("BoolValue")
stats.Name = "leaderstats"
stats.Parent = plr
local finances = Instance.new("NumberValue")
finances.Name = "Finances"
finances.Parent = plr.leaderstats
end)
-- below here i have problems, above this seems to work normally
rp.TaxPaymentsCheque.OnServerEvent:Connect(function(plr)
for start = 1, #provinces do
local provincegrowing = provinces[start]
if provincegrowing.BrickColor == BrickColor.new("Dark stone grey") then
local finances = plr.leaderstats.Finances.Value
local wealth = provincegrowing.ProvinceWealth.Value
local taxmodifier = 0.02
local revenue = wealth * taxmodifier
local oldfinanceamount = finances
finances = oldfinanceamount + revenue
end
end
end)
There is also a picture if it can be of any use.
So the reason why you don't see any change is because you're not actually changing the Finances Value
Instead what you are doing is saving Finances' Value to a variable and changing the variable
local number = SomeIntValue.Value
number = number * 5
-- is practically the same as saying
local number = 1
number = number * 5
It doesn't actually set the IntValue's value
To do that, do something like this
local number = SomeIntValue.Value
number = number * 5
SomeIntValue.Value = number
-- or
SomeIntValue.Value = SomeIntValue.Value * 5

Tshark - How to show the Dissection Tree of tcp playload only with tshark?

I am writing a wireshark dissector.I want to show the Dissection Tree in console.
So I tried:
tshark -V
It will show something like:
Frame 105: 69 bytes on wire (552 bits)...
...
Ethernet II, Src: Giga-Byt_97:b3:26 (e0:d5:5e:97:b3:26), Dst: Cradlepo_68:04:37 (00:e0:1c:68:04:37)
...
Internet Protocol Version 4, Src: 192.168.1.153, Dst: 192.168.1.99
...
Transmission Control Protocol, Src Port: 7555, Dst Port: 50555, Seq: 10182, Ack: 485, Len: 15
....
erlang term
PackageLength: 11
compressFlag: 0
m_system_hb_toc(2) [SmallTuple: 2]
time: 1589549432 [Int]
But only the last part is what i need:
erlang term
PackageLength: 11
compressFlag: 0
m_system_hb_toc(2) [SmallTuple: 2]
time: 1589549432 [Int]
I have tried with '-T fields' and -e option,but can not find any thing help.
Here's my dissecter's code:
local tcpPortLs = {7555}
local SIZE_LEN = 4
local pErlangExt = Proto("ErlangExt", "erlang term")
local fLen = ProtoField.uint32("ErlangExt.len", "PackageLength", base.DEC)
local fCompressFlag = ProtoField.string("ErlangExt.compressFlag", "compressFlag", base.ASCII)
local fBytes = ProtoField.bytes("ErlangExt.data", "PackageData", base.COLON)
pErlangExt.fields = {
fLen,
fBytes,
fCompressFlag,
}
local function msg_pdu_length(buf, pkt, offset)
local size_tvbr = buf:range(offset, SIZE_LEN)
local size = size_tvbr:uint()
return size + SIZE_LEN
end
local function _headBytes(n, dataBuf)
local head = dataBuf(0, n)
if dataBuf:len() == n then
return head, nil
end
local tailDataBuf = dataBuf(n, dataBuf:len() - n)
return head, tailDataBuf
end
local function _addToGroup()
-- ...
end
local function _calcMainTree()
-- ...
end
local function msg_proto_dissector(buf, pkt, root)
local dataLenBuf, metaAndDataBytes = _headBytes(SIZE_LEN, buf)
local detail = root:add(pErlangExt, buf)
local dataLen = dataLenBuf:uint()
detail:add(fLen, dataLenBuf, dataLen)
local zlibFlagBuf, tupleDataBuf = _headBytes(1, metaAndDataBytes)
local zlibFlag = zlibFlagBuf:uint()
detail:add(fCompressFlag, zlibFlagBuf, zlibFlag)
local dataRoot = detail:add(fBytes, tupleDataBuf)
pkt.cols.protocol = "ErlangExt"
local tree = _calcMainTree(tupleDataBuf, zlibFlag)
_addToGroup(dataRoot, tree)
end
function pErlangExt.dissector(buf, pkt, root)
local pktLen = buf:len()
if pktLen ~= buf:reported_len() then
return 0
end
dissect_tcp_pdus(buf, root, 4, msg_pdu_length, msg_proto_dissector)
return pktLen
end
local tcp_encap_table = DissectorTable.get("tcp.port")
for _, port in pairs(tcpPortLs) do
tcp_encap_table:add(port, pErlangExt)
end
And the captured data is https://github.com/cmingjian/testData/blob/master/stage.pcapng
How can I display only the data that I need?
Thanks.
Maybe tshark -O ErlangExt will provide you with the results you're seeking? You'll still get summary lines of all lower layers (Ethernet, IP, TCP), but only your ErlangExt data will be expanded.
From the tshark man page:
-O < protocols >
Similar to the -V option, but causes TShark to only show a detailed view of the comma-separated list of protocols specified, and show only the top-level detail line for all other protocols, rather than a detailed view of all protocols. Use the output of "tshark -G protocols" to find the abbreviations of the protocols you can specify.

Anatomizing lua code with lua itself

I have a config file written in lua with kind of a given structure, that later should dynamically create a GUI:
-- Input config file
package.path = '?.fsl;'
require "init"
global_unit(mm) -- Global unit (m, cm, mm)
pickdist(0.001) -- Snap distance
cosys(polar) -- Set system of coordinates
-- Define model name, for filenames etc.
new_model_force(model_name,"from main.fsl")
-- Define FE Control Data
m.hc_min = 95.000 -- Limit demagnetisa > 0:[%]Hc;<0:[kA/m]
m.con_hdcopy = 1.000 -- Hc-copy:Name:auto:0;intact:1; none:-1
m.b_max = 2.200 -- Max Induction [T] in colorgradation
m.b_min = 0.000 -- Move inside: 0 ; Move outside: > 0
pre_models("FE-contr-data");
pre_models("connect_models");
-- Define the Basic Model Parameter
m.tot_num_slot = QS -- Number of slots (>= 1)
m.num_poles = 2 * p -- Number of poles 2p (>= 2)
m.npols_gen = 2*p*mf -- Number of poles simulated (>= 1) --=m.num_poles * m.num_sl_gen / m.tot_num_slot
m.num_slots = QS*mf -- Number of slots in model
m.arm_length = lFe -- Effect. armature length [mm]
m.fc_radius = (Di-delta)/2 -- Radius air-gap center [mm] --=(m.inside_diam-ag)/2
pre_models("basic_modpar");
...
All m.whatever variables belong to the respective pre_models(...) statement below.
The number and names of the variables for each "pre_models(...) - block" are not known sometimes, same applies for the "pre_models(...) - blocks" itself.
GUI should show sth. like:
Tab1 = General settings: global_unit -> mm, pickdist -> 0.001, etc.
Tab2 = FE-contr-data: m.hc_min -> 95.000, m.con_hdcopy -> 1.000, etc.
Tab3 = connect_models:
Tab4 = basic_modpar: m.tot_num_slot -> 36, m.num_poles -> 30, etc.
..where QS = 36 and p = 15 is defined in the file init.
Wish: Code don't have to know the terms "FE-contr-data", global_unit(...) etc.
Any idea how to approach?
I am only answering b/c this answer has no answer so it can be classified as answered.
You need to intercept (using metatable(_G)) access to pre_models() and run the script. Finally, print a serialized m on every invocation of the intercepted version of pre_models().

SATA driver; AHCI port initialisation; Start (ST) command list;

I have an issue with initialising and running AHCI port on my devboard.
Processor has SATA host controller with 2 ports implemented (port 0 & port 1). SSD drive w/ SATA interface is connected to the port 1.
According to "Serial ATA AHCI 1.3.1 Specification", chapter "10.3.1 Start (PxCMD.ST)" a bunch of conditions should be satisfied before setting PxCMD.ST to '1'. After setting this bit, PxCMD.CR should be set to indicate that port is ready and runs.
The problem is PxCMD.CR never comes to be set.
State before enabling PxCMD.ST:
port is in idle state, PxCMD.ST and PxCMD.CR are both '0'
FIS Receive Enable, PxCMD.FRE is '1'
Drive is connected to the port, PxSSTS = 0x00000133 (SSTS.IPM =
1, SSTS.SPD = 3, SSTS.DET = 3), PxTFD = 0x00000150 (TFD.STS.BSY
= 0, TFD.STS.DRQ = 0, TFD.STS.ERR = 0)
PxCLB/PxCLBU are set with physical addresses to buffer and buffer is aligned. (1KB buffer, 1KB aligned)
PxCMD = 0x00404010 (PxCMD.FBSCP = 1 & PxCMD.FRE = 1)
After setting PxCMD.ST, PxCMD = 0x00404011 and PxCMD.CR stays '0'
PxSERR = 0 stay the same. ERR is cleaned in advance before setting PxCMD.FRE
PxSCTL = 0x00000330 (IPM = 3, SPD = 3, DET = 0)
PxTFD = 0x00000150 stay the same
PxSIG = 0x00000101. The reg has 0xFFFFFFFF when drive is not connected.
Assuming that PxSIG is set and PxSERR is '0' more likely that PLL for SATA HC is set correctly.
I have concerns about PxTFD.ERR = 1 (when drive is disconnected PxTFD has default 0x7F value and PxTFD.ERR = 0) but didn't find is it relevant or nor considering that still TFD.STS.ERR = 0
Any ideal what is missed?
PS: It has nothing to do w/ Linux.

Resources