Yes i tried with second part also but no success. I have modified the code and its working but only for connect packet is getting sent after that i am sending publish but no msg appears in broker. Though sending connect+publish packet i get msg in broker. In short single packet is getting sent. I have to send 500kb + data in future but i am stuuck here. I changed modem mode from binary to HEX as non printing ASCII characters were not getting sent in earlier mode.
modified code:
void writeChar(unsigned char** pptr, char c)
{
**pptr = c;
(*pptr)++;
}
void writeInt(unsigned char** pptr, int anInt)
{
**pptr = (unsigned char)(anInt / 256);
(*pptr)++;
**pptr = (unsigned char)(anInt % 256);
(*pptr)++;
}
void writeCString(unsigned char** pptr, const char* string)
{
int len = strlen(string);
writeInt(pptr, len);
memcpy(*pptr, string, len);
*pptr += len;
}
void binary_to_hex(const char *input_buff, unsigned int length, char *output_buff)
{
char hex[] = “0123456789ABCDEF”;
*output_buff = '\0';
for (; length > 0; --length)
{
unsigned char byte = *input_buff++;
*output_buff++ = hex[(byte >> 4) & 0x0F];
*output_buff++ = hex[byte & 0x0F];
}
*output_buff++ = '\0';
}
int MQTTConnect(void)
{
uint8_t status = 0;
uint8_t temp_str[250];
uint8_t *mqtt_str, *buff;
memset(temp_str, 0, 250);
mqtt_str = malloc(250);
buff = mqtt_str;
writeChar(&buff, 0x10);
MQTTProtocolNameLength = strlen(MQTTProtocolName);
MQTTClientIDLength = strlen(MQTTClientID);
MQTTUsernameLength = strlen(MQTTUsername);
MQTTPasswordLength = strlen(MQTTPassword);
datalength = MQTTProtocolNameLength+2+4+MQTTClientIDLength+2+MQTTUsernameLength+2+MQTTPasswordLength+2;
X=datalength;
do
{
encodedByte = X%128;
X = X/128;
// if there are more data to encode, set the top bit of this byte
if ( X > 0 ) { encodedByte |= 128; }
writeChar(&buff, encodedByte);
} while ( X > 0 );
writeCString(&buff, MQTTProtocolName);
writeChar(&buff, MQTTLVL);
writeChar(&buff, MQTTFlags);
writeInt(&buff, MQTTKeepAlive);
writeCString(&buff, MQTTClientID);
writeCString(&buff, MQTTUsername);
writeCString(&buff, MQTTPassword);
writeChar(&buff, 0x1A);
bin_to_hex(mqtt_str, encodedByte+3, temp_str);
sprintf(mqtt_str, "AT+USOWR=0,38,\"%s\"\r",temp_str);
status = sendATcommand2(mqtt_str, strlen(mqtt_str), "OK", "ERROR", 5000);
if(status == 1)
{
printf(“PACKET SEND SUCCESS”);
status = 1;
}
else if(status == 2)
{
printf(“PACKET SEND ERROR”);
status = 0;
}
else
{
printf(“PACKET SEND TIMEOUT”);
status = 0;
}
free(mqtt_str);
return status;
}
MODEM i am using is ublox LARA R280 4G LTE. I may be missing any configuration to make it work. I would really appreciate if you can look into this modem configuration once.