Problem with HC-05 receiving by ID

View previous topic View next topic Go down

Problem with HC-05 receiving by ID

Post by Andrey_ on Fri Jun 17, 2016 6:16 pm

Hi, I'm using the free app RoboRemoteFree 1.9.1 with the Bluetooth module HC-05 controlled by STM32F100RB. From microcontroller I'm sending:
- "x Led On \r\n" - When it receives 'v'
- "x Led Off \r\n" - When it receives 'i'

On the UI Interface I have created the following options and set it as below:
- Text field. Set id: x
- Text log. Standard configuration
- Kbd connector. Set Remote; Send at enter; Set Id: empty
- set command ending: \n
- Set Char Delay: I tried all options

The thing is, after connecting to the module and sending the first command 'v' or 'i' by the Kbd connector, I receive the correct message in both screens, Text log ('x Led On') and Text field ('Led On'), but if I try to send a second,third,fourth... command typing 'v' or 'i' as I did firstly I just receive the message in Text log ('x Led Off' or 'x Led On'), the Text Field or anything else that I set an Id stop receiving and freezes.

Just for test, I changed the Kbd connector to Local and type 'echo x text' and the text was showed in both screen again correctly, but setting back to Remote and typing 'v' or 'i', I just got the message in text log ;[.

I have no idea what's going on because it seems like the app is receiving the correct message (text log always receiving 'x Led Off' or 'x Led On')  but it's not putting the message in the fields set with id.

Andrey_

Posts : 3
Join date : 2016-06-17

View user profile

Back to top Go down

Re: Problem with HC-05 receiving by ID

Post by Admin on Tue Jun 21, 2016 7:37 am

Hi,
It seems to be a problem with command ending. Are you sure you set the command ending to "\n" and not "\r" ? Also, are you sure you send "x Led On \r\n" and not "x Led On \n\r" ? It is recommended to use same command ending everywhere in the project, so if you set it to "\n", it is recommended to send "x Led On \n", or if you want to use "x Led On \r\n", then set the command ending to "\r\n". I built an interface as you described and tested it with netcat and it seems to work. You can post the code for microcontroller if you want and I will take a look.

Admin
Admin

Posts : 111
Join date : 2016-03-08

View user profile http://roboremo.makeforum.net

Back to top Go down

Re: Problem with HC-05 receiving by ID

Post by Andrey_ on Tue Jun 21, 2016 8:27 am

Code:
#include "stm32f10x_usart.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "misc.h"
#include "stdio.h"


void USART1_IRQHandler(void);
void usarInit_rxtx(void);

int main(void)
{
  
 usartInit_rxtx();
 
    while(1)
    {

    }
}


void USART1_IRQHandler(void)
{int i;
    if ((USART1->SR & USART_FLAG_RXNE) != (u16)RESET)
    {
        i = USART_ReceiveData(USART1);
        if(i == 'v'){
            UARTSend("x Led On \r\n",sizeof("x Led On \r\n"));    // Send message to UART1
        }
        else if(i == 'i'){
            UARTSend("x Led Off \r\n",sizeof("x Led Off \r\n"));
        }
    }
}

void usartInit_rxtx(void)
{

   /* Enable USART1 and GPIOA clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

    /* NVIC Configuration */
    NVIC_Configuration();

    /* Configure the GPIOs */
    GPIO_Configuration();

    /* Configure the USART1 */
    USART_Configuration();

    /* Enable the USART1 Receive interrupt: this interrupt is generated when the
         USART1 receive data register is not empty */
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART1 Rx (PA.10) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

/*******************************************************************************
* Function Name  : USART_Configuration
* Description    : Configures the USART1
*******************************************************************************/
void USART_Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;

/* USART1 configuration ------------------------------------------------------*/
  USART_InitStructure.USART_BaudRate = 9600;        // Baud Rate
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  USART_Init(USART1, &USART_InitStructure);

  /* Enable USART1 */
  USART_Cmd(USART1, ENABLE);
}

/**
  * @brief  Configures the nested vectored interrupt controller.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the USARTx Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/*******************************************************************************
* Function Name  : UARTSend
* Description    : Send a string to the UART.
* Input          : - pucBuffer: buffers to be printed.
*                : - ulCount  : buffer's length
*******************************************************************************/
void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
    //
    // Loop while there are more characters to send.
    //
    while(ulCount--)
    {
        USART_SendData(USART1, (uint16_t) *pucBuffer++);
        /* Loop until the end of transmission */
        while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
        {
        }
    }
}

Sure, that's the code.
I have tested both '\r\n' and '\n' in command ending with no success.

Andrey_

Posts : 3
Join date : 2016-06-17

View user profile

Back to top Go down

Re: Problem with HC-05 receiving by ID

Post by Admin on Tue Jun 21, 2016 11:32 am

I am not familiar with STM microcontrollers, but the code seems OK.
Try to connect the microcontroller to a terminal like arduino serial monitor or CuteCom, and send "v\n", "i\n" and see the response.
Make sure the string you receive from microcontroller ends with command ending and nothing more. It may be useful to see the response in Hex format, maybe that characters are not displayed in String format.
I think it adds some more characters after "\n", and that characters go to the beginning of next command in RoboRemo, so the id won't be "x" anymore, but will have that characters before x.

Admin
Admin

Posts : 111
Join date : 2016-03-08

View user profile http://roboremo.makeforum.net

Back to top Go down

Re: Problem with HC-05 receiving by ID

Post by Admin on Tue Jun 21, 2016 11:43 am

I think I found the problem Smile
http://stackoverflow.com/questions/1392200/sizeof-string-literal
the UARTSend("x Led On \r\n",sizeof("x Led On \r\n"));
will send a 0x00 after \n
so the next command received by RoboRemo will have a 0x00 before "x" and will not match with items with id "x".

You can make a function
void UARTSendString(const unsigned char *pucBuffer)
and make it send everything until it finds a 0x00 (and don't send the 0x00)
then call UARTSendString("x Led On \r\n");

Admin
Admin

Posts : 111
Join date : 2016-03-08

View user profile http://roboremo.makeforum.net

Back to top Go down

Re: Problem with HC-05 receiving by ID

Post by Andrey_ on Fri Jun 24, 2016 6:34 am

Very Happy You're alright. The last character sent was '0' and it was coming by first on the next string. The first command was only working because no 0 was added on it, but after the call the next were starting with it. The responsible for that was the sizeof(), it was returning the correct length plus 1, so, I just put sizeof("what I want") -1, to the usart function. 
Now, everything is working ;] .

Thanks so much buddy.

Andrey_

Posts : 3
Join date : 2016-06-17

View user profile

Back to top Go down

Re: Problem with HC-05 receiving by ID

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum