Cant send the connect packet data to the broker.

116 views
Skip to first unread message

user25677

unread,
Jun 24, 2015, 5:17:33 AM6/24/15
to mq...@googlegroups.com
Hi guys,
             Im writing a C program to connect the mosquitto broker via GPRS/GSM Modem. I almost written a code to connect the mosquitto broker. I think we have to format the correct packet to connect with broker. I formed the packet format, but can't send the data to the broker. I've installed wireshark protocol analyser in broker machine. After run this code wireshark shows something happend from my modem to broker, but packet length and "MQIsdp" data not showing in the wireshark protocol analyser. Whats wrong in the code?  Below find the code and also i've attached the serial.h header file

Thanks


#include <stdio.h>
#include <time.h>
#include "serial.h"
#include "connpack.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#define A "AT\r"
#define B "AT+CPIN?\r"
#define C "AT+CREG?\r"
#define D "AT+CGATT?\r"
#define E "AT+CIPSHUT\r"
#define X "AT+CIPSTATUS\r"
#define G "AT+CIPMUX=0\r"
#define H "AT+CSTT=\"xxxx\",\"xxxx\",\"xxxx\"\r"
#define I "AT+CIICR\r"
#define J "AT+CIFSR\r"
#define K "AT+CIPSTART=\"TCP\",\"xxx.xxx.xxx.xxx\",\"xxxx\"\r"
#define L "AT+CIPSEND\r"
char* ATC[]={A,B,C,D,E,X,G,H,I,J,K,L};
#define MQTTCONNECT 1<<4
#define MQTTPUBLISH 3<<4
#define KEEPALIVE 15000
int main()
{
char *id="CONMQTT";

int i,j;
port_open();
uint16_t buffer[255];
uint16_t buf[255];

for(i=0;i<=11;i++)
{
port_write(ATC[i]);
sleep(5);
printf("%s\n",port_read());
if(i==11)
{
char *id="MQTT";
   uint8_t var_header[] = {0x00,0x06, 0x4d, 0x51, 0x49, 0x73, 0x64, 0x70, 0x03, 0x02, 0x00, KEEPALIVE/500, 0x00, strlen(id)};
   uint8_t fixed_header[] = {MQTTCONNECT, 12+strlen(id)+2};
   char packet[sizeof(fixed_header)+sizeof(var_header)+strlen(id) + 2];
   char connectdatalen=2+12+2+strlen(id);
    uint8_t packetlen = 2 + connectdatalen;
   memset(packet, 0, sizeof(packet));
   memcpy(packet, fixed_header, sizeof(fixed_header));
   memcpy(packet+sizeof(fixed_header), var_header, sizeof(var_header));
   memcpy(packet+sizeof(fixed_header)+sizeof(var_header),id,strlen(id));
 

       for(i=0;i<=packetlen;i++)
       {
        //putchar(packet[i]);
        sprintf(buf, "%c\x1A", packet[i]);
        port_write(buf);
       

       }
}
}
return 0;
}



serial.h

Roger Light

unread,
Jun 24, 2015, 6:07:57 AM6/24/15
to mq...@googlegroups.com
Hi Mahesh,

This mailing list is for the discussion of the protocol, not
programming problems.

The answer to your problem is that your port_write() function uses
strlen() to determine the length of data to write, but your binary
data contains a 0, so the write will be truncated at that point. You
need to pass a length parameter to port_write() so it knows how much
data to write. Don't use strlen.

This is what I was referring to in my comments on your stackoverflow question:

http://stackoverflow.com/questions/30979182/mosquitto-broker-is-receving-data-partially/

Cheers,

Roger
> --
> To learn more about MQTT please visit http://mqtt.org
> ---
> You received this message because you are subscribed to the Google Groups
> "MQTT" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mqtt+uns...@googlegroups.com.
> To post to this group, send email to mq...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mqtt.
> For more options, visit https://groups.google.com/d/optout.

user25677

unread,
Jun 24, 2015, 9:57:25 PM6/24/15
to mq...@googlegroups.com, ro...@atchoo.org
Hi Roger Light, 
                          I've sucessfully send it in Arduino. Have to read the C little deeper. Hope i will get the solution for it. Thanks Light. Learn useful info from you.  
Reply all
Reply to author
Forward
0 new messages