mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-17 16:36:03 +08:00
更新
This commit is contained in:
@@ -461,7 +461,7 @@ String getTime()
|
||||
}
|
||||
delay(500);
|
||||
}
|
||||
return "";
|
||||
return "1672524366000";
|
||||
}
|
||||
|
||||
//打印提示信息
|
||||
|
||||
@@ -450,7 +450,7 @@ String getTime()
|
||||
}
|
||||
delay(500);
|
||||
}
|
||||
return "";
|
||||
return "1672524366000";
|
||||
}
|
||||
|
||||
//打印提示信息
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
#include "ApConfig.h"
|
||||
|
||||
String randomName = "wumei-device" + (String)random(1000);
|
||||
String randomName = "fastbee-device" + (String)random(1000);
|
||||
const char *ap_ssid = randomName.c_str();
|
||||
//开放式网络,不设置密码
|
||||
const char *ap_password = "";
|
||||
|
||||
@@ -38,29 +38,29 @@ String pEventTopic = "/event/post";
|
||||
|
||||
/********************************** begin 可配置的项 **********************************/
|
||||
// wifi信息
|
||||
char *wifiSsid = "MERCURY100";
|
||||
char *wifiPwd = "164770707";
|
||||
char *wifiSsid = "";
|
||||
char *wifiPwd = "";
|
||||
char *userId = "1";
|
||||
// 产品启用授权码,则授权码不能为空
|
||||
char *authCode = "";
|
||||
|
||||
// 设备信息配置
|
||||
char *deviceNum = "D1H2584G22Q2";
|
||||
char *productId = "41";
|
||||
char *deviceNum = "D1FJTWOT3HIB";
|
||||
char *productId = "588";
|
||||
float firmwareVersion = 1.0;
|
||||
// 经度和纬度可选,如果产品使用设备定位,则必须传
|
||||
float latitude = 0;
|
||||
float longitude = 0;
|
||||
|
||||
// Mqtt配置
|
||||
char *mqttHost = "43.143.82.218";
|
||||
char *mqttHost = "fastbee.cn";
|
||||
int mqttPort = 1883;
|
||||
char *mqttUserName = "wumei-smart";
|
||||
char *mqttPwd = "P47T6OD5IPFWHUM6";
|
||||
char mqttSecret[17] = "KX3TSH4Q4OS835DO";
|
||||
char *mqttUserName = "FastBee";
|
||||
char *mqttPwd = "P63653937TRQ8F27";
|
||||
char mqttSecret[17] = "KV52PPZ813EFCQD8";
|
||||
|
||||
// NTP地址(用于获取时间,修改为自己部署项目的接口地址)
|
||||
String ntpServer = "http://wumei.live:8080/iot/tool/ntp?deviceSendTime=";
|
||||
String ntpServer = "http://fastbee.cn:8080/iot/tool/ntp?deviceSendTime=";
|
||||
|
||||
/********************************** end 可配置的项 **********************************/
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@ void loop() {
|
||||
// 发布模拟数据,测试用
|
||||
publishSimulateDataClient();
|
||||
}
|
||||
ESP.wdtFeed(); // 喂软件看门狗,防止程序跑偏
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -97,7 +97,7 @@ String randomPropertyData() {
|
||||
int randInt = 0;
|
||||
StaticJsonDocument<1024> doc;
|
||||
JsonObject objTmeperature = doc.createNestedObject();
|
||||
objTmeperature["id"] = "temperature";
|
||||
objTmeperature["id"] = "light_current";
|
||||
randFloat = random(1000, 3000);
|
||||
objTmeperature["value"] = (String)(randFloat / 100);
|
||||
objTmeperature["remark"] = (String)millis();
|
||||
@@ -159,7 +159,7 @@ void processFunction(String msg) {
|
||||
// 匹配云端定义的功能
|
||||
const char *id = object["id"];
|
||||
const char *value = object["value"];
|
||||
if (strcmp(id, "switch") == 0) {
|
||||
if (strcmp(id, "light_switch") == 0) {
|
||||
printMsg("开关 switch:" + (String)value);
|
||||
if (strcmp(value, "1") == 0) {
|
||||
// 打开继电器
|
||||
|
||||
@@ -38,29 +38,29 @@ String pEventTopic = "/event/post";
|
||||
|
||||
/********************************** begin 可配置的项 **********************************/
|
||||
// wifi信息
|
||||
char *wifiSsid = "MERCURY100";
|
||||
char *wifiPwd = "FastBee";
|
||||
char *userId = "1";
|
||||
char *wifiSsid = (char*)"MERCURY100";
|
||||
char *wifiPwd = (char*)"FastBee";
|
||||
char *userId = (char*)"1";
|
||||
// 产品启用授权码,则授权码不能为空
|
||||
char *authCode = "";
|
||||
char *authCode = (char*)"";
|
||||
|
||||
// 设备信息配置
|
||||
char *deviceNum = "D1PGLPG58KZ2";
|
||||
char *productId = "96";
|
||||
char *deviceNum = (char*)"D1PGLPG58K88";
|
||||
char *productId = (char*)"96";
|
||||
float firmwareVersion = 1.0;
|
||||
// 经度和纬度可选,如果产品使用设备定位,则必须传
|
||||
float latitude = 0;
|
||||
float longitude = 0;
|
||||
|
||||
// Mqtt配置
|
||||
char *mqttHost = "43.143.82.218";
|
||||
char *mqttHost = (char*)"iot.fastbee.cn";
|
||||
int mqttPort = 1883;
|
||||
char *mqttUserName = "FastBee";
|
||||
char *mqttPwd = "P467433O1MT8MXS2";
|
||||
char *mqttUserName = (char*)"FastBee";
|
||||
char *mqttPwd = (char*)"P467433O1MT8MXS2";
|
||||
char mqttSecret[17] = "KWF32S3H95LH14LO";
|
||||
|
||||
// NTP地址(用于获取时间,修改为自己部署项目的接口地址)
|
||||
String ntpServer = "http://wumei.live:8080/iot/tool/ntp?deviceSendTime=";
|
||||
String ntpServer = "http://fastbee.cn:8080/iot/tool/ntp?deviceSendTime=";
|
||||
|
||||
/********************************** end 可配置的项 **********************************/
|
||||
|
||||
|
||||
@@ -67,9 +67,9 @@ void publishProperty(String msg) {
|
||||
|
||||
// 4.发布功能
|
||||
void publishFunction(String msg) {
|
||||
printMsg("发布功能:" + prefix + pFunctionTopic);
|
||||
printMsg("发布属性(功能):" + prefix + pPropertyTopic);
|
||||
printMsg("消息:" + msg);
|
||||
mqttClient.publish((prefix + pFunctionTopic).c_str(), msg.c_str());
|
||||
mqttClient.publish((prefix + pPropertyTopic).c_str(), msg.c_str());
|
||||
}
|
||||
|
||||
// 5.发布事件
|
||||
|
||||
@@ -72,7 +72,8 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
|
||||
printMsg("当前时间:" + String(now, 0));
|
||||
} else if (strcmp(topic, (prefix + sPropertyTopic).c_str()) == 0 || strcmp(topic, (prefix + sPropertyOnline).c_str()) == 0) {
|
||||
printMsg("订阅到属性指令...");
|
||||
processProperty(data);
|
||||
printMsg("新版本属性和功能合并为功能主题...");
|
||||
// processProperty(data);
|
||||
} else if (strcmp(topic, (prefix + sFunctionTopic).c_str()) == 0 || strcmp(topic, (prefix + sFunctionOnline).c_str()) == 0) {
|
||||
printMsg("订阅到功能指令...");
|
||||
processFunction(data);
|
||||
@@ -437,6 +438,8 @@ void processFunction(String msg) {
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
// 处理子设备的数据上报
|
||||
processSubDeviceReport(id, value);
|
||||
}
|
||||
// 最后发布功能,服务端订阅存储(重要)
|
||||
publishFunction(msg);
|
||||
|
||||
113
sdk/合宙/air780e/lua/WeiMeiApp.lua
Normal file
113
sdk/合宙/air780e/lua/WeiMeiApp.lua
Normal file
@@ -0,0 +1,113 @@
|
||||
--- 模块功能:物美MQTT应用
|
||||
-- @author 杜兴杰
|
||||
-- @module 物美MQTT应用
|
||||
-- @license MIT
|
||||
-- @copyright 杜兴杰
|
||||
-- @email 1066950103@qq.com
|
||||
-- @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
|
||||
|
||||
|
||||
local rtos_bsp = rtos.bsp()
|
||||
function adc_pin() -- 根据不同开发板,设置ADC编号
|
||||
if rtos_bsp == "AIR101" then -- Air101开发板ADC编号
|
||||
return 0,1,255,255,adc.CH_CPU ,adc.CH_VBAT
|
||||
elseif rtos_bsp == "AIR103" then -- Air103开发板ADC编号
|
||||
return 0,1,2,3,adc.CH_CPU ,adc.CH_VBAT
|
||||
elseif rtos_bsp == "AIR105" then -- Air105开发板ADC编号
|
||||
return 0,5,6,255,255,255
|
||||
elseif rtos_bsp == "ESP32C3" then -- ESP32C3开发板ADC编号
|
||||
return 0,1,2,3,adc.CH_CPU , 255
|
||||
elseif rtos_bsp == "ESP32C2" then -- ESP32C2开发板ADC编号
|
||||
return 0,1,2,3,adc.CH_CPU , 255
|
||||
elseif rtos_bsp == "ESP32S3" then -- ESP32S3开发板ADC编号
|
||||
return 0,1,2,3,adc.CH_CPU , 255
|
||||
elseif rtos_bsp == "EC618" then --Air780E开发板ADC编号
|
||||
return 0,1,255,255,adc.CH_CPU ,adc.CH_VBAT
|
||||
else
|
||||
log.info("main", "define ADC pin in main.lua")
|
||||
return 0, 0,0,0,0,0
|
||||
end
|
||||
end
|
||||
local adc_pin_0,adc_pin_1,adc_pin_2,adc_pin_3,adc_pin_temp,adc_pin_vbat=adc_pin()
|
||||
|
||||
--模块温度返回回调函数
|
||||
--@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.get(adc_pin_0)
|
||||
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(adc_pin_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 = mobile.rssi(),
|
||||
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
|
||||
|
||||
return {
|
||||
InformationData = InformationData,
|
||||
PropertyData = PropertyData,
|
||||
FunctionData = FunctionData
|
||||
}
|
||||
184
sdk/合宙/air780e/lua/WeiMeiComAuth.lua
Normal file
184
sdk/合宙/air780e/lua/WeiMeiComAuth.lua
Normal file
@@ -0,0 +1,184 @@
|
||||
--- 模块功能:物美MQTT认证
|
||||
-- @author 杜兴杰
|
||||
-- @module 物美MQTT通信
|
||||
-- @license MIT
|
||||
-- @copyright 杜兴杰
|
||||
-- @email 1066950103@qq.com
|
||||
-- @release 2022.8.4
|
||||
|
||||
module(..., package.seeall)
|
||||
|
||||
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 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 = 1000--rtos.tick() * 5
|
||||
|
||||
local nEnd = string.find(body,'}')
|
||||
local outStr= string.sub(body,1,nEnd-4) .. '}'
|
||||
log.info("----outStr " .. outStr)
|
||||
|
||||
local jsonObj = json.decode(outStr) --bug json 64 转换不支持
|
||||
local nDeviceSendTime = jsonObj["deviceSendTime"]
|
||||
local nServerSendTime = jsonObj["serverSendTime"]
|
||||
local nServerRecvTime = jsonObj["serverRecvTime"]
|
||||
log.info("nDeviceSendTime=" .. nDeviceSendTime)
|
||||
log.info("nServerSendTime=" .. nServerSendTime)
|
||||
log.info("nServerRecvTime=" .. nServerRecvTime)
|
||||
|
||||
local nSyncTime = nServerRecvTime --秒为单位 --(nServerRecvTime + nServerSendTime + nDeviceRunTickMs - nDeviceSendTime)/2 --这里运算有问题 可能存在数据移除了
|
||||
log.info("nSyncTime=" .. nSyncTime)
|
||||
rtc.set(nSyncTime)
|
||||
--创建客户端ID
|
||||
CreatedClientId()
|
||||
--创建密码
|
||||
CreateCommunicationPassword()
|
||||
Callback(1)
|
||||
m_nTimeSyncFlag = 1
|
||||
end
|
||||
end
|
||||
|
||||
function SyncTime()
|
||||
local nDeviceRunTickMs = 1000--rtos.tick() * 5
|
||||
local strUrl = "http://" .. m_strIp .. ":8080/iot/tool/ntp?deviceSendTime=" .. nDeviceRunTickMs
|
||||
log.info("strUrl" .. strUrl)
|
||||
--http.request("GET",strUrl,nil,nil,nil,nil,httpCallbackFun)
|
||||
local code, headers, body = http.request("GET", strUrl).wait()
|
||||
log.info("http.get", code, headers, body)
|
||||
httpCallbackFun(code,"NULL",headers,body)
|
||||
end
|
||||
|
||||
local function GetCurrentTime()
|
||||
local nCurrentTime = os.time()
|
||||
log.info("GetCurrentTime= " .. nCurrentTime)
|
||||
return nCurrentTime
|
||||
end
|
||||
|
||||
function AesPassword()
|
||||
local nCurrentTime = GetCurrentTime()
|
||||
local nExpireTime = m_nTimeout + nCurrentTime --这里存在bug 数据溢出了
|
||||
|
||||
local strAesSource = nil
|
||||
if nil == m_strDeviceAuthorizationCode then
|
||||
strAesSource = m_strPassword .. "&" .. nExpireTime .. "000"
|
||||
else
|
||||
strAesSource = m_strPassword .. "&" .. nExpireTime .. "000" .."&" .. m_strDeviceAuthorizationCode
|
||||
end
|
||||
--加密内容: mqtt密码 & expireTime & 授权码(可选)
|
||||
--加密模式:CBC;填充方式:Pkcs5Padding;密钥:1234567890123456;密钥长度:128 bit;偏移量:1234567890666666
|
||||
log.info("strAesSource=".. strAesSource .. " m_strProductPassword=" ..m_strProductPassword)
|
||||
local encodeStr = crypto.cipher_encrypt("AES-128-CBC", "PKCS7", strAesSource, m_strProductPassword, "wumei-smart-open") --todo 密码验证
|
||||
log.info("AesPassword strAesSource" .. strAesSource .. " encodeStr " .. encodeStr )
|
||||
log.info("AES", "aes-128-cbc", encodeStr:toHex())
|
||||
encodeStr = crypto.base64_encode(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
|
||||
m_strDeviceAuthorizationCode = strDeviceAuthorizationCode
|
||||
m_strProductPassword = strProductPassword
|
||||
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
|
||||
|
||||
|
||||
return {
|
||||
GetUser = GetUser,
|
||||
GetPassword = GetPassword,
|
||||
GetClientId = GetClientId,
|
||||
GetIP = GetIP,
|
||||
Init=Init
|
||||
}
|
||||
304
sdk/合宙/air780e/lua/WeiMeiComInteraction.lua
Normal file
304
sdk/合宙/air780e/lua/WeiMeiComInteraction.lua
Normal file
@@ -0,0 +1,304 @@
|
||||
--- 模块功能:物美MQTT交互
|
||||
-- @author 杜兴杰
|
||||
-- @module 物美MQTT通信
|
||||
-- @license MIT
|
||||
-- @copyright 杜兴杰
|
||||
-- @email 1066950103@qq.com
|
||||
-- @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
|
||||
|
||||
return {
|
||||
Init = Init,
|
||||
DelayInit = DelayInit,
|
||||
SetCallbackPropertyData = SetCallbackPropertyData,
|
||||
SetCallbackFunctionData = SetCallbackFunctionData,
|
||||
SetCallbackEventData=SetCallbackEventData,
|
||||
SetCallbackInformationData=SetCallbackInformationData,
|
||||
SetPropertyPush =SetPropertyPush,
|
||||
SetFunctionPush =SetFunctionPush,
|
||||
SetEventPush=SetEventPush,
|
||||
GetSubscriberAll=GetSubscriberAll,
|
||||
OnRecvData=OnRecvData,
|
||||
GetData = GetData
|
||||
}
|
||||
130
sdk/合宙/air780e/lua/WuMeiTest.lua
Normal file
130
sdk/合宙/air780e/lua/WuMeiTest.lua
Normal file
@@ -0,0 +1,130 @@
|
||||
--- 模块功能:物美MQTT测试
|
||||
-- @author 杜兴杰
|
||||
-- @module 物美MQTT通信测试
|
||||
-- @license MIT
|
||||
-- @copyright 杜兴杰
|
||||
-- @email 1066950103@qq.com
|
||||
-- @release 2022.8.5
|
||||
module(..., package.seeall)
|
||||
WeiMeiComAuth = require"WeiMeiComAuth"
|
||||
WeiMeiComInteraction = require"WeiMeiComInteraction"
|
||||
WeiMeiApp = require"WeiMeiApp"
|
||||
--require"misc"
|
||||
--require"mqtt"
|
||||
|
||||
--[[特别注意, 使用mqtt库需要下列语句]]
|
||||
_G.sysplus = require("sysplus")
|
||||
|
||||
local ready = false
|
||||
|
||||
--物美配置参数相关配置
|
||||
local m_strEncryptionMode = "E"
|
||||
local m_strProductId = 253
|
||||
local m_strDeviceId = nil
|
||||
local m_strUserId = "1" -- admin
|
||||
local m_strMqttUser = "FastBee"
|
||||
local m_strMqttPassword = "P77A4MMCA20V0D0K"
|
||||
local m_strProductPassword = "K4PAICCX042H88E6" --产品密码
|
||||
local m_nMqttAuthenticationTimeout = 24*60*60*1 --24小时
|
||||
local m_strMqttIp = "www.fastbee.cn"
|
||||
local m_strDeviceAuthorizationCode = nill--= "A25040D2E34B483DA371B5F9A315BB43" --设备授权码
|
||||
|
||||
local mqttc = 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)
|
||||
mqttc:subscribe(ouSubscriber.topic,ouSubscriber.qos) --todo 可能订阅数量有限
|
||||
--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
|
||||
sys.waitUntil("IP_READY", 50000)
|
||||
|
||||
--创建一个MQTT客户端
|
||||
m_strDeviceId = mobile.imei()
|
||||
MqttInit()
|
||||
while m_mqttFlag == 0 do
|
||||
sys.wait(50)
|
||||
end
|
||||
log.info("ClientId=" .. WeiMeiComAuth.GetClientId() )
|
||||
log.info("User=" .. WeiMeiComAuth.GetUser() )
|
||||
log.info("Password=" .. WeiMeiComAuth.GetPassword() )
|
||||
log.info("Ip=" .. WeiMeiComAuth.GetIP() )
|
||||
|
||||
mqttc = mqtt.create(nil,WeiMeiComAuth.GetIP(), 1883, false, ca_file)
|
||||
mqttc:auth(WeiMeiComAuth.GetClientId(),WeiMeiComAuth.GetUser(),WeiMeiComAuth.GetPassword()) -- client_id必填,其余选填
|
||||
mqttc:keepalive(30) -- 默认值240s
|
||||
mqttc:autoreconn(true, 3000) -- 自动重连机制
|
||||
|
||||
mqttc:on(function(mqtt_client, event, data, payload)
|
||||
-- 用户自定义代码
|
||||
log.info("mqtt", "event", event, mqtt_client, data, payload)
|
||||
if event == "conack" then
|
||||
--连接成功
|
||||
sys.publish("mqtt_conack")
|
||||
if GetSubscriberAll() == true then
|
||||
WeiMeiComInteraction.SetCallbackInformationData(WeiMeiApp.InformationData)
|
||||
WeiMeiComInteraction.SetCallbackPropertyData(WeiMeiApp.PropertyData)
|
||||
WeiMeiComInteraction.SetPropertyPush(1000*30) --30秒钟定时上传一次属性
|
||||
WeiMeiComInteraction.DelayInit()
|
||||
end
|
||||
elseif event == "recv" then
|
||||
log.info("mqtt", "downlink", "topic", data, "payload", payload)
|
||||
WeiMeiComInteraction.OnRecvData(data,payload);
|
||||
elseif event == "sent" then
|
||||
log.info("mqtt", "sent", "pkgid", data)
|
||||
-- elseif event == "disconnect" then
|
||||
-- 非自动重连时,按需重启mqttc
|
||||
-- mqtt_client:connect()
|
||||
end
|
||||
end)
|
||||
|
||||
mqttc:connect()
|
||||
sys.waitUntil("mqtt_conack") --todo 超时重启
|
||||
while true do
|
||||
-- mqttc自动处理重连
|
||||
result,strMessage,strTopic,nQos = WeiMeiComInteraction.GetData()
|
||||
if result == true then
|
||||
local mqttResult = mqttc:publish(strTopic,strMessage,nQos)
|
||||
if not mqttResult then
|
||||
break
|
||||
end
|
||||
end
|
||||
sys.wait(50)
|
||||
end
|
||||
mqttc:close()
|
||||
mqttc = nil
|
||||
end
|
||||
)
|
||||
31
sdk/合宙/air780e/lua/main.lua
Normal file
31
sdk/合宙/air780e/lua/main.lua
Normal file
@@ -0,0 +1,31 @@
|
||||
--- 模块功能:lvgldemo
|
||||
-- @module main
|
||||
-- @author 杜兴杰
|
||||
-- @release 2023.03.15
|
||||
|
||||
-- LuaTools需要PROJECT和VERSION这两个信息
|
||||
PROJECT = "wumeiAir780"
|
||||
VERSION = "0.0.1"
|
||||
|
||||
log.info("main", PROJECT, VERSION)
|
||||
|
||||
-- sys库是标配
|
||||
_G.sys = require("sys")
|
||||
|
||||
|
||||
|
||||
--添加硬狗防止程序卡死
|
||||
if wdt then
|
||||
wdt.init(9000)--初始化watchdog设置为9s
|
||||
sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
|
||||
end
|
||||
|
||||
|
||||
require "WuMeiTest"
|
||||
|
||||
-- 用户代码已结束---------------------------------------------
|
||||
-- 结尾总是这一句
|
||||
sys.run()
|
||||
-- sys.run()之后后面不要加任何语句!!!!!
|
||||
|
||||
|
||||
65
sdk/合宙/air780e/lua/mainAir724.lua
Normal file
65
sdk/合宙/air780e/lua/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/合宙/air780e/lua/mainVscode.lua
Normal file
10
sdk/合宙/air780e/lua/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