7 years, 5 months ago.

Serial Interrupt not working

I am working on stm32 discovery board and have written this code to connect to a mqtt cloud. I am able to log the messages published through a mqtt client to the serial terminal but for publishing the message entered through the serial terminal to the mqtt cloud, I want to use serial receive interrupt and it's not working out for me. Whenever I send any message through the terminal, it doesn't get published. Here is my code-

#include "easy-connect.h"
#include "MQTTNetwork.h"
#include "MQTTmbed.h"
#include "MQTTClient.h"

int arrivedcount = 0,rc;
char buffer[50];

MQTT::Message message;
 NetworkInterface* ipstack = easy_connect(true); 
 
MQTTNetwork mqttnetwork(ipstack);
MQTT::Client<MQTTNetwork, Countdown> client = MQTT::Client<MQTTNetwork, Countdown>(mqttnetwork);

int received=0;
Serial pc(USBTX,USBRX);
void messageArrived(MQTT::MessageData& md)
{
    MQTT::Message &message = md.message;
    pc.printf("Message arrived: qos %d, retained %d, dup %d, packetid %d \n", message.qos, message.retained, message.dup, message.id);
    pc.printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
   
}

void serialRx(void)
{
    while(pc.readable()) {              
    char c=pc.getc();               
     buffer[received++]=c;                 
    }
   
    message.qos = MQTT::QOS1; 
     message.retained = false;
    message.dup = false;
    message.payload = (void*)buffer;
   message.payloadlen = strlen(buffer)+1;
    rc = client.publish("testout", message);
    received=0;
    }


int main(int argc, char* argv[])
{
    float version = 0.6;
   
  pc.attach(&serialRx,pc.RxIrq);
    pc.printf("HelloMQTT: version is %.2f \n", version);
    char* hostname = "m12.cloudmqtt.com";
    int port =16479;
    pc.printf("Connecting to %s:%d\r\n", hostname, port);
    int rc = mqttnetwork.connect(hostname, port);
    if (rc != 0)
        pc.printf("rc from TCP connect is %d \n", rc);

    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    data.MQTTVersion = 3;
    data.clientID.cstring = "1";
    data.username.cstring = "sxdzesyk";
    data.password.cstring = "dc_pY7Q7gOTw";
    rc = client.connect(data);
    pc.printf("rc from MQTT connect is %d \n", rc);

  rc = client.subscribe("testing", MQTT::QOS1, messageArrived);
  pc.printf("rc from MQTT subscribe is %d \n", rc);

  while(1)
{
client.yield(100);
}
}

1 Answer

7 years, 5 months ago.

Could you try this simple attach example so we can rule out any unintended problems with the other (non serial related) code?

I tried running this example code but it still doesn't get into the callback function whenever I send a character from the terminal.

posted by Rahul Gusai 23 Jun 2017

I'm watching this with interest as I see the same behaviour using the max32630fthr. Have you successfully used serial interrupts in the past on this platform? I'm trying to eliminate mbed code updates as a possible issue.

posted by Terry Koziniec 24 Jun 2017

No, this is the first time am using serial interrupts on this platform and it's not working out for me.Can anyone please help me out ?

posted by Rahul Gusai 24 Jun 2017

Can you please confirm that you are using this - https://developer.mbed.org/platforms/ST-Discovery-F429ZI/? And, specifically, the STM32F429I-DISCO? Please see the large yellow box beneath the overview. This box says that there is no printf capabilities for STM32F429I-DISCO. This means that there is no possibility of serial communication between your PC and this board.

posted by Sarah Marsh 28 Jun 2017