lua MQTT阿里云IOT通讯

 

完整实例个人测试版,subscribe-订阅可忽略

require('json')
require('encdec')
mqtt = require('mosquitto')
num = math.random(0,1)
log(num)
data = json.encode({params = {
      LightStatus = num,
		--humidity=19
}})
deviceName = 'deviceName' -- deviceName
client = 'deviceName' -- 不一定是deviceName,这个是我随意命名
productKey = 'productKey' -- productKey
deviceSecret = 'deviceSecret' -- deviceSecret
broker = productKey..'.iot-as-mqtt.cn-shanghai.aliyuncs.com'
port = 1883
username = deviceName..'&'..productKey
password ='clientId'..client..'deviceName'..deviceName..'productKey'..productKey
device_id = client..'|securemode=3,signmethod=hmacsha1|'
password = string.upper(encdec.hmacsha1(password, deviceSecret)) 
client = mqtt.new(device_id)
running = true
topic = '/sys/'..productKey..'/'..deviceName..'/thing/event/property/post'
client.ON_CONNECT = function(stat, code, err)
  if stat then
    log('publish')
    client:publish(topic, data) --发布
    client:subscribe(topic) --订阅
  else
    log('mqtt connect error', err, code)
  end
  --running = false
  --client:disconnect()
end

client.ON_MESSAGE = function(mid, topic, data)
  log('message', topic, data)
  client:disconnect()
end

client:version_set(mqtt.PROTOCOL_V311)
--client:tls_set('/home/ftp/ca.cer') -- securemode=1需要签证,此文件可在阿里文档寻找
client:login_set(username, password)
res, err = client:connect(broker, port)
if not res then
  log('mqtt initial connect error', err)
  running = false
end

if res then
  while running do
    res, err, code = client:loop()
    if not res then
      if running then
        log('mqtt loop error', err, code)
      end
      break
    end
  end
else
  log('mqtt connection failed', err)
end

用户,密码生成:http://mqtt.lovemcu.cn/

subscribe-订阅

client.ON_CONNECT = function(status, rc, err)
  if status then
    log('connect ok')
    client:subscribe(topic)--订阅topic
    
  else
    log('connect error', rc, err)
  end
end

client.ON_MESSAGE = function(mid, topic, data) --topic下发信息
  log('message', topic, data)
end

个人测试版

require('json')
num = math.random(0,1)
log(num)
data = json.encode({params={
      LightStatus=num,
		--humidity=19
}})

broker = '。。。。'
port = 1883
device_id = '。。。'
username = '。。。。'
password = '。。。'
pk = '。。。'

mqtt = require('mosquitto')
client = mqtt.new(device_id)
running = true
topic = '/sys/。。。/light/thing/event/property/post'
client.ON_CONNECT = function(stat, code, err)
  if stat then
    log('publish')
    client:publish(topic , data)
    client:subscribe(topic)
  else
    log('mqtt connect error', err, code)
  end
  --running = false
  --client:disconnect()
end

client.ON_MESSAGE = function(mid, topic, data)
  log('message', topic, data)
  client:disconnect()
end


client:version_set(mqtt.PROTOCOL_V311)
--client:tls_set('/home/ftp/ca.cer')
client:login_set(username, password)
res, err = client:connect(broker, port)
if not res then
  log('mqtt initial connect error', err)
  running = false
end

if res then
  while running do
    res, err, code = client:loop()

    if not res then
      if running then
        log('mqtt loop error', err, code)
      end

      break
    end
  end
else
  log('mqtt connection failed', err)
end

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值