Sending MQTT messages

Just starting out? Need help? Post your questions and find answers here.
Bogdan58
New User
New User
Posts: 2
Joined: Tue Jun 24, 2025 6:40 pm

Sending MQTT messages

Post 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
Bogdan58
New User
New User
Posts: 2
Joined: Tue Jun 24, 2025 6:40 pm

Re: Sending MQTT messages

Post by Bogdan58 »

Correction, I don't see any packets as MQTT protocol in Wireshark. I am seeing just regular packets on port 1883.
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sending MQTT messages

Post by infratec »

infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sending MQTT messages

Post 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
Last edited by infratec on Tue Jun 24, 2025 7:23 pm, edited 1 time in total.
User avatar
HeX0R
Addict
Addict
Posts: 1187
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Sending MQTT messages

Post 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.
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Sending MQTT messages

Post 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
User avatar
HeX0R
Addict
Addict
Posts: 1187
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Sending MQTT messages

Post by HeX0R »

:lol:
I meant more something like this:
https://hex0rs.coderbu.de/cgi-bin/hv.cg ... 33&ia=2215

Image
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 772
Joined: Fri Dec 04, 2015 9:26 pm

Re: Sending MQTT messages

Post 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
Post Reply