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