MQTT

SwordfishUser.MQTT History

Hide minor edits - Show changes to output

Added lines 18-19:

This library is [[http://sfcompiler.co.uk/phpBB3/viewtopic.php?f=5&t=1944| compatible ]] with this hardware also.
Deleted lines 27-28:
You can download test programs here: [[http://sfcompiler.co.uk/wiki/uploads/coccoliso/Test-MQTT-SF-Programs.zip | Swordfish test programs]]
Added line 30:
!!! Simple MQTT reader test program
Deleted line 58:
Include "Config_NOWDT"
Changed line 99 from:
   BrokerAddr(4)  As Byte    = (192,168,1,50),'(85,119,83,194), (2,229,136,20), (192,168,1,50)
to:
   BrokerAddr(4)  As Byte    = (85,119,83,194),     ' <-- MOSQUITTO BROKER
Added lines 191-192:

!!! Simple MQTT sender test program
Changed line 197 from:
*  Author  : M.Cerutti                                                      *
to:
*  Author  : Coccoliso                                                      *
Changed line 262 from:
   BrokerAddr(4)  As Byte    = (192,168,1,50),
to:
   BrokerAddr(4)  As Byte    = (85,119,83,194),   ' <-- MOSQUITTO BROKER
Added lines 17-18:
You can download Swordfish library modules here: [[http://sfcompiler.co.uk/wiki/uploads/coccoliso/MQTT-SF-Library.zip| MQTT Library ]]
Added lines 22-23:
You can download vb.net windows client examples here: [[http://www.firewing.info/user-uploads/FirewingUser/MQTT/MQTT-Windows-Client.zip | VB.NET test client]]
Changed lines 28-316 from:
Clearly not going to go into too much detail of MQTT but would definitely recommend to take a ride on the Internet to gather information and in a week or so you'll be more than satisfied.
to:
You can download test programs here: [[http://sfcompiler.co.uk/wiki/uploads/coccoliso/Test-MQTT-SF-Programs.zip | Swordfish test programs]]

Clearly not going to go into too much detail of MQTT but would definitely recommend to take a ride on the Internet to gather information and in a week or so you'll be more than satisfied.

=code [=
{
*****************************************************************************
*  Name    : MQTT-Reader.BAS                                                *
*  Author  : Coccoliso                                                      *
*  Notice  : Copyright (c) 2015 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 16/03/2015                                                    *
*  Version : 2.0                                                            *
*  Notes  : Added MQTT_ETH_CHIP for W5100 support                          *
*          :                                                                *
*****************************************************************************
}
Device = 18f2682
Clock = 40

#option WIZ_DHCP_DEBUG  = false
#option WIZ_DNS_DEBUG  = false
#option MQTT_DEBUG      = true            // Set USART debug flag
#option MQTT_DUMP      = true            // Enable MQTT message hex dump on USART
#option MQTT_ETH_CHIP  = W5500            // Eth chip selection (W5100 or W5500)
#option MQTT_TICKS      = true

#option USE_DHCP = true
#option USE_DNS  = false
#option WIZ_SCS_PIN = PORTC.0
#option WIZ_RST_PIN = PORTC.1 

Include "Config_NOWDT"
#if MQTT_ETH_CHIP = W5500
    Include "W5500-Consts"
    Include "W5500-Utils"
    Include "W5500"
#elseif MQTT_ETH_CHIP = W5100
    Include "W5100-Consts"
    Include "W5100-Utils"
    Include "W5100"
#endif
Include "MQTT"

Include "usart"
Include "convert"
Include "string"

Private Sub ConfigDisplay()
    USART.Write(#13,#10)
    USART.Write("MAC Address ",SARtoString(),#13,#10)
    USART.Write("IP Address  ",SIPRtoString(),#13,#10)
    USART.Write("Subnet Mask ",SUBRtoString(),#13,#10)
    USART.Write("Gateway    ",GARtoString(),#13,#10)
    USART.Write(#13,#10)   
End Sub

#if USE_DHCP
Event OnUpdate()
    ConfigDisplay()
End Event

Event OnConflict()
End Event
#endif

// ##################################################################################

// ##################################################################################

Dim sockreg        As Byte

Private Const
    BrokerAddr(4)  As Byte    = (192,168,1,50),'(85,119,83,194), (2,229,136,20), (192,168,1,50)
    Topic1                      = ("/mqtt/topic1"),
    Topic2                      = ("/mqtt/topic2"),
    TopicNum        As Byte    = 2,
    WillTopic                  = ("/mqtt/willtopic") 

Private Dim
    MessQueue1      As String(256),
    MessQueue2      As String(256),
    WillMessage    As String(256)
   
USART.SetBaudrate(br115200)
#if USE_DHCP = true
    USART.Write("Now find the DHCP server.. ",13,10)   
    DHCPSetEvents(@OnUpdate, @OnConflict)
    If DHCPTask(3) = false Then
        USART.Write("DHCP error, so must set static parms.. ",13,10)
        ConfigDisplay()
    End If   
#else
    USART.Write("Using static config.. ",13,10)   
    ConfigDisplay()
#endif

DelayMS(1000)

USART.Write(13,10)
USART.Write("My IP Address is " + SIPRtoString(),13,10)
DelayMS(2000)
sockreg = 0

MessQueue1= ""
MessQueue2= ""
WillMessage = "Sorry, see you later"

If Not MQTTSetBroker(sockreg, @BrokerAddr, 1883, $00) Then
USART.Write("MQTT: Errors into configuration parms",#13,#10)
    Close(sockreg)
Else
USART.Write("MQTT: Broker parms is ok..",#13,#10)
USART.Write("MQTT: Now store 2 subscriptions in memory",#13,#10)
    If MQTTAddSubscription(@Topic1,
                          QOS_LEVEL_EXACTLY_ONCE,
                          true,
                          true,
                          @MessQueue1,
                          SizeOf(MessQueue1)) Then
        USART.Write("MQTT: Subscriptions added successfully on Topic1", #13,#10)
    Else
        USART.Write("MQTT: Unable to set subscription on Topic1", #13,#10)
    End If

    If MQTTAddSubscription(@Topic2,
                          QOS_LEVEL_EXACTLY_ONCE,
                          true,
                          true,
                          @MessQueue2,
                          SizeOf(MessQueue2)) Then
        USART.Write("MQTT: Subscriptions added successfully on Topic2", #13,#10)
    Else
        USART.Write("MQTT: Unable to set subscription on Topic1", #13,#10)
    End If


USART.Write("MQTT: Now can try a connection to broker",#13,#10)

    If MQTTConnect("MyMQTTReader", false, false, 1, @WillTopic, @WillMessage) Then
        USART.Write("MQTT: MyReader is now connected to broker", #13,#10)           
        USART.Write("MQTT: Now send subscriptions to broker", #13,#10)           
        If MQTTSubscribe() Then
            USART.Write("MQTT: Subscriptions are made", #13,#10)
            While true
                MQTTService()
                If MQTTIsQueueChanged(0) Then
                    USART.Write("A new message on MessQueue1",13,10,
                                MessQueue1,13,10)
                End If
                If MQTTIsQueueChanged(1) Then
                    USART.Write("A new message on MessQueue2",13,10,
                                MessQueue2,13,10)
                End If
            End While               
        Else
            USART.Write("MQTT: Unable to set subscriptions on broker", #13,#10)
        End If
    Else
        USART.Write("MQTT: Unable to set connection", #13,#10)
    End If
End If

=]

=code [=
{
*****************************************************************************
*  Name    : MQTT-Sender.BAS                                                *
*  Author  : M.Cerutti                                                      *
*  Notice  : Copyright (c) 2015 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 16/03/2015                                                    *
*  Version : 2.0                                                            *
*  Notes  : Added MQTT_ETH_CHIP for W5100 support                          *
*          :                                                                *
*****************************************************************************
}

Device = 18f2682
Clock = 40

#option WIZ_DHCP_DEBUG  = false
#option WIZ_DNS_DEBUG  = false
#option MQTT_DEBUG      = false            // Set USART debug flag
#option MQTT_DUMP      = false            // Enable MQTT message hex dump on USART
#option MQTT_ETH_CHIP  = W5100            // Eth chip selection (W5100 or W5500)

#option USE_DHCP = true
#option USE_DNS  = false
#option WIZ_SCS_PIN = PORTC.0
#option WIZ_RST_PIN = PORTC.1 

#if MQTT_ETH_CHIP = W5500
    Include "W5500-Consts"
    Include "W5500-Utils"
    Include "W5500"
#elseif MQTT_ETH_CHIP = W5100
    Include "W5100-Consts"
    Include "W5100-Utils"
    Include "W5100"
#endif
Include "MQTT"

Include "usart"
Include "convert"
Include "string"

Private Sub ConfigDisplay()
    USART.Write(#13,#10)
    USART.Write("MAC Address ",SARtoString(),#13,#10)
    USART.Write("IP Address  ",SIPRtoString(),#13,#10)
    USART.Write("Subnet Mask ",SUBRtoString(),#13,#10)
    USART.Write("Gateway    ",GARtoString(),#13,#10)
    USART.Write("DNS Address ",AddrToString(@Dns_Addr),#13,#10)
    USART.Write(#13,#10)   
End Sub

#if USE_DHCP
Event OnUpdate()
    ConfigDisplay()
End Event

Event OnConflict()
End Event
#endif

// ##################################################################################

// ##################################################################################

Dim sockreg        As Byte

Private Const
    BrokerAddr(4)  As Byte    = (192,168,1,50),
    Topic1                      = ("/mqtt/topic1"),
    Topic2                      = ("/mqtt/topic2"),
    TopicNum        As Byte    = 2,
    WillTopic                  = ("/mqtt/willtopic")   

Private Dim
    WillMessage    As String(256)
   
USART.SetBaudrate(br115200)

#if USE_DHCP = true
    USART.Write("Now find the DHCP server.. ",13,10)   
    DHCPSetEvents(@OnUpdate, @OnConflict)
    If DHCPTask(3) = false Then
        USART.Write("DHCP error, so must set static parms.. ",13,10)
        ConfigDisplay()
    End If   
#else
    USART.Write("Using static config.. ",13,10)   
    ConfigDisplay()
#endif

DelayMS(1000)

USART.Write(13,10)
USART.Write("My IP Address is " + SIPRtoString(),13,10)
DelayMS(2000)
sockreg = 0
WillMessage = "Sorry, see you later"


If Not MQTTSetBroker(sockreg, @BrokerAddr, 1883, $00) Then
USART.Write("MQTT: Errors into configuration parms",#13,#10)
    Close(sockreg)
Else
USART.Write("MQTT: Broker parms is ok..",#13,#10)
USART.Write("MQTT: Now connect to it",#13,#10)

    If MQTTConnect("MyMQTTSender", false, false, 0, @WillTopic, @WillMessage ) Then
        USART.Write("MQTT: MySender is now connected to broker", #13,#10)           
        If MQTTPublish(@Topic1,"Hello world on Topic1") Then
            USART.Write("MQTT: Message published", #13,#10)
        Else
            USART.Write("MQTT: Unable to publish message", #13,#10)
        End If

        While true
            MQTTService()
        End While
    Else
        USART.Write("MQTT: Unable to set connection", #13,#10)
    End If
End If
=]
Changed line 18 from:
However, nothing prevents you from using other development systems since this protocol is well established.
to:
However, nothing prevents you from using other development systems since this protocol is well established and there are a lot of libraries/sources for Android and iOS.
Added lines 21-24:

In this protocol is defined "Topic" a message queue and one of the most important features is the ability to define a Topic (named WillTopic ) to send a message (named WillMessage) to other connected clients that occurred a single client disconnection. If we add the fact that everyone can have a broker in your own home and can make visible the internet service by opening a single port of a router .. hello internet of things.

Clearly not going to go into too much detail of MQTT but would definitely recommend to take a ride on the Internet to gather information and in a week or so you'll be more than satisfied.
Added lines 1-20:
The best system to interchange data between different systems remains MQTT and there are about libraries for all operating systems and for all development systems.
MQ Telemetry Transport (MQTT) is a lightweight broker-based publish/subscribe messaging protocol designed to be open, simple, lightweight and easy to implement.

These characteristics make it ideal for use in constrained environments, for example, but not limited to where the network is expensive, has low bandwidth or is unreliable or when run on an embedded device with limited processor or memory resources.

I wanted to try my hand from here in the thing and I started with good examples on the net and converting the various sources to get a library working with the protocol 3.1
This protocol has only a few restrictions in the use of wildcard characters but lends itself to interchange messages string between systems without great waste of resources and every system support it.

Features of the protocol include the publish/subscribe message pattern to provide one-to-many message distribution and decoupling of applications, a messaging transport that is agnostic to the content of the payload and the use of pure TCP/IP to provide basic network connectivity.
Three qualities of service for message delivery ar supported:
* "At most once", where messages are delivered according to the best efforts of the underlying TCP/IP network. Message loss or duplication can occur. This level could be used, for example, with ambient sensor data where it does not matter if an individual reading is lost as the next one will be published soon after.
* "At least once", where messages are assured to arrive but duplicates may occur.
* "Exactly once", where message are assured to arrive exactly once. This level could be used, for example, with billing systems where duplicate or lost messages could lead to incorrect charges being applied.

I started from the [[http://www.sfcompiler.co.uk/wiki/pmwiki.php?n=SwordfishUser.W5500 | library for Wiz550io]] that mounts the W5500 and I created an additional module called MQTT.BAS.

For the debugging I used two programs written in VB.NET that reference the open source library M2Mqtt which among other things allows to get executables for Windows / Linux (Mono) and Windows Phone (7.8 and 8.1) a boon.
However, nothing prevents you from using other development systems since this protocol is well established.

To use MQTT need a server named "Broker" and even in this case there are open source libraries for all operating systems and I used mosquitto for windows that provides two of his application to send new and receive messages from the command line. Mosquitto also provides a service broker on line for any test but after a couple of weeks has banned my IP address.