文档二级目录222
pin=17
gpio.mode(pin,gpio.OUTPUT)
cfg={};cfg.ssid='leodroid';cfg.pwd='142857zuo';wifi.startsta(cfg);cfg=nil;
tmr.start(1,1000,function()
print('Check STA Status:'..wifi.sta.getip())
if(wifi.sta.getip()~='0.0.0.0') then
tmr.stop(1)
doMQTT()
end
end)
function doMQTT()
clientid = '/wifimcu1/hello'
keepalive = 120
username = ''
password = ''
mqttClt = mqtt.new(clientid,keepalive,username,password)
mqtt.on(mqttClt,'connect',function()
print('mqtt connected to server')
QoS = mqtt.QOS0
mqtt.subscribe(mqttClt,clientid,QoS)
print('mqtt subscribe topic:'..clientid)
mqtt.publish(mqttClt,clientid,mqtt.QOS0, 'hi,I am wifimcu1')
end)
mqtt.on(mqttClt,'offline',function()
print('mqtt disconnected from server')
end)
mqtt.on(mqttClt,'message',cb_messagearrived)
server = '106.14.44.95'
port = 1883
mqtt.start(mqttClt,server,port)
end
function cb_messagearrived(topic,data)
--print('[Message Arrived]\r\ntopic:'..topic..' \r\nmessage:'..data)
if data == "0" then
--stop()
print(0)
gpio.write(pin,gpio.HIGH)
end
if data == "1" then
--go_forward()
print(1)
gpio.write(pin,gpio.LOW)
end
end