-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.lua
66 lines (54 loc) · 1.61 KB
/
app.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-- Filename: app.lua
-- Description: mqtt data handlers
-- Version: 1.0.0(2018/9/5 16:45:04)
-- Author: Pan Lin <[email protected]>
local sensorData = {}
sensorData["serNum"] = config.ID
sensorData["batVol"] = 0
sensorData["sgnStr"] = 0
sensorData["devLoc"] = config.ADDR
sensorData["msgTime"] = ""
sensorData["tprInt"] = 0
sensorData["tprDec"] = 0
local function genRandom()
math.randomseed(tmr.now())
sensorData["batVol"] = (math.random(50)/100 + 4.75)
sensorData["sgnStr"] = (math.random(10) + 60)
math.randomseed(tmr.now())
sensorData["tprInt"] = (math.random(6) + 22)
sensorData["tprDec"] = math.random(9)
end
local function genSendData()
genRandom()
sensorData["msgTime"] = getCurTime()
return string.format(
"%s,%4.2f,%d,%s,%s,%d,%d",
sensorData["serNum"],
sensorData["batVol"],
sensorData["sgnStr"],
sensorData["devLoc"],
sensorData["msgTime"] ,
sensorData["tprInt"],
sensorData["tprDec"]
)
end
local function _mqttPub(sendData)
m = mqtt.Client(config.ID, 60)
m:connect(config.HOST, config.PORT, 0, function(client)
print("connected")
client:publish(config.TOPIC, sendData, 0, 0, function(client) print("pressure value published") end)
end,
function(client, reason)
print("failed reason: " .. reason)
end)
m:close()
end
function mqttPub()
gpio.write(0, gpio.LOW)
sendData = genSendData()
print ("------data-------")
print (sendData)
print ("-----------------")
_mqttPub(sendData)
gpio.write(0, gpio.HIGH)
end