Lwmesh Cordinator Data Receive - Page 1
Forum section: electronics / beginners

author: Peili
author_rank: Regular Contributor
timestamp: January 25, 2021, 03:31:31 am
content: Hi,I have8 routers which send data with 1sec delay. Coordinator receives data from these routers. Butsome times the data from routers are Mixed up.At the Coordinator side i used to write the Receiving packets using the functionCode:static bool appDataInd(NWK_DataInd_t *ind){for (uint8_t i = 0; i < ind->size; i++)HAL_UartWriteByte(ind->data);return true;}but data are mixing with one another.For example data from Router 1 is"ID_1001_00.003_0.04_2.5*"and Data from Router 2 is"ID_1008_TS_2.5*".But at the receiving side of coordinator the data received like"ID_1001_00ID_1008_TS_2.5*.003_0.04_2.5*".i don't know why this happening. Please help me to solve this. Please help me. here I'm Attaching my code for Coordinator(WSNDemoC.c) and Router(WSNDemoR.c)

author: Peili
author_rank: Regular Contributor
timestamp: January 25, 2021, 04:09:49 am
content: Anyone have idea about this. please help me to solve this. This type of Mixing up Data never faced. So please help me to solve this.

author: ataradov
author_rank: Super Contributor
timestamp: January 25, 2021, 04:41:30 am
content: It is pretty obvious. You are setting the flag on the first byte you receive from the UART. So the packet is sent as soon as execution gets to appSendData(). So the data from one router is split into two frames, and another router may send its frame between the frames from the first device.You need to wait for the whole data to be received. For example have a timeout that you reset every time you receive a character. If there is no new character in 100 ms, for example, then set the flag to send the whole buffer.

author: Peili
author_rank: Regular Contributor
timestamp: January 27, 2021, 09:21:44 am
content: here I'm checking some delimiters in my data.For exampleif the character is "*", then need to write all the characters to UART.(in coordinator side)Code:static bool appDataInd(NWK_DataInd_t *ind){for (uint8_t i=0;i<ind->size;i++){if(ind->data==0x40){atvalue=i;checkflag=1;break;}}if(checkflag==1){for (uint8_t k=atvalue;k<ind->size;k++){appDataRFBuffer[appRFBufferPtr++]= ind->data;if(ind->data==0x2A){rfflag=1;break;}}}if(rfflag==1){for (uint8_t j=0;j<appRFBufferPtr;j++){HAL_UartWriteByte(appDataRFBuffer);}}checkflag=0;appRFBufferPtr=0;rfflag=0;atvalue=0;return true;}also in Router side I'm checkingif "*" then send the packet to Coordinator.Code:void HAL_UartBytesReceived(uint16_t bytes){for (uint16_t i = 0; i < bytes; i++){uint8_t byte = HAL_UartReadByte();HAL_UartWriteByte(byte);if (appUartBufferPtr < sizeof(appUartBuffer))appUartBuffer[appUartBufferPtr++] = byte;if(byte==0x2A){flag=1;break;}}}Code:static void appSendData(void){#ifdef NWK_ENABLE_ROUTINGmsg.parentShortAddr = NWK_RouteNextHop(0,0);#elsemsg.parentShortAddr = 0;#endif//msg.sensors.battery = rand();msg.sensors.temperature = rand() & 0x7f;if(flag==1){memcpy(appDataReqBuffer, appUartBuffer, appUartBufferPtr);int brkflag=0;int i=0,j=0,a=5;for(i=0;i<appUartBufferPtr;i++){appDataReqBuffer=appUartBuffer;if(appUartBuffer==0x23){break;}j++;}//}int k=j;appUartBufferPtr=k;nwkDataReq.dstAddr=a;nwkDataReq.dstEndpoint = APP_ENDPOINT;nwkDataReq.srcEndpoint = APP_ENDPOINT;nwkDataReq.options = NWK_OPT_ACK_REQUEST | NWK_OPT_ENABLE_SECURITY;nwkDataReq.data =appDataReqBuffer;//(uint8_t *)&msg; //appDataReqBuffer;//nwkDataReq.size =appUartBufferPtr;//sizeof(msg); //appUartBufferPtr;//sizeof(msg);nwkDataReq.confirm = appDataConf;appUartBufferPtr=0;flag=0;NWK_DataReq(&nwkDataReq);appState = APP_STATE_WAIT_CONF;}elseappState=APP_STATE_SENDING_DONE;}attaching my code. But now also data mixing up occurs

author: Peili
author_rank: Regular Contributor
timestamp: January 27, 2021, 09:34:20 am
content: Quote from: ataradov on January 25, 2021, 04:41:30 amIs this checking cause error? I have 8 routers which send continuously data. All routers send packets in the same format@ID_001_Xg_-0.00_-0.00_-0.00_-0.00_-0.00_-0.00_-0.00_-0.00_-0.00_0.00_*So " @ " the starting and " * " is ending delimiter.Where to add those timeout? Coordinator receives data from 8 routers at same time. How can i set that timeout here?

author: Peili
author_rank: Regular Contributor
timestamp: January 27, 2021, 01:28:57 pm
content: I found that at receiving side, ie, Coordinator side, the whole packet is not receiving some times.Around 40 byte of data is sending from Nodes to Coordinator at a time.Is that is the issue?i tried to send"@_A_B_C_D_E_F_G_H_I_J_K_L_M_N_O_P_Q_R_S_T_U_V_W_X_Y_Z_1_2_3_4_5_6_7_8_9*from Router to Coordinator continuously .At the Coordinator side it receive like"@_A_B_C_D_E_F_G_H_I_J_K_L_M_N_O_P_Q_R_S_T_U_V_W_X_Y_Z_1_2_3_4_5_6_7_8_9*"@_A_B_C_D_E_F_G1_2_3_4_5_6_7_8_9*@_A_B_C_D_E_F_G_H_I_J_K_L_M_N_O_P_Q_R_S_T_U_V_W_X_Y_Z_1_2_3_4_5_6_7_8_9*@_A_B_C_R_S_T_U_V_W_X_Y_Z_1_2_3_4_5_6_7_8_9*

author: Peili
author_rank: Regular Contributor
timestamp: January 27, 2021, 03:20:10 pm
content: Is this related to Payload size? or receiver side UART problem?Please help me to solve this

author: ataradov
author_rank: Super Contributor
timestamp: January 27, 2021, 05:16:03 pm
content: NWK_DataReq() is atomic. It either sends everything or nothing. Your appDataInd() code is a complete mess and I have no desire to read it, no idea what it tries to do. It should not be necessary to have any of those loops.In the HAL_UartBytesReceived() you need to check that request buffer is not currently busy (flag=0). Otherwise the same code would be called on the next iteration of the task loop.Why don't you just debug this?

author: Peili
author_rank: Regular Contributor
timestamp: January 27, 2021, 05:39:30 pm
content: Now i changed my code to Orginal Lwmesh. Just use Router to read and send packet to Cordinator.WSNDemoC.c is Cordinator and WSNDemoR.c is Router.Router send packet to Coordinator. but some junk charcters in data at receiving side. and also data packet is missing.In the router side device write data to uart as FLOAT. and my Router read uart and send that to Coordinator.this is the data format@ID_001_Xg_-0.00_-0.00_-0.00_-0.00_-0.00_-0.00_-0.00_-0.00_-0.00_0.00_*but using without any condition or loops i have issue with receiving

author: ataradov
author_rank: Super Contributor
timestamp: January 27, 2021, 05:44:00 pm
content: I don't understand any of that. What junk characters?Again, have you tried to DEBUG anything at all?

author: Peili
author_rank: Regular Contributor
timestamp: January 27, 2021, 05:46:00 pm
content: Yes i debug that.@ID_001_Xg_-0.00_-0.00_-0.00_-0.00_-0.00_-0.00_-0.00_-0.00_-0.00_0.00_*this is the data dens from Router .But at receiver side it receives as@ID_001_Xg_-0.00_-0.00_-0.00_-0.00_-$%0_-0.00_-0.00_-0.00_-0.00_0.00_*

author: ataradov
author_rank: Super Contributor
timestamp: January 27, 2021, 05:47:48 pm
content: Do you receive it this way in the data indication function or on the serial port? You may have baudrate issues, for example.

author: Peili
author_rank: Regular Contributor
timestamp: January 27, 2021, 05:56:12 pm
content: Quote from: ataradov on January 27, 2021, 05:47:48 pmyes in the appDataind() function write data to UART.Code:static bool appDataInd(NWK_DataInd_t *ind){AppMessage_t *msg = (AppMessage_t *)ind->data;HAL_LedToggle(APP_LED_DATA);msg->lqi = ind->lqi;msg->rssi = ind->rssi;appUartSendMessage(ind->data, ind->size);if (APP_CommandsPending(ind->srcAddr))NWK_SetAckControl(APP_COMMAND_PENDING);return true;}my router Baudrate is 9600 and Coordinator Baud Rate is 38400.When i change my Router packet as@ID_008_OT_23.78*no problem with receiving side.

author: ataradov
author_rank: Super Contributor
timestamp: January 27, 2021, 05:58:15 pm
content: This does not answer the question. Do you see the data corrupted when you stop the debugger in appDataInd()?I don't care what happens on UART.If the data is fine in appDataInd(), then something is wrong with your UART. Get the scope and check the baudrate.

author: Peili
author_rank: Regular Contributor
timestamp: January 27, 2021, 06:01:28 pm
content: Quote from: ataradov on January 27, 2021, 05:58:15 pmSorry i didn't understand with Debugger?

author: ataradov
author_rank: Super Contributor
timestamp: January 27, 2021, 06:03:09 pm
content: Do you have an actual debugger tool? What is your development process?

author: Peili
author_rank: Regular Contributor
timestamp: January 27, 2021, 06:04:39 pm
content: I Don't have any tools. I'm just confirming with UART. When appDataInd Receives and then write to UART

author: ataradov
author_rank: Super Contributor
timestamp: January 27, 2021, 06:05:20 pm
content: How do you program the devices?

author: Peili
author_rank: Regular Contributor
timestamp: January 27, 2021, 06:06:19 pm
content: I'm using ATMEGA256RFR2. devices are programming using AVR Dragon via Atmel Studio

author: ataradov
author_rank: Super Contributor
timestamp: January 27, 2021, 06:11:08 pm
content: Dragon support debugging as far as I know. So use Atmel Studio and start and actual debugging session, setup some breakpoints and do actual debugging.It is possible to get by without the debugger, but it is a skill you need to develop, and I can't help you here.If you want to debug things like this without a debugger, just check in the indication function that payload does not contain any garbage characters and toggle a pin if ti does. If pin does not toggle, but you see garbage on the output, then the issue is with the UART output.

author: Peili
author_rank: Regular Contributor
timestamp: January 28, 2021, 07:48:34 am
content: Fixed garbage character issue. thank you. it was due to Baud Rate .But still i have Mixing Up of data.i can't figure out where I'm getting these issues.Some packets receiving correctly and some packets are Mixing

author: Peili
author_rank: Regular Contributor
timestamp: January 28, 2021, 07:51:02 am
content: Sometimes the received packet write to Coordinator correctly. But some time missing of characters and mixing up is occurring.Code:/*** \file WSNDemo.c** \brief WSNDemo application implementation** Copyright (C) 2012-2014, Atmel Corporation. All rights reserved.** \asf_license_start** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions are met:** 1. Redistributions of source code must retain the above copyright notice,* this list of conditions and the following disclaimer.** 2. Redistributions in binary form must reproduce the above copyright notice,* this list of conditions and the following disclaimer in the documentation* and/or other materials provided with the distribution.** 3. The name of Atmel may not be used to endorse or promote products derived* from this software without specific prior written permission.** 4. This software may only be redistributed and used in connection with an* Atmel microcontroller product.** THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE* POSSIBILITY OF SUCH DAMAGE.** \asf_license_stop** Modification and other use of this code is subject to Atmel's Limited* License Agreement (license.txt).** $Id: WSNDemo.c 9267 2014-03-18 21:46:19Z ataradov $**//*- Includes ---------------------------------------------------------------*/#include <stdlib.h>#include <stdio.h>#include <string.h>#include "config.h"#include "hal.h"#include "phy.h"#include "sys.h"#include "nwk.h"#include "nwkRoute.h"#include "nwkSecurity.h"#include "sysTimer.h"#include "halUart.h"#include "halSleep.h"#include "halBoard.h"#include "halLed.h"#include "commands.h"/*- Definitions ------------------------------------------------------------*/#if defined(APP_COORDINATOR)#define APP_NODE_TYPE 0#elif defined(APP_ROUTER)#define APP_NODE_TYPE 1#else#define APP_NODE_TYPE 2#endif#define APP_CAPTION_SIZE (sizeof(APP_CAPTION) - 1)#define APP_COMMAND_PENDING 0x01#define APP_ENDPOINT 1#define APP_LED_NETWORK 0#define APP_LED_DATA 1/*- Types ------------------------------------------------------------------*/typedef struct PACK{uint8_t commandId;uint8_t nodeType;uint64_t extAddr;uint16_t shortAddr;uint32_t softVersion;uint32_t channelMask;uint16_t panId;uint8_t workingChannel;uint16_t parentShortAddr;uint8_t lqi;int8_t rssi;struct PACK{uint8_t type;uint8_t size;int32_t battery;int32_t temperature;int32_t light;} sensors;struct PACK{uint8_t type;uint8_t size;char text;} caption;} AppMessage_t;typedef enum AppState_t{APP_STATE_INITIAL,APP_STATE_SEND,APP_STATE_WAIT_CONF,APP_STATE_SENDING_DONE,APP_STATE_WAIT_SEND_TIMER,APP_STATE_WAIT_COMMAND_TIMER,APP_STATE_PREPARE_TO_SLEEP,APP_STATE_SLEEP,APP_STATE_WAKEUP,} AppState_t;static void appSendData(void);/*- Variables --------------------------------------------------------------*/static AppState_t appState = APP_STATE_INITIAL;#define APP_BUFFER_SIZE NWK_MAX_PAYLOAD_SIZE#if defined(APP_ROUTER) || defined(APP_ENDDEVICE)static NWK_DataReq_t appNwkDataReq;static SYS_Timer_t appNetworkStatusTimer;static SYS_Timer_t appCommandWaitTimer;static bool appNetworkStatus;#endifstatic AppMessage_t appMsg;static SYS_Timer_t appDataSendingTimer;static uint8_t appDataReqBuffer;static uint8_t appUartBuffer;static uint8_t appUartBufferPtr = 0;static uint8_t flag=0;/*- Implementations --------------------------------------------------------*//*************************************************************************//*******************************************************************************/void HAL_UartBytesReceived(uint16_t bytes){for (uint16_t i = 0; i < bytes; i++){uint8_t byte = HAL_UartReadByte();if (len(bytes)==0)flag=1;//if (appUartBufferPtr == sizeof(appUartBuffer))//appSendData();if (appUartBufferPtr < sizeof(appUartBuffer))appUartBuffer[appUartBufferPtr++] = byte;}}/*************************************************************************//*******************************************************************************/static void appUartSendMessage(uint8_t *data, uint8_t size){uint8_t cs = 0;//HAL_UartWriteByte(0x10);//HAL_UartWriteByte(0x02);for (uint8_t i = 0; i < size; i++){if (data == 0x10){// HAL_UartWriteByte(0x10);cs += 0x10;}HAL_UartWriteByte(data);cs += data;}// HAL_UartWriteByte(0x10);//HAL_UartWriteByte(0x03);cs += 0x10 + 0x02 + 0x10 + 0x03;// HAL_UartWriteByte(cs);}/*************************************************************************//*******************************************************************************/static bool appDataInd(NWK_DataInd_t *ind){AppMessage_t *msg = (AppMessage_t *)ind->data;HAL_LedToggle(APP_LED_DATA);msg->lqi = ind->lqi;msg->rssi = ind->rssi;appUartSendMessage(ind->data, ind->size);if (APP_CommandsPending(ind->srcAddr))NWK_SetAckControl(APP_COMMAND_PENDING);return true;}/*************************************************************************//*******************************************************************************/static void appDataSendingTimerHandler(SYS_Timer_t *timer){if (APP_STATE_WAIT_SEND_TIMER == appState)appState = APP_STATE_SEND;elseSYS_TimerStart(&appDataSendingTimer);(void)timer;}#if defined(APP_ROUTER) || defined(APP_ENDDEVICE)/*************************************************************************//*******************************************************************************/static void appNetworkStatusTimerHandler(SYS_Timer_t *timer){HAL_LedToggle(APP_LED_NETWORK);(void)timer;}/*************************************************************************//*******************************************************************************/static void appCommandWaitTimerHandler(SYS_Timer_t *timer){appState = APP_STATE_SENDING_DONE;(void)timer;}#endif/*************************************************************************//*******************************************************************************/#if defined(APP_ROUTER) || defined(APP_ENDDEVICE)static void appDataConf(NWK_DataReq_t *req){HAL_LedOff(APP_LED_DATA);if (NWK_SUCCESS_STATUS == req->status){if (!appNetworkStatus){HAL_LedOn(APP_LED_NETWORK);SYS_TimerStop(&appNetworkStatusTimer);appNetworkStatus = true;}}else{if (appNetworkStatus){HAL_LedOff(APP_LED_NETWORK);SYS_TimerStart(&appNetworkStatusTimer);appNetworkStatus = false;}}if (APP_COMMAND_PENDING == req->control){SYS_TimerStart(&appCommandWaitTimer);appState = APP_STATE_WAIT_COMMAND_TIMER;}else{appState = APP_STATE_SENDING_DONE;}}#endif/*************************************************************************//*******************************************************************************/static void appSendData(void){#ifdef NWK_ENABLE_ROUTINGappMsg.parentShortAddr = NWK_RouteNextHop(0, 0);#elseappMsg.parentShortAddr = 0;#endifappMsg.sensors.battery = rand() & 0xffff;appMsg.sensors.temperature = rand() & 0x7f;appMsg.sensors.light = rand() & 0xff;#if defined(APP_COORDINATOR)//appUartSendMessage((uint8_t *)&appMsg, sizeof(appMsg));SYS_TimerStart(&appDataSendingTimer);appState = APP_STATE_WAIT_SEND_TIMER;#elseif(flag==1){memcpy(appDataReqBuffer, appUartBuffer, appUartBufferPtr);appDataReqBuffer[appUartBufferPtr+1]=0x23;appNwkDataReq.dstAddr = 0;appNwkDataReq.dstEndpoint = APP_ENDPOINT;appNwkDataReq.srcEndpoint = APP_ENDPOINT;appNwkDataReq.options = NWK_OPT_ACK_REQUEST | NWK_OPT_ENABLE_SECURITY;appNwkDataReq.data = appDataReqBuffer;appNwkDataReq.size = appUartBufferPtr;appNwkDataReq.confirm = appDataConf;appUartBufferPtr = 0;HAL_LedOn(APP_LED_DATA);NWK_DataReq(&appNwkDataReq);appState = APP_STATE_WAIT_CONF;}elseappState = APP_STATE_SENDING_DONE;#endif}/*************************************************************************//*******************************************************************************/static void appInit(void){appMsg.commandId = APP_COMMAND_ID_NETWORK_INFO;appMsg.nodeType = APP_NODE_TYPE;appMsg.extAddr = APP_ADDR;appMsg.shortAddr = APP_ADDR;appMsg.softVersion = 0x01010100;appMsg.channelMask = (1L << APP_CHANNEL);appMsg.panId = APP_PANID;appMsg.workingChannel = APP_CHANNEL;appMsg.parentShortAddr = 0;appMsg.lqi = 0;appMsg.rssi = 0;appMsg.sensors.type = 1;appMsg.sensors.size = sizeof(int32_t) * 3;appMsg.sensors.battery = 0;appMsg.sensors.temperature = 0;appMsg.sensors.light = 0;appMsg.caption.type = 32;appMsg.caption.size = APP_CAPTION_SIZE;memcpy(appMsg.caption.text, APP_CAPTION, APP_CAPTION_SIZE);HAL_BoardInit();HAL_LedInit();NWK_SetAddr(APP_ADDR);NWK_SetPanId(APP_PANID);PHY_SetChannel(APP_CHANNEL);#ifdef PHY_AT86RF212PHY_SetBand(APP_BAND);PHY_SetModulation(APP_MODULATION);#endifPHY_SetRxState(true);#ifdef NWK_ENABLE_SECURITYNWK_SetSecurityKey((uint8_t *)APP_SECURITY_KEY);#endifNWK_OpenEndpoint(APP_ENDPOINT, appDataInd);appDataSendingTimer.interval = APP_SENDING_INTERVAL;appDataSendingTimer.mode = SYS_TIMER_INTERVAL_MODE;appDataSendingTimer.handler = appDataSendingTimerHandler;#if defined(APP_ROUTER) || defined(APP_ENDDEVICE)appNetworkStatus = false;appNetworkStatusTimer.interval = 500;appNetworkStatusTimer.mode = SYS_TIMER_PERIODIC_MODE;appNetworkStatusTimer.handler = appNetworkStatusTimerHandler;SYS_TimerStart(&appNetworkStatusTimer);appCommandWaitTimer.interval = NWK_ACK_WAIT_TIME;appCommandWaitTimer.mode = SYS_TIMER_INTERVAL_MODE;appCommandWaitTimer.handler = appCommandWaitTimerHandler;#elseHAL_LedOn(APP_LED_NETWORK);#endif#ifdef PHY_ENABLE_RANDOM_NUMBER_GENERATORsrand(PHY_RandomReq());#endifAPP_CommandsInit();appState = APP_STATE_SEND;}/*************************************************************************//*******************************************************************************/static void APP_TaskHandler(void){switch (appState){case APP_STATE_INITIAL:{appInit();} break;case APP_STATE_SEND:{appSendData();} break;case APP_STATE_SENDING_DONE:{#if defined(APP_ENDDEVICE)appState = APP_STATE_PREPARE_TO_SLEEP;#elseSYS_TimerStart(&appDataSendingTimer);appState = APP_STATE_WAIT_SEND_TIMER;#endif} break;case APP_STATE_PREPARE_TO_SLEEP:{if (!NWK_Busy()){NWK_SleepReq();appState = APP_STATE_SLEEP;}} break;case APP_STATE_SLEEP:{HAL_LedClose();HAL_Sleep(APP_SENDING_INTERVAL);appState = APP_STATE_WAKEUP;} break;case APP_STATE_WAKEUP:{NWK_WakeupReq();HAL_LedInit();HAL_LedOn(APP_LED_NETWORK);appState = APP_STATE_SEND;} break;default:break;}}/*************************************************************************//*******************************************************************************/int main(void){SYS_Init();HAL_UartInit(9600);while (1){SYS_TaskHandler();HAL_UartTaskHandler();APP_TaskHandler();}}I'm using this code for Router and Coordinator.

author: ataradov
author_rank: Super Contributor
timestamp: January 28, 2021, 07:53:59 am
content: Start by eliminating USRAT entirely and just sending the same fixed buffer filled with the same value from every device. Make the value different for each R.On the C check that every frame that is received is filled with the same data. If it is the case, then you know that the issue is in a way USART formats and splits the data. This is easy enough to test. Just check the length of the received data on the C.One other thing that is wrong with your original code. You are setting "flag = 0;" right after you call NWK_DataReq(), but the buffer is really free once confirmation callback is called.And get the debugging to work. You clearly have no idea how to debug indirectly.