This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf 9160 Serial MQTT

Hello All,

I am trying to send data on serial port of nrf9160 to be published on a topic using MQTT protocol. So far using the mqtt example and UART example, I have successfully run both code independently not with each other. I am not sure how to integrate the UART received buffer in the publish function so that I could send the data to the broker on that specific topic. Can some please help me out with this. I would really appreciate it.

 The following are the modifications which I have added to the main code: 1. function to respond on serial event 

static u8_t uart_buf[1024];
int i=0;
static u8_t inData[20];
void uart_cb(struct device *x)
{
	uart_irq_update(x);
	int data_length = 0;

	if (uart_irq_rx_ready(x)) {
		data_length = uart_fifo_read(x, uart_buf, sizeof(uart_buf));
		uart_buf[data_length] = 0;
	}

        if(i==19 || *uart_buf == '?'){ 
        inData[i] = '\0';
        printk("%s\n",inData);
        i=0;
        }
        else{
        memcpy(&inData[i], uart_buf, 1); 
        i++;
        }
}
 

2. In the main function I have added this 

struct device *uart = device_get_binding("UART_0");

	uart_irq_callback_set(uart, uart_cb);
	uart_irq_rx_enable(uart);
	printk("UART loopback start!\n");

4. The following code has modifications. It takes data from serial port "uart_buf" and then publish it. I have made minor changes to length and some changes to if statement so that it publishes directly 

case MQTT_EVT_PUBLISH: {
		const struct mqtt_publish_param *p = &evt->param.publish;

		printk("[%s:%d] MQTT PUBLISH result=%d len=%d\n", __func__,
		       __LINE__, evt->result, p->message.payload.len);
		err = publish_get_payload(c, p->message.payload.len);
		if (err >= 0) { // just for testing
			data_print("Received: ", uart_buf,
				strlen(uart_buf));
			/* Echo back received data */
			data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE,
				uart_buf, strlen(uart_buf));
		} else {
			printk("mqtt_read_publish_payload: Failed! %d\n", err);
			printk("Disconnecting MQTT client...\n");

			err = mqtt_disconnect(c);
			if (err) {
				printk("Could not disconnect: %d\n", err);
			}
		}
	} break;
It doesn't seem to work. Can some please help me out with the same 

  • Hi Simon,

    The example seems great, but its not working on my board. I get some data on serial port which is the loopback but it doesn't seem to publish. I seems the configuration for the topic is same as before with some little changes on yours. I double checked the publish, subscribe topic with port and server name, but still doesn't seem to work. Could you please help me out with.Also feel free to make the topic public.

    Thanks a lot again I really appreciate your help and follow up. 

    Best regards

    Nordic User

  • The sample works in the following manner

    • Several work items are initialized:

    for(int i=0; i < SERIAL_Q_SIZE; i++){
        k_work_init(&w_data[i].work, pub_uart_mqtt);
    }

    • Serial data is sent from the computer to nRF9160 through a micro USB and uart_cb() gets called
    • In uart_cb() the incoming serial data is processed and put into the queue (at the top of main.c I have created an array of static struct work_data of size 5)
    • Each incoming element (separated by 10 (=LF)) is submitted to the work queue through k_work_submit()
    • When the work queue Thread gets scheduled to run, it will call pub_uart_mqtt() for each element, where the elements will be published to the topic CONFIG_MQTT_PUB_TOPIC through data_publish()

    Try following these steps:

    CONFIG_MQTT_PUB_TOPIC="/my/publish/topic"
    CONFIG_MQTT_SUB_TOPIC="/my/subscribe/topic"
    CONFIG_MQTT_CLIENT_ID="my-client-id"
    CONFIG_MQTT_BROKER_HOSTNAME="mqtt.eclipse.org"
    CONFIG_MQTT_BROKER_PORT=1883

    • Build and run the sample
      • west build -b nrf9160_pca10090ns
      • nrfjprog --eraseall
      • west flash
    • Subscribe to the topic /my/publish/topic -v from the mosquitto broker, by typing the following in a command line (cmd, git bash etc..)
      • mosquitto_sub -h mqtt.eclipse.org -t /my/publish/topic -v
    • Open Tera Term with the following setup:

    • Then, in Tera Term, type in e.g. "Hey", followed by enter and you should see the following response in the command line where mosquitto_sub -h were called

    C:\Users\Simon>mosquitto_sub -h mqtt.eclipse.org -t /my/publish/topic -v
    /my/publish/topic Hey

  • I did the exact the same steps but it doesn't seem to to show up on my terminal

    I did press enter after typing

     If its not a problem could we have a zoom conference where I could share my screen with you and you could help me out with this. 

  • It seems like you haven't configured Tera Term correctly, do the following:

     '

    This will generate a 13 (CR) + 10 (LF)  when enter is pressed, and the message will get published.

    Another way of solving it is to edit the code to look for both 13 (CR) and 10 (LF):

    //Check if last char is 'LF' or 'CR'
                    if(uart_buf[data_length-1] == 10 || uart_buf[data_length-1] == 13){
                          printk("last char is 'LF', call k_work_submit(), word number %d \n", serial_in_count);
                          w_data[serial_in_count].data_len = tot_len - 1;
                          k_work_submit(&w_data[serial_in_count].work);
    

    Best regards,

    Simon

  • tried to do some debugging and went to the first example through which I wanted to check whether my sim has some data or not, Actually it had. It has now more than 10MB of data. Then I tried the following

    So I just noticed that when you make changes to the topic in prj.conf or prj_quemo_x86 files it doesn't reflect to the change. I tried to publish and subscribe on the new chnaged topic name and it doesn't work. It works on the old topic. To confirm that, I did a clean build and restarted the segger studio also and downloaded the new firmware to nrf9160 but still seems to use the old topic. That might be one of the problems too. 

Related