Article 4VKTC HC-06 resends last message on connection loss

HC-06 resends last message on connection loss

by
CollieJim
from LinuxQuestions.org on (#4VKTC)
The remote controls for my TV and Satellite box are failing, so I made a new remote that uses Bluetooth rather than IR.

The keypad uses an STM32C103C8T6 (Blue Pill) and an HC-05, and sends the 4-byte code. The receiver uses an Arduino Pro Mini and HC-06, and feeds it to the irremote library.

It works quite well, but has a rather annoying behavior when the keypad is turned off to save its battery.

20 seconds after the keypad is turned off, the receiver's HC-06 starts blinking rapidly indicating it's waiting for a connection. At that time it appears to send to the Pro Mini the last message again, followed by 2 messages containing a single 'c'. If the last command was to turn off the TV, it gets turned on again!

The workaround is to make sure the last button pressed does nothing.
Do I have a weird module, or am I missing something?
Perhaps the extra messages are part of the protocol to indicate connection loss?

Code:#define Tx_Pin 6
#define Rx_Pin 5

#include <SoftwareSerial.h>
SoftwareSerial mySerial ( Rx_Pin, Tx_Pin ); // RX, TX

#include <IRremote.h> // IR remote control library
IRsend irsend;

// pin for the LED:
const int IR_Pin = 3;
const int FB_Pin = 8;

#define NEC 1 // tell library to use NEC protocol
char dbg[20];
char buf[20]; // receives code from BT link
int i;
unsigned long key;
/*
* Visual feedback
*/
void Blink ( int Count )
{
for ( int j = 0; j < Count; j++ )
{
digitalWrite ( FB_Pin, HIGH );
delay ( 100 );
digitalWrite ( FB_Pin, LOW );
delay ( 100 );
}
}

void Blink1 ( )
{
digitalWrite ( FB_Pin, HIGH );
delay ( 20 );
digitalWrite ( FB_Pin, LOW );
}
/*
* Get code to send via IR as ASCII HEX
* and return binary value in key
*/
void GetCode ( void )
{
for ( i = 0; i < 20; i++ )
buf[i] = 0;

while ( !mySerial.available ( ) ); // wait for something

i = 0;
do
{
while ( !mySerial.available ( ) ); // wait for something
buf[i] = mySerial.read ( );
i++;
} while ( buf[i-1] >= '0');

// get rid of any trailing stuff
if ( mySerial.available ( ) )
mySerial.read ( );
delay ( 2 );
if ( mySerial.available ( ) )
mySerial.read ( );
delay ( 2 );
if ( mySerial.available ( ) )
mySerial.read ( );

sscanf ( buf, "%lX", &key );
Serial.println ( key, HEX );
}

void setup ( )
{
Serial.begin ( 38400 );
mySerial.begin ( 38400 );

// pinMode ( IR_Pin, OUTPUT );
pinMode ( FB_Pin, OUTPUT );
Blink ( 8 );
Serial.println ( "Starting" );
}

void loop ( )
{
GetCode ( ); // blocking
irsend.sendNEC ( key, 32 );
Blink1 ( );
}latest?d=yIl2AUoC8zA latest?i=5idDDsy0Bog:NjbBbVhF-V0:F7zBnMy latest?i=5idDDsy0Bog:NjbBbVhF-V0:V_sGLiP latest?d=qj6IDK7rITs latest?i=5idDDsy0Bog:NjbBbVhF-V0:gIN9vFw5idDDsy0Bog
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments