this app deals with mqtt communication with secure ssl connection, everything is ok on the simulator, it connects to the mqtt broker and exchanges data while when simulating on a real device the app crashes with a black screen . portion of code and project annex
inviato.rar (1.5 MB)
code:
–
– title.lua
local composer = require( “composer” )
local scene = composer.newScene()
local mqtt = require (‘mqtt.init’)
–local mqttp = require ‘paho.mqtt’
local socket = require (“socket”)
–**************
local http = require(‘socket.http’)
local ltn12 = require(‘ltn12’)
–local crypto = require “crypto”
–**************
–require(“ssl”)
local json = require “json”
local bitlib = require( “plugin.bit” )
local ssl = require( “plugin.openssl” )
–local plugin_luasec_ssl = require(‘plugin_luasec_ssl’)
lua_openssl_version, lua_version, openssl_version = openssl.version()
print( "lua-openssl version: " … lua_openssl_version, lua_version, openssl_version )
– forward declaration
local background
local function vaiSuCanc()
composer.gotoScene( “cancella”, “slideLeft”, 200 )
end
local function vaiSuImpo()
composer.gotoScene( “impo”, “slideLeft”, 200 )
end
local function vaiSuDisp()
composer.gotoScene( “aggiungi”, “slideLeft”, 200 )
end
local function vaiSuInit()
composer.gotoScene( “init” )
end
function scene:create( event )
local sceneGroup = self.view
-- Called when the scene's view does not exist.
--
-- INSERT code here to initialize the scene
-- e.g. add display objects to 'sceneGroup', add touch listeners, etc.
-- display a background image
local background = display.newRect(0,0,360, 640) --background = display.newImageRect( sceneGroup, "img/schermoBlu.png", display.contentWidth, display.contentHeight )
background:setFillColor(0.3,0.3,0.3)
background.x = display.contentCenterX – sfondo da 0 alla massima copertura
background.y = display.contentCenterY – uguale per Y
– local buttonMenu = display.newImageRect(“img/menu.png”,50,50)
– buttonMenu:setFillColor(1,0,0) – Set the fill color to Red (These are normalized R,G
– buttonMenu.x = 35-- centerX
– buttonMenu.y = 35–centerY
local buttonMenu2 = display.newImageRect(“img/frecciaB.png”,30,30)
buttonMenu2:setFillColor(1,1,1) – Set the fill color to Red (These are normalized R,G
buttonMenu2.x = display.contentCenterX -120 --35-- centerX
buttonMenu2.y = display.contentCenterY -260 --35–centerY
-- Add more text
local menuText = display.newText( " DOMO ELKOR", 10, 10, native.systemFont, 15 )
menuText.x = display.contentCenterX -0 --+ 35--display.contentWidth -0
menuText.y = display.contentCenterY -260
menuText:setFillColor(1,1,1)
local buttonImpo = display.newImageRect(“img/uomo+.png”,30,30)
buttonImpo:setFillColor(1,1,1) – Set the fill color to Red (These are normalized R,G
buttonImpo.x = display.contentCenterX +100-- centerX
buttonImpo.y = display.contentCenterY +220–centerY
-- Add more text
local impoText = display.newText( " Account", 0, 0, native.systemFont, 11 )
impoText.x = display.contentCenterX +100 --display.contentCenterX + 35--display.contentWidth -0
impoText.y = display.contentCenterY +250 --display.contentCenterY -150--display.contentHeight - (display.contentHeight*0.1)
impoText:setFillColor(1,1,1)
local buttonDisp = display.newImageRect(“img/piu.png”,30,30)
buttonDisp:setFillColor(1,1,1) – Set the fill color to Red (These are normalized R,G
buttonDisp.x = display.contentCenterX -0 – centerX
buttonDisp.y = display.contentCenterY +220–centerY
local buttonCanc = display.newImageRect(“img/meno.png”,25,25)
buttonCanc:setFillColor(1,1,1) – Set the fill color to Red (These are normalized R,G
buttonCanc.x = display.contentCenterX -100 – centerX
buttonCanc.y = display.contentCenterY +220–centerY
-- Add more text
local dispoText = display.newText( " Aggiungi", 10, 10, native.systemFont, 11 )
dispoText.x = display.contentCenterX -0 --display.contentCenterX + 35--display.contentWidth -0
dispoText.y = display.contentCenterY +250 --display.contentCenterY -150--display.contentHeight - (display.contentHeight*0.1)
dispoText:setFillColor(1,1,1)
local cancText = display.newText( " Cancella", 10, 10, native.systemFont, 11 )
cancText.x = display.contentCenterX -100 --display.contentCenterX + 35–display.contentWidth -0
cancText.y = display.contentCenterY +250 --display.contentCenterY -150–display.contentHeight - (display.contentHeight*0.1)
cancText:setFillColor(1,1,1)
-- all display objects must be inserted into group
sceneGroup:insert( background )
sceneGroup:insert( dispoText )
sceneGroup:insert( impoText )
sceneGroup:insert( buttonImpo )
sceneGroup:insert(buttonDisp)
sceneGroup:insert(menuText)
sceneGroup:insert(buttonMenu2)
sceneGroup:insert(cancText)
sceneGroup:insert(buttonCanc)
buttonImpo:addEventListener( “tap”, vaiSuImpo ) – vado sullo screen (scena) game.lua
buttonDisp:addEventListener( “tap”, vaiSuDisp )
buttonMenu2:addEventListener( “tap”, vaiSuInit )
buttonCanc:addEventListener( “tap”, vaiSuCanc )
end
–========== funzione sostituisce caratteri in stringa ===============
function replaceChar(pos, str, r)
return table.concat{str:sub(1,pos-1), r, str:sub(pos+1)}
end
–===================================================================
local function tx()
– create mqtt client
local client = mqtt.client{
– NOTE: this broker is not working sometimes; comment username = “…” below if you still want to use it
– uri = “test.mosquitto.org”,
uri = “mqtt.flespi.io”,
– NOTE: more about flespi tokens: https://flespi.com/kb/tokens-access-keys-to-flespi-platform
username = “stPwSVV73Eqw5LSv0iMXbc4EguS7JyuZR9lxU5uLxI5tiNM8ToTVqNpu85pFtJv9”,
clean = true,
}
print(“created MQTT client”, client)
client:on{
connect = function(connack)
if connack.rc ~= 0 then
print(“connection to broker failed:”, connack:reason_string(), connack)
return
end
print(“connected:”, connack) – successful connection
-- subscribe to test topic and publish message after it
assert(client:subscribe{ topic="luamqtt/#", qos=1, callback=function(suback)
print("subscribed:", suback)
-- publish test message
print('publishing test message "hello" to "luamqtt/simpletest" topic...')
assert(client:publish{
topic = "luamqtt/simpletest",
payload = "hello",
qos = 1
})
end})
end,
message = function(msg)
assert(client:acknowledge(msg))
print("received:", msg)
print("disconnecting...")
assert(client:disconnect())
end,
error = function(err)
print("MQTT client error:", err)
end,
}
print(“running ioloop for it”)
mqtt.run_ioloop(client)
print(“done, ioloop is stopped”)
end
–=====================================================================
function scene:show( event )
–sceneGroup = self.view
local phase = event.phase
if phase == "will" then
-- Called when the scene is still off screen and is about to move on screen
elseif phase == "did" then
-- Called when the scene is now on screen
--
-- INSERT code here to make the scene come alive
-- e.g. start timers, begin animation, play audio, etc.
--background.touch = onBackgroundTouch
--background:addEventListener( "touch", background )
--###########################################################
tx()
--###########################################################
end
end
–[[
function ricevi(rx)
local payload = rx.payload
local lung = string.len(payload)
local payl = replaceChar(lung,payload," “)
payl = replaceChar(1,payload,” ")
print(“stampa segnale :”,payl)
end
–]]
function scene:hide( event )
–sceneGroup = self.view
local phase = event.phase
if event.phase == "will" then
-- Called when the scene is on screen and is about to move off screen
elseif phase == "did" then
-- Called when the scene is now off screen
--composer.removeScene( "menu" )
end
end
function scene:destroy( event )
–local sceneGroup = self.view
-- Called prior to the removal of scene's "view" (sceneGroup)
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene