mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-17 08:25:53 +08:00
82
sdk/air724/wumeimqtt/WeiMeiApp.lua
Normal file
82
sdk/air724/wumeimqtt/WeiMeiApp.lua
Normal file
@@ -0,0 +1,82 @@
|
||||
--- 模块功能:物美MQTT应用
|
||||
-- @author 杜兴杰
|
||||
-- @module 物美MQTT应用
|
||||
-- @license MIT
|
||||
-- @copyright 杜兴杰
|
||||
-- @release 2022.8.5
|
||||
|
||||
module(..., package.seeall)
|
||||
|
||||
local VERSION = "0.1"
|
||||
local m_strUserId = "1"
|
||||
local m_strLongitude = "0"
|
||||
local m_strLatitude = "0"
|
||||
|
||||
local m_nTemperature = 25
|
||||
--local m_nVoltval = 4.0
|
||||
|
||||
--模块温度返回回调函数
|
||||
--@temp温度,srting类型,如果要对该值进行运算,可以使用带float的固件将该值转为number
|
||||
local function getTemperatureCb(temp)
|
||||
m_nTemperature = temp
|
||||
end
|
||||
|
||||
--- ADC读取测试
|
||||
-- @return 无
|
||||
-- @usage read()
|
||||
local function read0()
|
||||
--ADC0接口用来读取电压
|
||||
local ADC_ID = 0
|
||||
local adcval,voltval = adc.read(ADC_ID)
|
||||
log.info("testAdc0.read",adcval,(voltval-(voltval%3))/3,voltval)
|
||||
-- 输出计算得出的原始电压值
|
||||
m_nVoltval = voltval/3 * 3127 / 1000
|
||||
log.info("testAdc0.Vbat", m_nVoltval)
|
||||
end
|
||||
|
||||
--2秒循环查询模块温度
|
||||
sys.timerLoopStart(misc.getTemperature,1000,getTemperatureCb)
|
||||
sys.timerLoopStart(read0,1000)
|
||||
|
||||
-- 开启对应的adc通道
|
||||
adc.open(0)
|
||||
|
||||
|
||||
|
||||
function FunctionData()
|
||||
local jsonStr = ""
|
||||
end
|
||||
|
||||
function PropertyData()
|
||||
local jsonData = {{
|
||||
id= "temperature",
|
||||
value= m_nTemperature ,
|
||||
remark="温度信息"
|
||||
},{
|
||||
id= "voltage",
|
||||
value= m_nVoltval,
|
||||
remark="电压信息"
|
||||
}}
|
||||
local jsonStr = json.encode(jsonData)
|
||||
return jsonStr
|
||||
end
|
||||
|
||||
function InformationData()
|
||||
local jsonData = {
|
||||
rssi = net.getRssi(),
|
||||
firmwareVersion = VERSION,
|
||||
status = 3 ,
|
||||
userId = m_strUserId ,
|
||||
longitude = m_strLongitude ,
|
||||
latitude = m_strLatitude,
|
||||
summary = {
|
||||
name= "device",
|
||||
chip = "air724",
|
||||
author = "duxingjie",
|
||||
version=0.1,
|
||||
create = "2022-08-07"
|
||||
}
|
||||
}
|
||||
local jsonStr = json.encode(jsonData)
|
||||
return jsonStr
|
||||
end
|
||||
175
sdk/air724/wumeimqtt/WeiMeiComAuth.lua
Normal file
175
sdk/air724/wumeimqtt/WeiMeiComAuth.lua
Normal file
@@ -0,0 +1,175 @@
|
||||
--- 模块功能:物美MQTT认证
|
||||
-- @author 杜兴杰
|
||||
-- @module 物美MQTT通信
|
||||
-- @license MIT
|
||||
-- @copyright 杜兴杰
|
||||
-- @email 1066950103@qq.com
|
||||
-- @release 2022.8.4
|
||||
|
||||
module(..., package.seeall)
|
||||
require"http"
|
||||
require"utils"
|
||||
require "common"
|
||||
require"misc"
|
||||
|
||||
local m_strEncryptionMode
|
||||
local m_strProductId
|
||||
local m_strDeviceId
|
||||
local m_strUserId
|
||||
local m_strUser
|
||||
local m_strPassword
|
||||
local m_nTimeout
|
||||
local m_strIp
|
||||
local m_strDeviceAuthorizationCode = nil
|
||||
local m_strProductPassword = nil
|
||||
local m_InitCallback = nil
|
||||
|
||||
local m_strOutPassword
|
||||
local m_strClientId
|
||||
local m_nTimeSyncFlag
|
||||
|
||||
local function CreateCommunicationPassword()
|
||||
if m_strEncryptionMode == "S" then
|
||||
if nil == m_strDeviceAuthorizationCode then
|
||||
m_strOutPassword = m_strPassword
|
||||
else
|
||||
m_strOutPassword = m_strPassword .. "&" .. m_strDeviceAuthorizationCode
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
if m_strEncryptionMode == "E" then
|
||||
m_strOutPassword = AesPassword()
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function Callback(nResult)
|
||||
log.info("Callback" .. nResult)
|
||||
if m_InitCallback ~= nil then
|
||||
m_InitCallback(nResult)
|
||||
end
|
||||
end
|
||||
|
||||
local function ParameterCheck()
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
local function FormatUnixTime2Date(unixTime)
|
||||
if unixTime and unixTime >= 0 then
|
||||
unixTime = unixTime /1000
|
||||
local ntim = os.date("*t", unixTime)
|
||||
return ntim
|
||||
end
|
||||
end
|
||||
|
||||
local function CreatedClientId()
|
||||
m_strClientId = m_strEncryptionMode .. "&" .. m_strDeviceId .. "&" .. m_strProductId .. "&" .. m_strUserId
|
||||
end
|
||||
|
||||
local function httpCallbackFun(result,prompt,head,body)
|
||||
log.info("GetTimeHttp.cbFnc",result,prompt)
|
||||
if result and body then
|
||||
log.info("GetTimeHttp.cbFnc"..body.."bodyLen="..body:len())
|
||||
local nDeviceRunTickMs = rtos.tick() * 5
|
||||
local jsonObj = json.decode(body)
|
||||
local nDeviceSendTime = jsonObj["deviceSendTime"]
|
||||
local nServerSendTime = jsonObj["serverSendTime"]
|
||||
local nServerRecvTime = jsonObj["serverRecvTime"]
|
||||
local nSyncTime = (nServerRecvTime + nServerSendTime + nDeviceRunTickMs - nDeviceSendTime)/2
|
||||
log.info("nSyncTime=" .. nSyncTime)
|
||||
local tTime = FormatUnixTime2Date(nSyncTime)
|
||||
log.info("tTime" .. tTime.year)
|
||||
misc.setClock(tTime,nil)
|
||||
--rtos.set_time(tTime.year,tTime.month,tTime.day,tTime.hour,tTime.min,tTime.sec)
|
||||
--创建客户端ID
|
||||
CreatedClientId()
|
||||
--创建密码
|
||||
CreateCommunicationPassword()
|
||||
Callback(1)
|
||||
m_nTimeSyncFlag = 1
|
||||
end
|
||||
end
|
||||
|
||||
function SyncTime()
|
||||
local nDeviceRunTickMs = rtos.tick() * 5
|
||||
local strUrl = "http://" .. m_strIp .. ":8080/iot/tool/ntp?deviceSendTime=" .. nDeviceRunTickMs
|
||||
http.request("GET",strUrl,nil,nil,nil,nil,httpCallbackFun)
|
||||
end
|
||||
|
||||
local function GetCurrentTime()
|
||||
local tm = misc.getClock()
|
||||
local nCurrentTime = os.time(tm)
|
||||
return nCurrentTime
|
||||
end
|
||||
|
||||
local function AesPassword()
|
||||
local nCurrentTime = GetCurrentTime()
|
||||
local nExpireTime = m_nTimeout + nCurrentTime
|
||||
local strAesSource = nil
|
||||
if nil == m_strDeviceAuthorizationCode then
|
||||
strAesSource = m_strPassword .. "&" .. nExpireTime
|
||||
else
|
||||
strAesSource = m_strPassword .. "&" .. nExpireTime .. "&" .. m_strDeviceAuthorizationCode
|
||||
end
|
||||
--加密内容: mqtt密码 & expireTime & 授权码(可选)
|
||||
--加密模式:CBC;填充方式:Pkcs5Padding;密钥:1234567890123456;密钥长度:128 bit;偏移量:1234567890666666
|
||||
encodeStr = crypto.aes_encrypt("CBC","PKCS5",strAesSource,m_strProductPassword,"wumei-smart-open")
|
||||
log.info("AesPassword strAesSource" .. strAesSource .. " encodeStr " .. encodeStr )
|
||||
encodeStr = crypto.base64_encode(encodeStr,slen(encodeStr))
|
||||
log.info("AesPassword strAesSource" .. strAesSource .. " encodeStrbase64 " .. encodeStr )
|
||||
return encodeStr
|
||||
end
|
||||
|
||||
--- mqtt认证初始化
|
||||
-- @string strEncryptionMode,string类型,认证类型:S=简单认证,E=加密认证
|
||||
-- @string strProductId,string类型,物美里面定义的产品ID
|
||||
-- @string strDeviceId,string类型,设备ID 一般用imei
|
||||
-- @string strUserId,string类型,用户ID
|
||||
-- @string strUser,string类型,用户
|
||||
-- @string strPassword,string类型,密码
|
||||
-- @number nTimeout,number类型,延迟秒为单位
|
||||
-- @string strIp,string类型,Ip
|
||||
-- @string strDeviceAuthorizationCode,string类型 设备认证码 不用不填
|
||||
-- @string strProductPassword,string类型 产品密码
|
||||
-- @return nil
|
||||
function Init(strEncryptionMode,strProductId,strDeviceId,strUserId,strUser,strPassword,nTimeout,strIp,strDeviceAuthorizationCode,strProductPassword,callback)
|
||||
m_strEncryptionMode = strEncryptionMode
|
||||
m_strProductId = strProductId
|
||||
m_strDeviceId = strDeviceId
|
||||
m_strUserId = strUserId
|
||||
m_strUser = strUser
|
||||
m_strPassword = strPassword
|
||||
m_nTimeout = nTimeout
|
||||
m_strIp = strIp
|
||||
if nil == strDeviceAuthorizationCode then
|
||||
m_strDeviceAuthorizationCode = strDeviceAuthorizationCode
|
||||
end
|
||||
if nil == strProductPassword then
|
||||
m_strProductPassword = strProductPassword
|
||||
end
|
||||
m_nTimeSyncFlag = 0
|
||||
m_InitCallback = callback
|
||||
--if ParameterCheck() == false then
|
||||
-- Callback(0)
|
||||
-- end
|
||||
SyncTime()
|
||||
end
|
||||
|
||||
function GetUser()
|
||||
return m_strUser
|
||||
end
|
||||
|
||||
function GetPassword()
|
||||
return m_strOutPassword
|
||||
end
|
||||
|
||||
function GetClientId()
|
||||
return m_strClientId
|
||||
end
|
||||
|
||||
function GetIP()
|
||||
return m_strIp
|
||||
end
|
||||
288
sdk/air724/wumeimqtt/WeiMeiComInteraction.lua
Normal file
288
sdk/air724/wumeimqtt/WeiMeiComInteraction.lua
Normal file
@@ -0,0 +1,288 @@
|
||||
--- 模块功能:物美MQTT交互
|
||||
-- @author 杜兴杰
|
||||
-- @module 物美MQTT通信
|
||||
-- @license MIT
|
||||
-- @copyright 杜兴杰
|
||||
-- @release 2022.8.5
|
||||
|
||||
module(..., package.seeall)
|
||||
|
||||
local m_strProductId
|
||||
local m_strDeviceNum
|
||||
|
||||
local m_tMessageQueue = {}
|
||||
|
||||
local m_callbackPropertyData = nil
|
||||
local m_callbackFunctionData = nil
|
||||
local m_callbackEventData = nil
|
||||
local m_callbackDeviceInformationData = nil
|
||||
|
||||
local m_timePropertyId = 0
|
||||
local m_timeFunctionId = 0
|
||||
local m_timeEventId = 0
|
||||
|
||||
local m_nMonitorCount = 0
|
||||
local m_nMonitorTime = 0
|
||||
local m_timeMonitorId = 0
|
||||
|
||||
local function GetSubscriberDeviceInformation()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/info/get"
|
||||
end
|
||||
|
||||
local function GetSubscriberDeviceInOta()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/ota/get"
|
||||
end
|
||||
|
||||
local function GetSubscriberDeviceProperty()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/property/get"
|
||||
end
|
||||
|
||||
local function GetSubscriberDevicePropertyOnline()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/property-online/get"
|
||||
end
|
||||
|
||||
local function GetSubscriberDeviceFunction()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/function/get"
|
||||
end
|
||||
|
||||
local function GetSubscriberDeviceFunctionOnline()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/function-online/get"
|
||||
end
|
||||
|
||||
local function GetSubscriberDeviceMonitor()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/monitor/get"
|
||||
end
|
||||
|
||||
local function GetSubscriberDeviceNtp()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/ntp/get"
|
||||
end
|
||||
|
||||
local function GetPublishDeviceInformation()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/info/post"
|
||||
end
|
||||
|
||||
local function GetPublishDeviceProperty()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/property/post"
|
||||
end
|
||||
|
||||
local function GetPublishDeviceFunction()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/function/post"
|
||||
end
|
||||
|
||||
local function GetPublishDeviceEvent()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/event/post"
|
||||
end
|
||||
|
||||
local function GetPublishDeviceNtp()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/ntp/post"
|
||||
end
|
||||
|
||||
local function GetPublishMonitorProperty()
|
||||
return "/" .. m_strProductId .. "/" .. m_strDeviceNum .. "/monitor/post"
|
||||
end
|
||||
|
||||
local function PropertyPush()
|
||||
if m_callbackPropertyData ~= nil then
|
||||
local strTopic = GetPublishDeviceProperty()
|
||||
local strMessage = m_callbackPropertyData()
|
||||
local nQos = 1
|
||||
table.insert(m_tMessageQueue,{topic=strTopic,payload=strMessage,qosr=nQos})
|
||||
log.info("------------------PropertyPush---------------")
|
||||
end
|
||||
end
|
||||
|
||||
local function FunctionPush()
|
||||
if m_callbackFunctionData ~= nil then
|
||||
local strTopic = GetPublishDeviceFunction()
|
||||
local strMessage = m_callbackFunctionData()
|
||||
local nQos = 1
|
||||
table.insert(m_tMessageQueue,{topic=strTopic,payload=strMessage,qosr=nQos})
|
||||
log.info("------------------FunctionPush---------------")
|
||||
end
|
||||
end
|
||||
|
||||
local function EventPush()
|
||||
if m_callbackEventData ~= nil then
|
||||
local strTopic = GetPublishDeviceEvent()
|
||||
local strMessage = m_callbackEventData()
|
||||
local nQos = 1
|
||||
table.insert(m_tMessageQueue,{topic=strTopic,payload=strMessage,qosr=nQos})
|
||||
log.info("------------------EventPush---------------")
|
||||
end
|
||||
end
|
||||
|
||||
local function MonitorPush()
|
||||
if m_callbackPropertyData ~= nil then
|
||||
local strTopic = GetPublishMonitorProperty()
|
||||
local strMessage = m_callbackPropertyData()
|
||||
local nQos = 1
|
||||
table.insert(m_tMessageQueue,{topic=strTopic,payload=strMessage,qosr=nQos})
|
||||
log.info("------------------MonitorPush---------------")
|
||||
end
|
||||
end
|
||||
|
||||
local function DeviceMonitorTimeCallback()
|
||||
MonitorPush()
|
||||
if m_nMonitorCount ~= 0 then
|
||||
m_nMonitorCount = m_nMonitorCount -1
|
||||
m_timeMonitorId = sys.timerStart(
|
||||
DeviceMonitorTimeCallback
|
||||
,m_nMonitorTime)
|
||||
else
|
||||
sys.timerStop(m_timeMonitorId)
|
||||
m_timeMonitorId= 0
|
||||
end
|
||||
log.info("----m_nMonitorCount=" .. m_nMonitorCount)
|
||||
end
|
||||
|
||||
local function DeviceInformationPush()
|
||||
if m_callbackDeviceInformationData ~= nil then
|
||||
local strTopic = GetPublishDeviceInformation()
|
||||
local strMessage = m_callbackDeviceInformationData()
|
||||
local nQos = 1
|
||||
table.insert(m_tMessageQueue,{topic=strTopic,payload=strMessage,qosr=nQos})
|
||||
end
|
||||
end
|
||||
|
||||
--- mqtt交互初始化
|
||||
-- @string strProductId,string类型,产品ID
|
||||
-- @string strDeviceNum,string类型,设备号 IMEI
|
||||
-- @return nil
|
||||
function Init(strProductId,strDeviceNum)
|
||||
m_strProductId = strProductId
|
||||
m_strDeviceNum = strDeviceNum
|
||||
end
|
||||
|
||||
--- mqtt交互延时初始化
|
||||
-- @return nil
|
||||
function DelayInit()
|
||||
DeviceInformationPush();
|
||||
end
|
||||
--- mqtt 获取待发送数据 一次取出一天且删除
|
||||
-- @return bResult false 没有数据 true 有数据
|
||||
-- @return strMessage 要发送的数据
|
||||
-- @return strTopic 要发送的主题
|
||||
-- @return nQos 发送消息级别
|
||||
function GetData()
|
||||
local bResult = false
|
||||
local strMessage =""
|
||||
local strTopic =""
|
||||
local nQos = 0
|
||||
if #m_tMessageQueue>0 then
|
||||
local outMsg = table.remove(m_tMessageQueue,1)
|
||||
bResult = true
|
||||
strMessage = outMsg.payload
|
||||
strTopic = outMsg.topic
|
||||
nQos = outMsg.qosr
|
||||
end
|
||||
return bResult,strMessage,strTopic,nQos
|
||||
end
|
||||
|
||||
--- mqtt 设置属性的获取获取函数,内部调用发送的时候会执行这个函数 这个函数外部实现
|
||||
-- @function callback,函数类型, 数据函
|
||||
function SetCallbackPropertyData(callback)
|
||||
m_callbackPropertyData = callback
|
||||
end
|
||||
|
||||
--- mqtt 设置功能的获取获取函数,内部调用发送的时候会执行这个函数 这个函数外部实现
|
||||
-- @function callback,函数类型, 数据函
|
||||
function SetCallbackFunctionData(callback)
|
||||
m_callbackFunctionData = callback
|
||||
end
|
||||
|
||||
--- mqtt 设置事件的获取获取函数,内部调用发送的时候会执行这个函数 这个函数外部实现
|
||||
-- @function callback,函数类型, 数据函
|
||||
function SetCallbackEventData(callback)
|
||||
m_callbackEventData = callback
|
||||
end
|
||||
|
||||
--- mqtt 设置设备信息获取函数,这个函数外部实现
|
||||
-- @function callback,函数类型, 数据函
|
||||
function SetCallbackInformationData(callback)
|
||||
m_callbackDeviceInformationData = callback
|
||||
end
|
||||
|
||||
--- mqtt 设置属性发布
|
||||
-- @number nTime,数字类型, 0停止定时器 其他值开启定时器
|
||||
function SetPropertyPush(nTime)
|
||||
if m_timePropertyId ~= 0 then
|
||||
sys.timerStop(m_timePropertyId)
|
||||
m_timePropertyId = 0
|
||||
end
|
||||
if nTime~= 0 then
|
||||
m_timePropertyId = sys.timerLoopStart(PropertyPush,nTime)
|
||||
end
|
||||
end
|
||||
|
||||
--- mqtt 设置功能发布
|
||||
-- @number nTime,数字类型, 0停止定时器 其他值开启定时器
|
||||
function SetFunctionPush(nTime)
|
||||
if m_timeFunctionId ~= 0 then
|
||||
sys.timerStop(m_timeFunctionId)
|
||||
m_timeFunctionId = 0
|
||||
end
|
||||
if nTime~= 0 then
|
||||
m_timeFunctionId = sys.timerLoopStart(FunctionPush,nTime)
|
||||
end
|
||||
end
|
||||
|
||||
--- mqtt 设置事件发布
|
||||
-- @number nTime,数字类型, 0停止定时器 其他值开启定时器
|
||||
function SetEventPush(nTime)
|
||||
if m_timeEventId ~= 0 then
|
||||
sys.timerStop(m_timeEventId)
|
||||
m_timeEventId = 0
|
||||
end
|
||||
if nTime~= 0 then
|
||||
m_timeEventId = sys.timerLoopStart(EventPush,nTime)
|
||||
end
|
||||
end
|
||||
|
||||
--- mqtt 获取要订阅的所有主题
|
||||
-- @return tSubscriber,表类型, topic 主题 qos级别
|
||||
function GetSubscriberAll()
|
||||
local tSubscriber ={}
|
||||
table.insert(tSubscriber,{topic=GetSubscriberDeviceInformation(),qos=1})
|
||||
table.insert(tSubscriber,{topic=GetSubscriberDeviceProperty(),qos=1})
|
||||
table.insert(tSubscriber,{topic=GetSubscriberDevicePropertyOnline(),qos=1})
|
||||
table.insert(tSubscriber,{topic=GetSubscriberDeviceFunction(),qos=1})
|
||||
table.insert(tSubscriber,{topic=GetSubscriberDeviceFunctionOnline(),qos=1})
|
||||
table.insert(tSubscriber,{topic=GetSubscriberDeviceMonitor(),qos=1})
|
||||
table.insert(tSubscriber,{topic=GetSubscriberDeviceNtp(),qos=1})
|
||||
return tSubscriber
|
||||
end
|
||||
|
||||
--- mqtt接受数据
|
||||
-- @string topic,string类型,接收到的主题
|
||||
-- @string message,string类型,消息内容
|
||||
-- @return nil
|
||||
function OnRecvData(topic , message)
|
||||
if topic == GetSubscriberDeviceInformation() then
|
||||
DeviceInformationPush();
|
||||
elseif topic == GetSubscriberDeviceInOta() then
|
||||
local jsonObj = json.decode(message)
|
||||
local strVersion = jsonObj["version"]
|
||||
local strUrl = jsonObj["downloadUrl"]
|
||||
elseif topic == GetSubscriberDeviceProperty() then
|
||||
PropertyPush();
|
||||
elseif topic == GetSubscriberDevicePropertyOnline() then
|
||||
PropertyPush();
|
||||
elseif topic == GetSubscriberDeviceFunction() then
|
||||
FunctionPush();
|
||||
elseif topic == GetSubscriberDeviceFunctionOnline() then
|
||||
FunctionPush();
|
||||
elseif topic == GetSubscriberDeviceMonitor() then
|
||||
local jsonObj = json.decode(message)
|
||||
m_nMonitorCount = jsonObj["count"]
|
||||
m_nMonitorTime = jsonObj["interval"]
|
||||
if m_timeMonitorId ~= 0 then
|
||||
sys.timerStop(m_timeMonitorId)
|
||||
m_timeMonitorId = 0
|
||||
end
|
||||
m_timeMonitorId = sys.timerStart(
|
||||
DeviceMonitorTimeCallback
|
||||
,m_nMonitorTime)
|
||||
elseif topic == GetSubscriberDeviceNtp() then
|
||||
log.info("--DeviceNtp---" .. message)
|
||||
end
|
||||
end
|
||||
133
sdk/air724/wumeimqtt/WuMeiTest.lua
Normal file
133
sdk/air724/wumeimqtt/WuMeiTest.lua
Normal file
@@ -0,0 +1,133 @@
|
||||
--- 模块功能:物美MQTT测试
|
||||
-- @author 杜兴杰
|
||||
-- @module 物美MQTT通信测试
|
||||
-- @license MIT
|
||||
-- @copyright 杜兴杰
|
||||
-- @email 1066950103@qq.com
|
||||
-- @release 2022.8.5
|
||||
module(..., package.seeall)
|
||||
require"WeiMeiComAuth"
|
||||
require"WeiMeiComInteraction"
|
||||
require"WeiMeiApp"
|
||||
require"misc"
|
||||
require"mqtt"
|
||||
|
||||
local ready = false
|
||||
|
||||
--物美配置参数相关配置
|
||||
local m_strEncryptionMode = "S"
|
||||
local m_strProductId = 220
|
||||
local m_strDeviceId = nil
|
||||
local m_strUserId = "1" -- admin
|
||||
local m_strMqttUser = "wumei-smart"
|
||||
local m_strMqttPassword = "PVMXS6V46205CAQ5"
|
||||
local m_strProductPassword = "KC3169JOU816X5C0" --产品密码
|
||||
local m_nMqttAuthenticationTimeout = 24*60*60*1000 --24小时
|
||||
local m_strMqttIp = "wumei.live"
|
||||
local m_strDeviceAuthorizationCode = "A25040D2E34B483DA371B5F9A315BB43" --设备授权码
|
||||
|
||||
local m_mqttClient = nil
|
||||
local m_mqttFlag = 0
|
||||
|
||||
function AuthenticationResultCallback(nResult)
|
||||
if nResult == 1 then
|
||||
log.info("---AuthenticationResultCallback---ok")
|
||||
WeiMeiComInteraction.Init(m_strProductId,m_strDeviceId)
|
||||
m_mqttFlag = 1
|
||||
end
|
||||
end
|
||||
|
||||
local function MqttInit()
|
||||
WeiMeiComAuth.Init(m_strEncryptionMode,m_strProductId,m_strDeviceId,m_strUserId,m_strMqttUser,m_strMqttPassword,m_nMqttAuthenticationTimeout,m_strMqttIp,m_strDeviceAuthorizationCode,m_strProductPassword,AuthenticationResultCallback)
|
||||
end
|
||||
|
||||
-- 订阅所有主题
|
||||
local function GetSubscriberAll()
|
||||
local tSubscriber = WeiMeiComInteraction.GetSubscriberAll()
|
||||
while #tSubscriber > 0 do
|
||||
local ouSubscriber = table.remove(tSubscriber,1)
|
||||
if m_mqttClient:subscribe({[ouSubscriber.topic]=ouSubscriber.qos}) == nil then
|
||||
log.info("subscribe eeror ")
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
--- MQTT连接是否处于激活状态
|
||||
-- @return 激活状态返回true,非激活状态返回false
|
||||
-- @usage mqttTask.isReady()
|
||||
function isReady()
|
||||
return ready
|
||||
end
|
||||
|
||||
--启动MQTT客户端任务
|
||||
sys.taskInit(
|
||||
function()
|
||||
local retryConnectCnt = 0
|
||||
while true do
|
||||
if not socket.isReady() then
|
||||
retryConnectCnt = 0
|
||||
--等待网络环境准备就绪,超时时间是5分钟
|
||||
sys.waitUntil("IP_READY_IND",300000)
|
||||
end
|
||||
|
||||
if socket.isReady() then
|
||||
m_strDeviceId = misc.getImei()
|
||||
MqttInit()
|
||||
while m_mqttFlag == 0 do
|
||||
sys.wait(50)
|
||||
end
|
||||
--创建一个MQTT客户端
|
||||
log.info("ClientId=" .. WeiMeiComAuth.GetClientId() )
|
||||
log.info("User=" .. WeiMeiComAuth.GetUser() )
|
||||
log.info("Password=" .. WeiMeiComAuth.GetPassword() )
|
||||
log.info("Ip=" .. WeiMeiComAuth.GetIP() )
|
||||
m_mqttClient = mqtt.client(WeiMeiComAuth.GetClientId(),600,WeiMeiComAuth.GetUser(),WeiMeiComAuth.GetPassword())
|
||||
if m_mqttClient:connect(WeiMeiComAuth.GetIP(),1883,"tcp") then
|
||||
retryConnectCnt = 0
|
||||
ready = true
|
||||
--订阅主题
|
||||
if GetSubscriberAll() == true then
|
||||
WeiMeiComInteraction.SetCallbackInformationData(WeiMeiApp.InformationData)
|
||||
WeiMeiComInteraction.SetCallbackPropertyData(WeiMeiApp.PropertyData)
|
||||
WeiMeiComInteraction.SetPropertyPush(1000*30) --30秒钟定时上传一次属性
|
||||
WeiMeiComInteraction.DelayInit()
|
||||
--循环处理接收和发送的数据
|
||||
while true do
|
||||
local result,data = m_mqttClient:receive(300,"APP_SOCKET_SEND_DATA")
|
||||
if result or data=="timeout" or data=="APP_SOCKET_SEND_DATA" then
|
||||
if result then
|
||||
log.info("data.topic" .. data.topic .. "data.payload" .. data.payload)
|
||||
WeiMeiComInteraction.OnRecvData(data.topic,data.payload);
|
||||
end
|
||||
else
|
||||
break -- 出错了
|
||||
end
|
||||
result,strMessage,strTopic,nQos = WeiMeiComInteraction.GetData()
|
||||
if result == true then
|
||||
local mqttResult = m_mqttClient:publish(strTopic,strMessage,nQos)
|
||||
if not mqttResult then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
ready = false
|
||||
else
|
||||
retryConnectCnt = retryConnectCnt+1
|
||||
end
|
||||
--断开MQTT连接
|
||||
m_mqttClient:disconnect()
|
||||
if retryConnectCnt>=5 then link.shut() retryConnectCnt=0 end
|
||||
sys.wait(5000)
|
||||
else
|
||||
--进入飞行模式,20秒之后,退出飞行模式
|
||||
net.switchFly(true)
|
||||
sys.wait(20000)
|
||||
net.switchFly(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
65
sdk/air724/wumeimqtt/main.lua
Normal file
65
sdk/air724/wumeimqtt/main.lua
Normal file
@@ -0,0 +1,65 @@
|
||||
|
||||
--必须在这个位置定义PROJECT和VERSION变量
|
||||
--PROJECT:ascii string类型,可以随便定义,只要不使用,就行
|
||||
--VERSION:ascii string类型,如果使用Luat物联云平台固件升级的功能,必须按照"X.X.X"定义,X表示1位数字;否则可随便定义
|
||||
PROJECT = "DTU"
|
||||
VERSION = "1.0.0"
|
||||
|
||||
--加载日志功能模块,并且设置日志输出等级
|
||||
--如果关闭调用log模块接口输出的日志,等级设置为log.LOG_SILENT即可
|
||||
require "log"
|
||||
LOG_LEVEL = log.LOGLEVEL_TRACE
|
||||
--[[
|
||||
如果使用UART输出日志,打开这行注释的代码"--log.openTrace(true,1,115200)"即可,根据自己的需求修改此接口的参数
|
||||
如果要彻底关闭脚本中的输出日志(包括调用log模块接口和Lua标准print接口输出的日志),执行log.openTrace(false,第二个参数跟调用openTrace接口打开日志的第二个参数相同),例如:
|
||||
1、没有调用过sys.opntrace配置日志输出端口或者最后一次是调用log.openTrace(true,nil,921600)配置日志输出端口,此时要关闭输出日志,直接调用log.openTrace(false)即可
|
||||
2、最后一次是调用log.openTrace(true,1,115200)配置日志输出端口,此时要关闭输出日志,直接调用log.openTrace(false,1)即可
|
||||
--]]
|
||||
--log.openTrace(true,1,115200)
|
||||
|
||||
require "sys"
|
||||
|
||||
require "net"
|
||||
--每1分钟查询一次GSM信号强度
|
||||
--每1分钟查询一次基站信息
|
||||
net.startQueryAll(60000, 60000)
|
||||
|
||||
--此处关闭RNDIS网卡功能
|
||||
--否则,模块通过USB连接电脑后,会在电脑的网络适配器中枚举一个RNDIS网卡,电脑默认使用此网卡上网,导致模块使用的sim卡流量流失
|
||||
--如果项目中需要打开此功能,把ril.request("AT+RNDISCALL=0,1")修改为ril.request("AT+RNDISCALL=1,1")即可
|
||||
--注意:core固件:V0030以及之后的版本、V3028以及之后的版本,才以稳定地支持此功能
|
||||
ril.request("AT+RNDISCALL=1,1")
|
||||
|
||||
--加载控制台调试功能模块(此处代码配置的是uart2,波特率115200)
|
||||
--此功能模块不是必须的,根据项目需求决定是否加载
|
||||
--使用时注意:控制台使用的uart不要和其他功能使用的uart冲突
|
||||
--使用说明参考demo/console下的《console功能使用说明.docx》
|
||||
--require "console"
|
||||
--console.setup(2, 115200)
|
||||
|
||||
--加载网络指示灯和LTE指示灯功能模块
|
||||
--根据自己的项目需求和硬件配置决定:1、是否加载此功能模块;2、配置指示灯引脚
|
||||
--合宙官方出售的Air720U开发板上的网络指示灯引脚为pio.P0_1,LTE指示灯引脚为pio.P0_4
|
||||
require "netLed"
|
||||
pmd.ldoset(2,pmd.LDO_VLCD)
|
||||
netLed.setup(true,pio.P0_1,pio.P0_4)
|
||||
--网络指示灯功能模块中,默认配置了各种工作状态下指示灯的闪烁规律,参考netLed.lua中ledBlinkTime配置的默认值
|
||||
--如果默认值满足不了需求,此处调用netLed.updateBlinkTime去配置闪烁时长
|
||||
--LTE指示灯功能模块中,配置的是注册上4G网络,灯就常亮,其余任何状态灯都会熄灭
|
||||
|
||||
--加载错误日志管理功能模块【强烈建议打开此功能】
|
||||
--如下2行代码,只是简单的演示如何使用errDump功能,详情参考errDump的api
|
||||
require "errDump"
|
||||
errDump.request("udp://dev_msg1.openluat.com:12425", nil, true)
|
||||
|
||||
--加载远程升级功能模块【强烈建议打开此功能,如果使用了阿里云的OTA功能,可以不打开此功能】
|
||||
--如下3行代码,只是简单的演示如何使用update功能,详情参考update的api以及demo/update
|
||||
PRODUCT_KEY = "7wazHLKGOdfjrSoG5tXOr4uUg7D5wT9k"
|
||||
--require "update"
|
||||
--update.request()
|
||||
|
||||
--加载MQTT功能测试模块
|
||||
require "WuMeiTest"
|
||||
--启动系统框架
|
||||
sys.init(0, 0)
|
||||
sys.run()
|
||||
65
sdk/air724/wumeimqtt/mainAir724.lua
Normal file
65
sdk/air724/wumeimqtt/mainAir724.lua
Normal file
@@ -0,0 +1,65 @@
|
||||
|
||||
--必须在这个位置定义PROJECT和VERSION变量
|
||||
--PROJECT:ascii string类型,可以随便定义,只要不使用,就行
|
||||
--VERSION:ascii string类型,如果使用Luat物联云平台固件升级的功能,必须按照"X.X.X"定义,X表示1位数字;否则可随便定义
|
||||
PROJECT = "DTU"
|
||||
VERSION = "1.0.0"
|
||||
|
||||
--加载日志功能模块,并且设置日志输出等级
|
||||
--如果关闭调用log模块接口输出的日志,等级设置为log.LOG_SILENT即可
|
||||
require "log"
|
||||
LOG_LEVEL = log.LOGLEVEL_TRACE
|
||||
--[[
|
||||
如果使用UART输出日志,打开这行注释的代码"--log.openTrace(true,1,115200)"即可,根据自己的需求修改此接口的参数
|
||||
如果要彻底关闭脚本中的输出日志(包括调用log模块接口和Lua标准print接口输出的日志),执行log.openTrace(false,第二个参数跟调用openTrace接口打开日志的第二个参数相同),例如:
|
||||
1、没有调用过sys.opntrace配置日志输出端口或者最后一次是调用log.openTrace(true,nil,921600)配置日志输出端口,此时要关闭输出日志,直接调用log.openTrace(false)即可
|
||||
2、最后一次是调用log.openTrace(true,1,115200)配置日志输出端口,此时要关闭输出日志,直接调用log.openTrace(false,1)即可
|
||||
--]]
|
||||
--log.openTrace(true,1,115200)
|
||||
|
||||
require "sys"
|
||||
|
||||
require "net"
|
||||
--每1分钟查询一次GSM信号强度
|
||||
--每1分钟查询一次基站信息
|
||||
net.startQueryAll(60000, 60000)
|
||||
|
||||
--此处关闭RNDIS网卡功能
|
||||
--否则,模块通过USB连接电脑后,会在电脑的网络适配器中枚举一个RNDIS网卡,电脑默认使用此网卡上网,导致模块使用的sim卡流量流失
|
||||
--如果项目中需要打开此功能,把ril.request("AT+RNDISCALL=0,1")修改为ril.request("AT+RNDISCALL=1,1")即可
|
||||
--注意:core固件:V0030以及之后的版本、V3028以及之后的版本,才以稳定地支持此功能
|
||||
ril.request("AT+RNDISCALL=1,1")
|
||||
|
||||
--加载控制台调试功能模块(此处代码配置的是uart2,波特率115200)
|
||||
--此功能模块不是必须的,根据项目需求决定是否加载
|
||||
--使用时注意:控制台使用的uart不要和其他功能使用的uart冲突
|
||||
--使用说明参考demo/console下的《console功能使用说明.docx》
|
||||
--require "console"
|
||||
--console.setup(2, 115200)
|
||||
|
||||
--加载网络指示灯和LTE指示灯功能模块
|
||||
--根据自己的项目需求和硬件配置决定:1、是否加载此功能模块;2、配置指示灯引脚
|
||||
--合宙官方出售的Air720U开发板上的网络指示灯引脚为pio.P0_1,LTE指示灯引脚为pio.P0_4
|
||||
require "netLed"
|
||||
pmd.ldoset(2,pmd.LDO_VLCD)
|
||||
netLed.setup(true,pio.P0_1,pio.P0_4)
|
||||
--网络指示灯功能模块中,默认配置了各种工作状态下指示灯的闪烁规律,参考netLed.lua中ledBlinkTime配置的默认值
|
||||
--如果默认值满足不了需求,此处调用netLed.updateBlinkTime去配置闪烁时长
|
||||
--LTE指示灯功能模块中,配置的是注册上4G网络,灯就常亮,其余任何状态灯都会熄灭
|
||||
|
||||
--加载错误日志管理功能模块【强烈建议打开此功能】
|
||||
--如下2行代码,只是简单的演示如何使用errDump功能,详情参考errDump的api
|
||||
require "errDump"
|
||||
errDump.request("udp://dev_msg1.openluat.com:12425", nil, true)
|
||||
|
||||
--加载远程升级功能模块【强烈建议打开此功能,如果使用了阿里云的OTA功能,可以不打开此功能】
|
||||
--如下3行代码,只是简单的演示如何使用update功能,详情参考update的api以及demo/update
|
||||
PRODUCT_KEY = "7wazHLKGOdfjrSoG5tXOr4uUg7D5wT9k"
|
||||
--require "update"
|
||||
--update.request()
|
||||
|
||||
--加载MQTT功能测试模块
|
||||
require "WuMeiTest"
|
||||
--启动系统框架
|
||||
sys.init(0, 0)
|
||||
sys.run()
|
||||
10
sdk/air724/wumeimqtt/mainVscode.lua
Normal file
10
sdk/air724/wumeimqtt/mainVscode.lua
Normal file
@@ -0,0 +1,10 @@
|
||||
PROJECT = 'test'
|
||||
VERSION = '2.0.0'
|
||||
require 'log'
|
||||
LOG_LEVEL = log.LOGLEVEL_TRACE
|
||||
require 'sys'
|
||||
require "WuMeiTest"
|
||||
|
||||
|
||||
sys.init(0, 0)
|
||||
sys.run()
|
||||
Reference in New Issue
Block a user