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 

Parents
  • 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. 

  • Every time you change prj.conf you have to rebuild your project. If using SEGGER, you have to open it again (File-->Open nRF Connect SDK Project) for the changes in prj.conf to take effect. In west I think it is enough to rebuild your project (west build -b nrf9160_pca10090ns).

    You can also change it using menuconfig, but then it will get deleted when you rebuild your project, and I would not recommend doing this for permanent changes (only temporary changes for testing).

    Best regards,

    Simon

Reply
  • Every time you change prj.conf you have to rebuild your project. If using SEGGER, you have to open it again (File-->Open nRF Connect SDK Project) for the changes in prj.conf to take effect. In west I think it is enough to rebuild your project (west build -b nrf9160_pca10090ns).

    You can also change it using menuconfig, but then it will get deleted when you rebuild your project, and I would not recommend doing this for permanent changes (only temporary changes for testing).

    Best regards,

    Simon

Children
No Data
Related