-- Sends a simple ping to the broker localfunctionsend_ping() m:publish("$oc/devices/641474d140773741f9fb9dab_wifi1234/sys/bootstrap/up",gpio.read(1)..gpio.read(2)..gpio.read(3)..gpio.read(4),0,1) print("Published the status") end
localfunctionget_request(topic) local t = string.find(topic, "=") returnstring.sub(topic,t+1,-1) end
--响应消息命令 localfunctionsend_suf() msg = {} msg.object_device_id = "641474d140773741f9fb9dab_wifi1234" msg.name="open_light" msg.id="12345" msg.content ={} msg.content.result = true print(sjson.encode(msg)) m:publish("$oc/devices/641474d140773741f9fb9dab_wifi1234/sys/messages/up",sjson.encode(msg),0,1) print("Published the responce") end -- 响应命令 localfunctionsend_command(request_id) local msg ={} msg.result_code = 0 msg.response_name = "COMMAND_RESPONSE" msg.paras ={} msg.paras.result="success" m:publish("$oc/devices/641474d140773741f9fb9dab_wifi1234/sys/commands/response/request_id="..request_id,sjson.encode(msg),0,1) end
IO_BLINK=4 gpio.mode(IO_BLINK, gpio.OUTPUT) localfunctionblinking(flag) print(flag) gpio.write(IO_BLINK, flag and gpio.HIGH or gpio.LOW) end
localfunctionconsume_data( payload, request_id ) print(sjson.decode(payload).paras.light) if sjson.decode(payload).paras.light == "on"then blinking(true) else blinking(false) end --do someting with the payload and send responce send_suf() print(request_id) send_command(request_id) end
-- Sends my id to the broker for registration localfunctionregister_myself() m:subscribe(config.ENDPOINT,0,function(conn) print("Successfully subscribed to data endpoint") send_ping() end) end