1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Question Mqtt Client Written In Abl

Discussion in 'Development' started by Cecil, Jul 30, 2017.

Tags:
  1. Cecil

    Cecil 17+ years progress programming and still learning.

    I'm writing an a MQTT client and I'm getting stuck at the first hurdle. The problem I am having is the MQTT broker is disconnecting the SOCKET connection without any returning any data first.

    Here is my code. You should be able to just copy and past the code into your own Progress editor and run it.

    What it should do is perform a socket connection, send 10 bytes of data and expecting some data (should be 4 bytes) to be returned. However the server just disconnects????

    I guessing the problem is in the Header I send to the MQTT broker but I can't see what the problem is.

    Code (progress):
    1.  
    2.  
    3. define variable connectHeader as memptr no-undo.
    4. define variable connectFlags as integer no-undo.
    5. define variable timethen            as datetime.
    6.  
    7. SET-BYTE-ORDER(connectHeader ) = LITTLE-ENDIAN.
    8.  
    9. set-size(connectHeader) = 0.
    10. set-size(connectHeader) = 10. /** connected Header is 10 bytes in size **/
    11.  
    12. /** 3.1.2.1 Protocol Name **/
    13. put-byte(connectHeader, 1) = 0x00.
    14. put-byte(connectHeader, 2) = 0x04.
    15. put-byte(connectHeader, 3) = asc('M':U).
    16. put-byte(connectHeader, 4) = asc('Q':U).
    17. put-byte(connectHeader, 5) = asc('T':U).
    18. put-byte(connectHeader, 6) = asc('T':U).
    19.  
    20. /** 3.1.2.2 Protocol Level **/
    21. put-byte(connectHeader, 7) = 0x04. /** MQTT Version 3.1.1**/
    22.  
    23. /** 3.1.2.3 Connect Flags **/
    24. put-bits(connectFlags, 8, 1) = 0x01.  /** Username Flag **/
    25. put-bits(connectFlags, 7, 1) = 0x01.  /** Password Flag **/
    26. put-bits(connectFlags, 6, 1) = 0x00.  /** Will Retain **/
    27. put-bits(connectFlags, 4, 2) = 0x01.  /** Will QoS, 2 Bits **/
    28. put-bits(connectFlags, 3, 1) = 0x01.  /** Will Flag **/
    29. put-bits(connectFlags, 2, 1) = 0x01.  /** Clean Session **/
    30. put-bits(connectFlags, 1, 1) = 0x00.  /** Reserved **/
    31.  
    32. put-byte(connectHeader, 8) = connectFlags.
    33.  
    34. /** 3.1.2.10 Keep Alive **/
    35. put-byte(connectHeader, 9) = 0x0.
    36. put-byte(connectHeader, 10) = 0x10.
    37.  
    38. /* PUT-SHORT(connectHeader, 9) = 0x0A. /** 16-bit/2 bytes short word of the number of seconds. A=10**/ */
    39.  
    40.  
    41. copy-lob from object connectHeader to file "./connectHeader.txt".
    42.  
    43. define variable mqttClinet as handle.
    44.  
    45. CREATE SOCKET mqttClinet .
    46.  
    47. mqttClinet:SET-SOCKET-OPTION("SO-KEEPALIVE", "true").
    48. mqttClinet:SET-SOCKET-OPTION("SO-RCVTIMEO", "10").
    49.  
    50. mqttClinet:connect("-H broker.hivemq.com -S 1883").
    51.  
    52. if mqttClinet:connected() then
    53. Do:
    54.     message "Connected to MQTT server.."
    55.     mqttClinet:write(connectHeader, 1, get-size(connectHeader))     /** write the connect header to the MQTT server **/
    56.     mqttClinet:bytes-written.
    57. End.
    58. timethen = now.
    59.  
    60. pause 0.5.
    61.  
    62. define variable packetLength as integer no-undo.
    63.  
    64. message
    65.     mqttClinet:connected().
    66.  
    67. WAIT-FOR-SERVER:
    68. do while mqttClinet:connected():
    69.  
    70.     packetLength = mqttClinet:get-bytes-available().
    71.  
    72.     if packetLength  > 0 then
    73.     do:
    74.         message packetLength
    75.             view-as alert-box info.
    76.        
    77.     end.
    78.    
    79.     if absolute( interval(timethen, now, 'seconds') ) ge 10 then
    80.         leave WAIT-FOR-SERVER.
    81.  
    82. end.
    83.  
    84. if not mqttClinet:connected() then
    85.     message "Server Disconnected"
    86.         view-as alert-box error.
    87. else      
    88.     mqttClinet:disconnect().
    89.  
    90. delete object mqttClinet.
    91.  

    MQTT 3.1.1 technical specifications.
    MQTT Version 3.1.1
     
  2.  
  3. Cecil

    Cecil 17+ years progress programming and still learning.

    I think I've missing the payload data for it to work correctly.
     
  4. Cecil

    Cecil 17+ years progress programming and still learning.

    This has become harder than expected. It's combination of interpreting the specifications and cross checking with other languages to get the expected output.
     
  5. Cecil

    Cecil 17+ years progress programming and still learning.

    I developed a proof of concept of a MQTT client written in the ABL. This project is so in it's infancy that it's no way complete. My objective it to publish messages (XML, JSON, Text, binary etc) from the ABL to a MQTT Broker and have Arduino/ESP32/ESP8266 devices subscribe to these messages and process these messages appropriately.

    Future development is to be able to subscribe to 'topics' and receive messages from other MQTT devices via the MQTT broker.

    Source code and be found here.
    GitHub - Jimbobnz/ABL-MQTT: MQTT ABL Client

    MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.
     
    RealHeavyDude likes this.

Share This Page