B4i Library iMQTT - Official iOS MQTT client

Discussion in 'iOS Libraries' started by Erel, Oct 20, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Attached Files:

    Last edited: Apr 27, 2017
  2. RichardHirst

    RichardHirst Member Licensed User

    Hello Erel.

    Very interested in this library.. Installed the lastest IDE and libs. I receive the error "<B4IExceptionWrapper: (null)>" when I run the app. Connection Error.

    Any help would be great

    Richard
     
    cloner7801 likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    MQTT depends on a broker (see the tutorial). If you want to use CloudMQTT then you need to register with them and then set the user name and password (and the correct link).
     
  4. Hypnos

    Hypnos Active Member Licensed User

    Hi All,

    I'm now testing the MQTT library, eveything ok if I using host builder but if I using my own mac local builder, the app will quit immediately after I run it. seems the problem related to my local builder.

    I download the library from : www.b4x.com/b4i/files/iMQTT.zip
    and put the iMQTT.h and libiMQTT.a into the Libs folder

    Build Server version : 2.3

    Any hints?

    Thanks!
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    What is the Xcode version?
     
  6. Hypnos

    Hypnos Active Member Licensed User

    Hi Erel,

    Version 7.1.1 (7B1005)
    Thanks!
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    I've uploaded a new zip file. I think that the previous one was corrupted.

    Note that you can compare the libraries files to the online files: http://63.135.170.51:51041/test
     
  8. Hypnos

    Hypnos Active Member Licensed User

    Thank you Erel!
    Work now!
     
  9. gjt211

    gjt211 Member Licensed User

    Hi Erel,

    I have downloaded the attached file in the first post, but how do I open it in B4i?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Double click on the B4i file.
     
  11. gjt211

    gjt211 Member Licensed User

    Hi Erel,

    There are no B4i files in the ZIP archive.
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    There should be a folder named MQTTCanvas in the zip file. If you don't see it then your browser or anti-virus have probably broken the file.
     
  13. cloner7801

    cloner7801 Active Member Licensed User

    How can I send file to all users in network and opened the application?
     
  14. cloner7801

    cloner7801 Active Member Licensed User

    I have this problem too.

    How can I fix this?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

  16. cloner7801

    cloner7801 Active Member Licensed User

  17. Erel

    Erel Administrator Staff Member Licensed User

    You will need to run the broker somewhere else.
     
  18. Descartex

    Descartex Well-Known Member Licensed User

    Hi.
    When i try to run this sample, changing the data for my cloudMQTT account... it raises an error
    Application_Start
    MQTTSessionManager connecting
    Application_Active
    MQTTSessionManager reconnecting
    Unexpected event (missing RaisesSynchronousEvents): mqtt_disconnected
    <B4IExceptionWrapper: (null)>
    <B4IExceptionWrapper: (null)>
    Error occurred on line: 55 (Main)
    -[__NSCFString setDataBuffer:]: unrecognized selector sent to instance 0x155fa050
    Stack Trace: (
    CoreFoundation <redacted> + 154
    libobjc.A.dylib objc_exception_throw + 38
    CoreFoundation <redacted> + 202
    CoreFoundation <redacted> + 706
    CoreFoundation _CF_forwarding_prep_0 + 24
    MQTT Example -[MQTTDecoder stream:handleEvent:] + 1978
    CoreFoundation <redacted> + 118
    CoreFoundation <redacted> + 196
    CoreFoundation <redacted> + 328
    CFNetwork <redacted> + 74
    CFNetwork <redacted> + 36
    CFNetwork <redacted> + 42
    CFNetwork <redacted> + 122
    CFNetwork <redacted> + 34
    CFNetwork <redacted> + 54
    CFNetwork <redacted> + 144
    CFNetwork <redacted> + 58
    CoreFoundation <redacted> + 578
    CoreFoundation <redacted> + 14
    CoreFoundation <redacted> + 206
    CoreFoundation <redacted> + 622
    CoreFoundation CFRunLoopRunSpecific + 522
    CoreFoundation CFRunLoopRunInMode + 106
    GraphicsServices GSEventRunModal + 138
    UIKit UIApplicationMain + 1136
    MQTT Example main + 108
    libdyld.dylib <redacted> + 2
    )

    on my android version, it runs good, same data.
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Can you post your code without the user name and password?
     
  20. Descartex

    Descartex Well-Known Member Licensed User

    Yes,
    Code:
    'Code module
    #Region  Project Attributes
        
    #ApplicationLabel: MQTT Example
        
    #Version: 1.0.0
        
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
        #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
        
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
        
    #ProvisionFile: DescartexGeneral.mobileprovision
    #End Region

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'Public variables can be accessed from all modules.
        Public App As Application
        
    Public NavControl As NavigationController
        
    Private Page1 As Page
        
    Private mqtt As MqttClient
        
    Private mytopic As String
        
    Private serializator As B4XSerializator
        
    Type CircleData (x As Double, y As Double, clr As Int)
        
    Private serverURI As String = "tcp://***.cloudmqtt.com:*****"
        
    Private canvas1 As Canvas
        
    Private lblStatus As Label
        
    Private Panel1 As Panel
    End Sub

    Private Sub Application_Start (Nav As NavigationController)
        NavControl = Nav
        Page1.Initialize(
    "Page1")
        Page1.RootPanel.LoadLayout(
    "1")
        NavControl.ShowPage(Page1)
        canvas1.Initialize(Panel1)
        mqtt.Initialize(
    "mqtt", serverURI, Rnd(0999999999) & DateTime.Now)
        
    Dim mo As MqttConnectOptions
        mo.Initialize(
    "myuser""mypass"'set user name and password!!!
    '    mo.SetLastWill("last", "test iohui".GetBytes("utf8"), 1, False)
        mqtt.Connect2(mo)
        mytopic = 
    "mytopic/" & mqtt.ClientId
    End Sub



    Private Sub Page1_Resize(Width As Int, Height As Int)
        canvas1.Initialize(Panel1)   
    End Sub

    Sub mqtt_Connected (Success As Boolean)
        
    If Success = False Then
            
    Log(LastException)   
            lblStatus.Text = 
    "Error connecting"
        
    Else
            lblStatus.Text = 
    "Connected"
            mqtt.Subscribe(
    "mytopic/#"0)
        
    End If
    End Sub

    Private Sub mqtt_Disconnected
        lblStatus.Text = 
    "Disconnected"
        mqtt.Connect
    End Sub

    Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
        
    Dim obj As Object = serializator.ConvertBytesToObject(Payload)
        
    If obj Is CircleData Then
            
    If Topic = mytopic Then Return 'the circle was already drawn
            Dim cd As CircleData = obj
            DrawCircleData(cd)
        
    Else 'obj is string
            Dim s As String = obj
            
    Select s
                
    Case "clear"
                    canvas1.DrawColor(
    Colors.White)
                    canvas1.Refresh
                
    Case "close"
                    
    'no closing in iOS...
            End Select
        
    End If
    End Sub

    Private Sub DrawCircleData(cd As CircleData)
        canvas1.DrawCircle(cd.X * (
    100dip / 100), cd.Y  * (100dip / 100), 20dip, cd.clr, True0)
        canvas1.Refresh
    End Sub

    Private Sub Page1_Touch(Action As Int, X As Float, Y As Float)
        
    If mqtt.Connected = False Then  Return
        
    Dim cd As CircleData
        cd.x = x * (
    100 / 100dip)
        cd.y = y * (
    100 / 100dip)
        cd.clr = 
    Rnd(0x80000000, -1)
        DrawCircleData(cd)
        mqtt.Publish2(mytopic, serializator.ConvertObjectToBytes(cd), mqtt.QOS_2_EXACTLY_ONCE, 
    True)
    End Sub

    Private Sub Application_Background
       
    End Sub
    s:
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice