0

I am trying to implement an MQTT subscription client in a microcontroller using the eclipse paho mqttPacket library.

The mcu resets in the middle while receiving subscribed topics.

I am running a infinite loop delayed by 1 sec for publishing using python script, and for subscription I could get only 15 or 17 messages and the mcu resets.

I am using atmega328p. The mcu code is below

while(1){ 
    rc = transport_getdata(buf);
    if(rc == 0){ } 
    else if(rc < 0){
        softuart_puts("closed");
        goto stop;
    }
    else{
        flen=buf[1]-48;
        paylen=buf[3]-48;
        for(i=4;i<14;i++){
            softuart_putchar(buf[i]);
        }
        for(i=14;i<rc;i++){
            softuart_putchar(buf[i]);
        }
    }
    _delay_ms(1000);
}
stop: while(1);

The python script I am using to publish

publish.single(topic="test/relay", payload="sheep", qos=0, retain=False, hostname="128.199.179.240", port=1883, client_id="", keepalive=60, will=None, tls=None, auth={'username': 'XXXXX', 'password': 'XXXXX'}, protocol=mqtt.MQTTv311)
hardillb
  • 54,545
  • 11
  • 67
  • 105
jagadeesh
  • 11
  • 1
  • 5
  • 2
    Not going to get anywhere without... 1) Which micro controller are you using? 2) Some view of the code your running 3) some description of what sort of messages your sending e.g. size 4) how many topics your subscribed to. In fact we can't even guess what might be the problem with the amount of information you've provided so far – hardillb Jun 26 '16 at 19:55
  • I have edited the question. Will it help – jagadeesh Jun 29 '16 at 14:57

1 Answers1

0

hardlib thank you for your quick responses. The problem is with keepalive time, i increased it and now i could receive at messages. Now the mcu resets or hangs when i do subscription and publish at the same time. I forgot to mention i am using a gprs modem for tcp/ip communication.

while(1){
    rc = transport_getdata(buf);
    if((rc > 0)&&(buf[0]==0x30)){
        flen=buf[1]-48;
        paylen=buf[3]-48;
        for(i=4;i<14;i++){
            softuart_putchar(buf[i]);
        }
        for(i=14;i<rc;i++){
            softuart_putchar(buf[i]);
        }
    }
    else if(rc < 0){
        softuart_puts("closed");
        goto stop;
    }
    len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, (unsigned char*)payload, payloadlen);
    send_tcp(buf,len);
    transport_getdata(buf);
    _delay_ms(1000);
}
jagadeesh
  • 11
  • 1
  • 5