两个步骤:
1、建立客户端
mqtt.Client(clientid, keepalive[, username, password, cleansession, max_message_length])
该方法返回一个客户端对象。
2、连接服务器
客户端对象:connect(host[, port[, secure]][, function(client)[, function(client, reason)]])
gpio.mode(2,gpio.OUTPUT)
wifi.setmode(wifi.STATION)
cfg = {}
cfg.ssid = "kyn"
cfg.pwd = "20160118"
wifi.sta.config(cfg)
wifi.sta.connect()
--牛逼的三元组
--mqtt.Client(clientid, keepalive[, username, password, cleansession, max_message_length]) 注意该函数,返回一个对象。这个对象再调用方法进行连接。
--设备ID,这个用作客户端的 clientid
DeviceId = "587667371"
--产品ID,这个用作客户端的 username
ProductId = "325428"
--鉴权信息,这个用作客户端的 password
AuthoInfo = "test"
host = "183.230.40.39"
--端口不用加引号的
port = 6002
timer = tmr.create()
function con()
if wifi.sta.getip() == nil then
print("coneting........")
else
timer:stop()
print("success!~")
gpio.write(2,gpio.HIGH)
print(wifi.sta.getip())
client_hum = mqtt.Client(DeviceId,120,ProductId,AuthoInfo)
client_hum:connect(host,port,0,function(client)
print("connect success!")
end)
end
end
timer:alarm(1000,tmr.ALARM_AUTO,con)