Android Question MQTT & Widget Receiver

amir hosein

Member
Hello everyone
I am preparing an app to send my SMS to a MQTT server .
I inspired from some examples of MQTT and widget receiver and tried to combine them .
When a SMS received to my phone , the receiver recognized it and try to send a sample text (or received SMS text) to a MQTT server but it fails to connect .
Some time it connects and send a sample text and in 95% of tries it can't connect .
Please help me to find my faults .
The SMS service works fine and every time a SMS (text) is received (by phone) , a flag (newSMS) will be true .
In the following code , I make the connection in a test routine (just for test) that placed at the end of the code . ( label1_click )

Regards

widget receiver - code:
Sub Process_Globals
    Private rv As RemoteViews
    Private currentImage As Int
    Dim tmr As Timer
    Public smsBody As String
    Public smsUpdate As Boolean
    Public newSms As Boolean
    Dim userName As String = "AUserNameForMQTT"
    Dim passWord As String = "ThePassKeyForMQTT"
    Private mqtt As MqttClient
    Private mqttOption As MqttConnectOptions
    Private serializator As B4XSerializator
    Dim clientId As String
    Dim shouldBeSend As Boolean
    Private SubscribeTopic As String = "Sms2Mqtt/#"        'change topic!
    Private PublishTopic As String = "Sms2Mqtt/"            'change topic!
End Sub

'Called when an intent is received.
'Do not assume that anything else, including the starter service, has run before this method.
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    If FirstTime Then
        rv = ConfigureHomeWidget("L1", "rv", 60, "SMS-MQTT Widget",True)
    End If
    newSms=False
    smsBody="No New Message"
    rv.HandleWidgetEvents(StartingIntent)
    tmr.Initialize("tmr",5000)
    smsUpdate=True
    tmr.Enabled= True
End Sub

Sub tmr_Tick
    rv_RequestUpdate
'    If shouldBeSend Then
'        connect2Mqtt
'        shouldBeSend=False
'    End If
End Sub

Sub mqtt_Disconnected
    ToastMessageShow("Mqtt Disconnected",False)
End Sub

Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
    Dim GetIDFromTopic As String = Topic.SubString(9)    'Topic.SubString(10)'get UserId
    Dim str1 As String = serializator.ConvertBytesToObject(Payload) 'BytesToString(Payload,0,Payload.Length , "UTF8")
    ToastMessageShow("From:" & GetIDFromTopic & CRLF & str1 , False)
End Sub

Sub mqtt_Connected (Success As Boolean)
    If Success = False Then
        Log(LastException)
        rv.SetVisible("lblLedGrn",False)
        rv.SetVisible("lblLedRed",True)
    Else
        Sleep(10)
        mqtt.Subscribe(SubscribeTopic, 1)
        PublishTopic = PublishTopic & mqtt.ClientId
        rv.SetVisible("lblLedRed",False)
        rv.SetVisible("lblLedGrn",True)
        Sleep(1000)
        mqtt.Publish(PublishTopic, serializator.ConvertObjectToBytes("<Joined>"))
    End If
    rv.UpdateWidget
End Sub

private Sub connect2Mqtt
'    If newSms = False Then Return
    If mqtt.IsInitialized Then mqtt.Close
    Do While mqtt.IsInitialized
        Sleep(100)
    Loop
    clientId = "AHT_SMS_SERVICE" & "-" & Rnd(0, 999999999)    'create a unique id
    ToastMessageShow(clientId , False)
    mqtt.Initialize("mqtt", "tcp://broker.hivemq.com:1883",  clientId)
    mqttOption.Initialize(userName, passWord)
    mqtt.Connect2(mqttOption)
End Sub

Private Sub rv_RequestUpdate
    SetTime
    If smsUpdate Then
        rv.SetText("lblMess", smsBody)
        smsUpdate= False
'        If newSms Then
'            newSms=False
'        End If
    End If
    rv.SetVisible("lblLedRed",False)
    rv.SetVisible("lblLedGrn",False)
    rv.UpdateWidget
End Sub

Private Sub SetTime
    rv.SetText("Label1", DateTime.Time(DateTime.Now))
End Sub

Sub label1_Click
    ToastMessageShow("Clicked",False)
    shouldBeSend= True
    newSms=True
    connect2Mqtt
end sub
 
Last edited:

amir hosein

Member
Hello everyone .
My problem is solved . It was because of broker that disconnected for several hours !!!
I chose another broker and it worked but after 12 hours it disconnected again !!!
Finally , After 3 hours It connected again and not disconnected so far .

Regards
 
Upvote 0
Top