MQTT

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 library for Wiz550io that mounts the W5500 and I created an additional module called MQTT.BAS.

You can download Swordfish library modules here: MQTT Library

This library is compatible with this hardware also.

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 and there are a lot of libraries/sources for Android and iOS.

You can download vb.net windows client examples here: VB.NET test client

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.

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.

Simple MQTT reader test program

{
*****************************************************************************
*  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  

#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     = (85,119,83,194),     ' <-- MOSQUITTO BROKER
    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

Simple MQTT sender test program

{
*****************************************************************************
*  Name    : MQTT-Sender.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      = 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     = (85,119,83,194),    ' <-- MOSQUITTO BROKER
    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