-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
66 lines (46 loc) · 1.71 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
display.setStatusBar( display.HiddenStatusBar )
local MQTT = require("mqtt.mqtt_library")
MQTT.Utility.set_debug(true)
-----------------------------------------------
-- Initialize static UI elements
-----------------------------------------------
local background = display.newRect(0,0,display.contentWidth, display.contentHeight)
background:setFillColor(.29,.51,.80)
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
function callback(topic,payload)
print("topic and payload",topic,payload)
end
mqtt_client = MQTT.client.create("iot.eclipse.org", 1883, callback)
mqtt_client:connect(system.getInfo( "deviceID" ))
mqtt_client.KEEP_ALIVE_TIME = 120
mqtt_client:subscribe({ "/jen" })
local function networkListener( event )
if ( event.isError ) then
print( "Network error!")
else
print ( event.response )
end
end
local function onTouch( event )
if event.phase == "ended" then
print(event.target.id)
if event.target.id == "red" then
mqtt_client:publish("/jen", "red")
else
mqtt_client:publish("/jen", "green")
end
return true
end
end
local redCircle = display.newCircle( 100, 100, 30 )
redCircle.id = "red"
redCircle:setFillColor(1,0,0)
redCircle.x,redCircle.y = display.contentWidth/2, display.contentHeight/3
redCircle:addEventListener("touch",onTouch)
local greenCircle = display.newCircle( 100, 100, 30 )
greenCircle.id = "green"
greenCircle:setFillColor(0,1,0)
greenCircle.x,greenCircle.y = display.contentWidth/2, display.contentHeight/2
greenCircle:addEventListener("touch",onTouch)
timer.performWithDelay(500, function(event) mqtt_client:handler() end, 0)