Hi Support team,
I am using nRF9160 module and i want run two server on the module, one is modbus server and other is CoAP server. so is it possible to run both server simultaneously ?
Thanks,
Dvl
Hi Support team,
I am using nRF9160 module and i want run two server on the module, one is modbus server and other is CoAP server. so is it possible to run both server simultaneously ?
Thanks,
Dvl
Hi Dejan Simonovic,
to make you batter understanding i have attaching complete project zip and debug log file, it very simple project that i am trying to implement, if your are engineer then you might able to understand the flow of code from main.c/main() function.
for compilation i am usinng the SDK version 2.4.0
1. Here is the debug log file
*** Booting nRF Connect SDK v3.5.99-ncs1 *** [00:00:00.333,648] <inf> main: [142]main [00:00:02.605,895] <inf> main: RSP : +CEREG: 0,4 OK [00:00:03.614,898] <inf> main: RSP : +CEREG: 0,2,"2BDC","0340830A",7 OK [00:00:04.623,931] <inf> main: RSP : +CEREG: 0,2,"2BDC","0340830A",7 OK [00:00:05.624,572] <inf> main: RSP : +CEREG: 0,2,"2BDC","0340830A",7 OK [00:00:06.625,427] <inf> main: RSP : +CEREG: 0,2,"2BDC","0340830A",7 OK [00:00:07.626,007] <inf> main: RSP : +CEREG: 0,1,"2BDC","0340830A",7 OK [00:00:07.626,647] <inf> CoAP_APP: coap_service_start : 0 [00:00:08.626,708] <inf> CoAP_APP: coap_service_is_running : 1 [00:00:09.626,922] <inf> main: [67]StartTCPServer [00:00:09.626,953] <inf> main: Project : CoAPAndServer [00:00:09.627,349] <inf> main: [76]StartTCPServer >> socket >> serv >> 3 [00:00:09.627,685] <inf> main: Started TCP server example on port 502, 0 [00:00:09.627,716] <err> main: error: accept: 23, -1 [00:00:09.827,850] <err> main: error: accept: 23, -1 [00:00:10.027,984] <err> main: error: accept: 23, -1 [00:00:10.228,088] <err> main: error: accept: 23, -1 [00:00:10.428,192] <err> main: error: accept: 23, -1 [00:00:10.628,295] <err> main: error: accept: 23, -1 [00:00:10.828,369] <err> main: error: accept: 23, -1 [00:00:11.028,503] <err> main: error: accept: 23, -1 [00:00:11.228,637] <err> main: error: accept: 23, -1 [00:00:11.428,741] <err> main: error: accept: 23, -1 [00:00:11.628,845] <err> main: error: accept: 23, -1 [00:00:11.828,948] <err> main: error: accept: 23, -1 [00:00:12.029,022] <err> main: error: accept: 23, -1 [00:00:12.229,156] <err> main: error: accept: 23, -1 [00:00:12.429,290] <err> main: error: accept: 23, -1 [00:00:12.629,394] <err> main: error: accept: 23, -1 [00:00:12.829,498] <err> main: error: accept: 23, -1 [00:00:13.029,602] <err> main: error: accept: 23, -1 [00:00:13.229,675] <err> main: error: accept: 23, -1
2. main.c file
/* * Copyright (c) 2019 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #include <stdio.h> #include <zephyr/kernel.h> #include <zephyr/device.h> #include <zephyr/logging/log.h> #include <modem/nrf_modem_lib.h> #include <nrf_modem_at.h> #include <zephyr/net/socket.h> #include "CoAP/CoAP.h" LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); char response[512]; uint8_t RecvBuff[512]; // TCP Server Implementation ---------- static int SeverRcevHandler(int client) { int rc; int errno; struct pollfd fd = { .fd = client, .events = POLLIN }; rc = poll(&fd, 1, MSEC_PER_SEC * 10); if (rc < 0) { LOG_WRN("poll() error: %d", -errno); return -errno; } else if (rc == 0) { LOG_WRN("poll() timeout"); return -EAGAIN; } rc = recv(client, RecvBuff, sizeof(RecvBuff), 0); if (rc <= 0) { return rc == 0 ? -ENOTCONN : -errno; } LOG_HEXDUMP_DBG(RecvBuff, sizeof(RecvBuff), "RecvBuff:>"); if (send(client, RecvBuff, strlen(RecvBuff), 0) < 0) { return -errno; } return errno; } int StartTCPServer(void) { int serv; struct sockaddr_in bind_addr; static int counter; int err = 0; char *Server_Addr_str = "70.25.147.111"; uint32_t Server_Addr_u32 = 0; uint16_t Server_port = 502; char *next = 0; LOG_INF("[%d]%s", __LINE__, __func__); serv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (serv < 0) { LOG_ERR("error: socket: %d", errno); return 0; } LOG_INF("[%d]%s >> socket >> serv >> %d", __LINE__, __func__, serv); Server_Addr_u32 = strtol(Server_Addr_str, &next, 10); Server_Addr_u32 = Server_Addr_u32 << 8; Server_Addr_u32 |= strtol(&next[1], &next, 10); Server_Addr_u32 = Server_Addr_u32 << 8; Server_Addr_u32 |= strtol(&next[1], &next, 10); Server_Addr_u32 = Server_Addr_u32 << 8; Server_Addr_u32 |= strtol(&next[1], &next, 10); bind_addr.sin_family = AF_INET; bind_addr.sin_addr.s_addr = htonl(Server_Addr_u32);// htonl(INADDR_ANY); // INADDR_ANY bind_addr.sin_port = htons(Server_port); if (bind(serv, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) { LOG_ERR("error: bind: %d", errno); return 0; } if (listen(serv, 5) < 0) { LOG_ERR("error: listen: %d", errno); return 0; } errno = 0; LOG_INF("Started TCP server example on port %d, %d", Server_port, errno); LOG_DBG("Entering infinite while Loop %d", __LINE__); while (1) { struct sockaddr_in client_addr; socklen_t client_addr_len = sizeof(client_addr); char addr_str[INET_ADDRSTRLEN]; int client; int rc; client = accept(serv, (struct sockaddr *)&client_addr, &client_addr_len); if (client < 0) { LOG_ERR("error: accept: %d, %d", errno, client); k_msleep(200); continue; } inet_ntop(client_addr.sin_family, &client_addr.sin_addr, addr_str, sizeof(addr_str)); LOG_INF("Connection #%d from %s", counter++, addr_str); do { rc = SeverRcevHandler(client); k_msleep(1); } while (!rc); close(client); LOG_INF("Connection from %s closed, errno %d", addr_str, rc); } return 0; } void main(void) { int err = 0; LOG_INF("[%d]%s", __LINE__, __func__); LOG_INF("Project : CoAPAndServer"); err = nrf_modem_lib_init(); if (err < 0) { LOG_ERR("Modem library init failed, err: %d", err); return; } k_msleep(1000); err = nrf_modem_at_cmd(response, sizeof(response), "AT+CFUN=1"); if (err) // This condition will true, When there is an error occured { LOG_ERR("CFUN Failed %d", err); return; } do // Check Network in do..while(); { k_sleep(K_SECONDS(1)); // Delay between two iteration err = nrf_modem_at_cmd(response, sizeof(response), "AT+CEREG?"); if (err) // true, when error { LOG_ERR("ERR : %d, %s", err, response); } LOG_INF("RSP : %s", response); } while(!strstr(response, "+CEREG: 0,1")); CoAPSetup(); k_msleep(1000); StartTCPServer(); while(1) { // RTC_Read(); // RTC Read test // SHT40_Test(); // SHT40, NOTE : check mutex lock // vI2cScan(); // NOTE : check mutex lock // char DisplayValue[20] = {0}; // snprintf(DisplayValue, 20, "%.1f C\n%.1f RH\n%.1f H", fReadTemprature(), fReadHumidity(), fReadHumidex()); // DisplayString(DisplayValue); // LOG_INF("%s", DisplayValue); k_msleep(5000); } } /* [ Thread Tree ] Start_SysInit_Thread() --> Start_Network_Check_Thread() // Create new thread and distory self Start_Network_Check_Thread() --> Start_Get_APN_Thread((uint32_t)1000); // Create new thread and distory self Start_Get_APN_Thread((uint32_t)1000); --> Start_ModbusServer() // Create new thread and distory self Start_ModbusServer() // This one runs forever */
3. Coap server related source file
/* * @file : CoAP.c */ #include <zephyr/kernel.h> #include <zephyr/logging/log.h> #include <zephyr/net/coap_service.h> #include "CoAP.h" int tcp_socket = 0; LOG_MODULE_REGISTER(CoAP_APP, LOG_LEVEL_INF); static const uint16_t my_service_port = 5683; COAP_SERVICE_DEFINE(my_service, "70.25.147.111", &my_service_port, COAP_SERVICE_AUTOSTART); static int my_get(struct coap_resource *resource, struct coap_packet *request, struct sockaddr *addr, socklen_t addr_len) { static const char *msg = "Hello, This is CoAP Server !!"; uint8_t data[CONFIG_COAP_SERVER_MESSAGE_SIZE]; struct coap_packet response; uint16_t id; uint8_t token[COAP_TOKEN_MAX_LEN]; uint8_t tkl, type; LOG_INF("my_get from COAP"); type = coap_header_get_type(request); id = coap_header_get_id(request); tkl = coap_header_get_token(request, token); /* Determine response type */ type = (type == COAP_TYPE_CON) ? COAP_TYPE_ACK : COAP_TYPE_NON_CON; coap_packet_init(&response, data, sizeof(data), COAP_VERSION_1, type, tkl, token, COAP_RESPONSE_CODE_CONTENT, id); /* Set content format */ coap_append_option_int(&response, COAP_OPTION_CONTENT_FORMAT, COAP_CONTENT_FORMAT_TEXT_PLAIN); /* Append payload */ coap_packet_append_payload_marker(&response); coap_packet_append_payload(&response, (uint8_t *)msg, strlen(msg)); /* Send to response back to the client */ return coap_resource_send(resource, &response, addr, addr_len, NULL); } static int my_put(struct coap_resource *resource, struct coap_packet *request, struct sockaddr *addr, socklen_t addr_len) { /* ... Handle the incoming request ... */ /* Return a CoAP response code as a shortcut for an empty ACK message */ return COAP_RESPONSE_CODE_CHANGED; } static const char * const my_resource_path[] = { "test", NULL }; COAP_RESOURCE_DEFINE(my_resource, my_service, { .path = my_resource_path, .get = my_get, .put = my_put, }); void CoAPSetup(void) { LOG_INF("coap_service_start : %d", coap_service_start(&my_service)); k_msleep(1000); LOG_INF("coap_service_is_running : %d", coap_service_is_running(&my_service)); }
4. Complete Project, you might able to compile this project using nRF SDK 2.6.0
I hope you might get idea from this information to understand the issue, and if you are not an engineer then please show this information to engineer, he might solve this or will give clear information of this issue.
Best Regard,
Dhaval
Hi Dejan Simonovic,
to make you batter understanding i have attaching complete project zip and debug log file, it very simple project that i am trying to implement, if your are engineer then you might able to understand the flow of code from main.c/main() function.
for compilation i am usinng the SDK version 2.4.0
1. Here is the debug log file
*** Booting nRF Connect SDK v3.5.99-ncs1 *** [00:00:00.333,648] <inf> main: [142]main [00:00:02.605,895] <inf> main: RSP : +CEREG: 0,4 OK [00:00:03.614,898] <inf> main: RSP : +CEREG: 0,2,"2BDC","0340830A",7 OK [00:00:04.623,931] <inf> main: RSP : +CEREG: 0,2,"2BDC","0340830A",7 OK [00:00:05.624,572] <inf> main: RSP : +CEREG: 0,2,"2BDC","0340830A",7 OK [00:00:06.625,427] <inf> main: RSP : +CEREG: 0,2,"2BDC","0340830A",7 OK [00:00:07.626,007] <inf> main: RSP : +CEREG: 0,1,"2BDC","0340830A",7 OK [00:00:07.626,647] <inf> CoAP_APP: coap_service_start : 0 [00:00:08.626,708] <inf> CoAP_APP: coap_service_is_running : 1 [00:00:09.626,922] <inf> main: [67]StartTCPServer [00:00:09.626,953] <inf> main: Project : CoAPAndServer [00:00:09.627,349] <inf> main: [76]StartTCPServer >> socket >> serv >> 3 [00:00:09.627,685] <inf> main: Started TCP server example on port 502, 0 [00:00:09.627,716] <err> main: error: accept: 23, -1 [00:00:09.827,850] <err> main: error: accept: 23, -1 [00:00:10.027,984] <err> main: error: accept: 23, -1 [00:00:10.228,088] <err> main: error: accept: 23, -1 [00:00:10.428,192] <err> main: error: accept: 23, -1 [00:00:10.628,295] <err> main: error: accept: 23, -1 [00:00:10.828,369] <err> main: error: accept: 23, -1 [00:00:11.028,503] <err> main: error: accept: 23, -1 [00:00:11.228,637] <err> main: error: accept: 23, -1 [00:00:11.428,741] <err> main: error: accept: 23, -1 [00:00:11.628,845] <err> main: error: accept: 23, -1 [00:00:11.828,948] <err> main: error: accept: 23, -1 [00:00:12.029,022] <err> main: error: accept: 23, -1 [00:00:12.229,156] <err> main: error: accept: 23, -1 [00:00:12.429,290] <err> main: error: accept: 23, -1 [00:00:12.629,394] <err> main: error: accept: 23, -1 [00:00:12.829,498] <err> main: error: accept: 23, -1 [00:00:13.029,602] <err> main: error: accept: 23, -1 [00:00:13.229,675] <err> main: error: accept: 23, -1
2. main.c file
/* * Copyright (c) 2019 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #include <stdio.h> #include <zephyr/kernel.h> #include <zephyr/device.h> #include <zephyr/logging/log.h> #include <modem/nrf_modem_lib.h> #include <nrf_modem_at.h> #include <zephyr/net/socket.h> #include "CoAP/CoAP.h" LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); char response[512]; uint8_t RecvBuff[512]; // TCP Server Implementation ---------- static int SeverRcevHandler(int client) { int rc; int errno; struct pollfd fd = { .fd = client, .events = POLLIN }; rc = poll(&fd, 1, MSEC_PER_SEC * 10); if (rc < 0) { LOG_WRN("poll() error: %d", -errno); return -errno; } else if (rc == 0) { LOG_WRN("poll() timeout"); return -EAGAIN; } rc = recv(client, RecvBuff, sizeof(RecvBuff), 0); if (rc <= 0) { return rc == 0 ? -ENOTCONN : -errno; } LOG_HEXDUMP_DBG(RecvBuff, sizeof(RecvBuff), "RecvBuff:>"); if (send(client, RecvBuff, strlen(RecvBuff), 0) < 0) { return -errno; } return errno; } int StartTCPServer(void) { int serv; struct sockaddr_in bind_addr; static int counter; int err = 0; char *Server_Addr_str = "70.25.147.111"; uint32_t Server_Addr_u32 = 0; uint16_t Server_port = 502; char *next = 0; LOG_INF("[%d]%s", __LINE__, __func__); serv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (serv < 0) { LOG_ERR("error: socket: %d", errno); return 0; } LOG_INF("[%d]%s >> socket >> serv >> %d", __LINE__, __func__, serv); Server_Addr_u32 = strtol(Server_Addr_str, &next, 10); Server_Addr_u32 = Server_Addr_u32 << 8; Server_Addr_u32 |= strtol(&next[1], &next, 10); Server_Addr_u32 = Server_Addr_u32 << 8; Server_Addr_u32 |= strtol(&next[1], &next, 10); Server_Addr_u32 = Server_Addr_u32 << 8; Server_Addr_u32 |= strtol(&next[1], &next, 10); bind_addr.sin_family = AF_INET; bind_addr.sin_addr.s_addr = htonl(Server_Addr_u32);// htonl(INADDR_ANY); // INADDR_ANY bind_addr.sin_port = htons(Server_port); if (bind(serv, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) { LOG_ERR("error: bind: %d", errno); return 0; } if (listen(serv, 5) < 0) { LOG_ERR("error: listen: %d", errno); return 0; } errno = 0; LOG_INF("Started TCP server example on port %d, %d", Server_port, errno); LOG_DBG("Entering infinite while Loop %d", __LINE__); while (1) { struct sockaddr_in client_addr; socklen_t client_addr_len = sizeof(client_addr); char addr_str[INET_ADDRSTRLEN]; int client; int rc; client = accept(serv, (struct sockaddr *)&client_addr, &client_addr_len); if (client < 0) { LOG_ERR("error: accept: %d, %d", errno, client); k_msleep(200); continue; } inet_ntop(client_addr.sin_family, &client_addr.sin_addr, addr_str, sizeof(addr_str)); LOG_INF("Connection #%d from %s", counter++, addr_str); do { rc = SeverRcevHandler(client); k_msleep(1); } while (!rc); close(client); LOG_INF("Connection from %s closed, errno %d", addr_str, rc); } return 0; } void main(void) { int err = 0; LOG_INF("[%d]%s", __LINE__, __func__); LOG_INF("Project : CoAPAndServer"); err = nrf_modem_lib_init(); if (err < 0) { LOG_ERR("Modem library init failed, err: %d", err); return; } k_msleep(1000); err = nrf_modem_at_cmd(response, sizeof(response), "AT+CFUN=1"); if (err) // This condition will true, When there is an error occured { LOG_ERR("CFUN Failed %d", err); return; } do // Check Network in do..while(); { k_sleep(K_SECONDS(1)); // Delay between two iteration err = nrf_modem_at_cmd(response, sizeof(response), "AT+CEREG?"); if (err) // true, when error { LOG_ERR("ERR : %d, %s", err, response); } LOG_INF("RSP : %s", response); } while(!strstr(response, "+CEREG: 0,1")); CoAPSetup(); k_msleep(1000); StartTCPServer(); while(1) { // RTC_Read(); // RTC Read test // SHT40_Test(); // SHT40, NOTE : check mutex lock // vI2cScan(); // NOTE : check mutex lock // char DisplayValue[20] = {0}; // snprintf(DisplayValue, 20, "%.1f C\n%.1f RH\n%.1f H", fReadTemprature(), fReadHumidity(), fReadHumidex()); // DisplayString(DisplayValue); // LOG_INF("%s", DisplayValue); k_msleep(5000); } } /* [ Thread Tree ] Start_SysInit_Thread() --> Start_Network_Check_Thread() // Create new thread and distory self Start_Network_Check_Thread() --> Start_Get_APN_Thread((uint32_t)1000); // Create new thread and distory self Start_Get_APN_Thread((uint32_t)1000); --> Start_ModbusServer() // Create new thread and distory self Start_ModbusServer() // This one runs forever */
3. Coap server related source file
/* * @file : CoAP.c */ #include <zephyr/kernel.h> #include <zephyr/logging/log.h> #include <zephyr/net/coap_service.h> #include "CoAP.h" int tcp_socket = 0; LOG_MODULE_REGISTER(CoAP_APP, LOG_LEVEL_INF); static const uint16_t my_service_port = 5683; COAP_SERVICE_DEFINE(my_service, "70.25.147.111", &my_service_port, COAP_SERVICE_AUTOSTART); static int my_get(struct coap_resource *resource, struct coap_packet *request, struct sockaddr *addr, socklen_t addr_len) { static const char *msg = "Hello, This is CoAP Server !!"; uint8_t data[CONFIG_COAP_SERVER_MESSAGE_SIZE]; struct coap_packet response; uint16_t id; uint8_t token[COAP_TOKEN_MAX_LEN]; uint8_t tkl, type; LOG_INF("my_get from COAP"); type = coap_header_get_type(request); id = coap_header_get_id(request); tkl = coap_header_get_token(request, token); /* Determine response type */ type = (type == COAP_TYPE_CON) ? COAP_TYPE_ACK : COAP_TYPE_NON_CON; coap_packet_init(&response, data, sizeof(data), COAP_VERSION_1, type, tkl, token, COAP_RESPONSE_CODE_CONTENT, id); /* Set content format */ coap_append_option_int(&response, COAP_OPTION_CONTENT_FORMAT, COAP_CONTENT_FORMAT_TEXT_PLAIN); /* Append payload */ coap_packet_append_payload_marker(&response); coap_packet_append_payload(&response, (uint8_t *)msg, strlen(msg)); /* Send to response back to the client */ return coap_resource_send(resource, &response, addr, addr_len, NULL); } static int my_put(struct coap_resource *resource, struct coap_packet *request, struct sockaddr *addr, socklen_t addr_len) { /* ... Handle the incoming request ... */ /* Return a CoAP response code as a shortcut for an empty ACK message */ return COAP_RESPONSE_CODE_CHANGED; } static const char * const my_resource_path[] = { "test", NULL }; COAP_RESOURCE_DEFINE(my_resource, my_service, { .path = my_resource_path, .get = my_get, .put = my_put, }); void CoAPSetup(void) { LOG_INF("coap_service_start : %d", coap_service_start(&my_service)); k_msleep(1000); LOG_INF("coap_service_is_running : %d", coap_service_is_running(&my_service)); }
4. Complete Project, you might able to compile this project using nRF SDK 2.6.0
I hope you might get idea from this information to understand the issue, and if you are not an engineer then please show this information to engineer, he might solve this or will give clear information of this issue.
Best Regard,
Dhaval