SIM900A MQTT connection problem

I am trying to send data using MQTT in my GSM900A module.
when i am running the code then after executing connect packet function, In publish function, its showing error when i am publishing data and sometime showing connection is closed. I also wanted confirm in the code that you have posted which variable you are using for sending data.
I am sending json data over the server.
please help me how to fix it. I am trying from two weeks.

If your connection is getting closed, it can be due to server timeout where you didnt send the data fast enough after connection opened,
Your connect packet is malformed and not in the right format. You have to verify the data you are sending. Follow the format that your broker supports. To know the data format, you can use wire shark and use softwares like MQTTFx to send data from PC and snoop the bytes going to broker and compare with the bytes you are sending via SIM900A

my data format is correct

Have you done remaining length calculation properly. If its more than 127, there is a different calculation for it.


i am sending this data, and also can you tell me the variable of your code in which you sending data, i am not sure in which variable you sensding.

Data is almost near 127 bytes. I suspect you are exceeding 127. Try sending shorter strings. Like HELLO and see. I dont remember the code i wrote. Its too long. paste the snippet you are question about.