Page 1 of 1

Sending MQTT messages

Posted: Tue Jun 24, 2025 6:52 pm
by Bogdan58
Hi, I'm new to this forum and just starting with PureBasic. I would like to send MQTT topic messages from PureBasic and to do that I have to send data in HEX while the SendNetworkString function sends data as string. So I used a HEX() function to convert the values to string. The problem is that I don't see any packets sent out using Wireshark.

Code: Select all

Port = 1883

ConnectionID = OpenNetworkConnection("127.0.0.1", Port)
If ConnectionID

  ;10 0c 00 04 4d 51 54 54 04 02 00 3c 00 00 hex    ;MQTT connect command
  ;16 12 00 04 77 81 84 84 04 02 00 60 00 00 dec
MQQTMessageString = Hex(16,#PB_Byte)+Hex(12,#PB_Byte)+Hex(00,#PB_Byte)+Hex(04,#PB_Byte)+Hex(77,#PB_Byte)+Hex(81,#PB_Byte)+Hex(84,#PB_Byte)+Hex(84,#PB_Byte)+Hex(04,#PB_Byte)+Hex(02,#PB_Byte)+Hex(00,#PB_Byte)+Hex(60,#PB_Byte)+Hex(00,#PB_Byte)+Hex(00,#PB_Byte)
 
  SendNetworkString(ConnectionID, MQQTMessageString)
  
  CloseNetworkConnection(ConnectionID)
Else
  MessageRequester("PureBasic - Client", "Can't find the server (Is it launched ?).", 0)
EndIf

Re: Sending MQTT messages

Posted: Tue Jun 24, 2025 7:07 pm
by Bogdan58
Correction, I don't see any packets as MQTT protocol in Wireshark. I am seeing just regular packets on port 1883.

Re: Sending MQTT messages

Posted: Tue Jun 24, 2025 7:08 pm
by infratec

Re: Sending MQTT messages

Posted: Tue Jun 24, 2025 7:11 pm
by infratec
Btw. MQTT does not use strings as header.
https://www.hivemq.com/blog/mqtt-packet ... ive-guide/

You need SendNetworkData() and a binary buffer

Re: Sending MQTT messages

Posted: Tue Jun 24, 2025 7:18 pm
by HeX0R
Payloads might be strings, though.
I just saw, my client example is pretty rudimentary :mrgreen:
I have to give this an overhaul somewhen.

Re: Sending MQTT messages

Posted: Tue Jun 24, 2025 8:25 pm
by infratec
Example:

Code: Select all

DataSection
  MQTTMsg:
  Data.a 16, 12, 00, 04, 77, 81, 84, 84, 04, 02, 00, 60, 00, 00
  MQTTMsgEnd:
EndDataSection


Port = 1883

ConnectionID = OpenNetworkConnection("127.0.0.1", Port)
If ConnectionID
  SendNetworkData(ConnectionID, ?MQTTMsg, ?MQTTMsgEnd - ?MQTTMsg)
  
  CloseNetworkConnection(ConnectionID)
Else
  MessageRequester("PureBasic - Client", "Can't find the server (Is it launched ?).", 0)
EndIf

Re: Sending MQTT messages

Posted: Tue Jun 24, 2025 8:49 pm
by HeX0R
:lol:
I meant more something like this:
https://hex0rs.coderbu.de/cgi-bin/hv.cg ... 33&ia=2215

Image

Re: Sending MQTT messages

Posted: Wed Jun 25, 2025 2:00 pm
by skinkairewalker
HeX0R wrote: Tue Jun 24, 2025 8:49 pm :lol:
I meant more something like this:
https://hex0rs.coderbu.de/cgi-bin/hv.cg ... 33&ia=2215

Image
awesome