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

Reply
  • 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

Children
No Data
Related