Trouble sending data over bluetooth HC-05

Hello to all

This is my first post on the forum and I am new to arduino and programming.

I have been working on an rfid tag reader to read sheep ear tags.
I am using an Arduino nano every, HC-05 bluetooth module, .91" I2C OLED display and
I purchased an rfid module from Priority 1 Design.

The module reads the tag, and displays the tag number on an oled display, sends the number to an app on my phone and writes to serial in this format:

826_023239200001

826 being the country code (UK) and 023239200001 is my flock/individual animal identifier with an underscore as a data separator in between the two.

This is working perfectly using this code

// SoftwareSerial - Version: Latest 
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


SoftwareSerial RFID(8, 9); //pin2 Rx, pin3 Tx
SoftwareSerial EEBlue(5, 6); // Rx | Tx


int out_flag = 0;
char rfidData[16];
int readCount = 0;




void setup()
{
  Serial.begin(9600);
  EEBlue.begin(9600);
  
  
 
  RFID.begin(9600);
  delay(10);
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(10);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("TAG READER");
  display.display();
}

void loop()

{
    while (RFID.available()) {
    byte C = RFID.read();
   
    if(C != '\r' && C != '\n')
     {
      rfidData[readCount] = C;
      readCount++;
      rfidData[readCount] = '\0';
      out_flag = 1;      
      delay(5);
     }
     
     display.clearDisplay();
     display.setTextSize(2);
     display.setTextColor(WHITE);
     display.setCursor(0,0);
     display.println(rfidData);
     display.display();                   

   
    
          
     EEBlue.write(C); // Send tag number to app
    
     
     Serial.write(C);  //Display tag number on serial monitor
    
    
    
    
    
   
   
    out_flag = 1;
  }

     
     
  
}

The app recognises the tag number and I then enter any relevant information I need to record like weights or treatments etc. When I’m finished I send the information recorded on the app to some online software and here lies the problem.

The software does not accept the underscore as the data separator and so the tag number “826_023239200001” is in an invalid format.

Acceptable formats would be 826 023239200001, or 826023239200001.

So I replaced the _ with a blank space

rfidData[3] = ' ';   // relaces underscore with blank space

This is now displaying on the oled display correctly as 826 023239200001.

However, When I try to send the char rfidData over the bluetooth

EEBlue.println(rfidData); // Send tag number to app

or print to serial it reads like this

8
82
826
826
826 0
826 02
826 023
826 0232
826 02323
826 023239
826 0232392
826 02323920
826 023239200
826 0232392000
826 02323920000
826 023239200001
826 023239200001

I have tried .print and .write but they don’t send it correctly either.

Before I was writing byte C and not the char to EEBlue.write but obviously this still has the underscore in it.

I am a complete novice and this is my first project so don’t really understand what I’m doing. I have been reading and trying different things for the past few days but had no luck so far.

How can I transmit the tag number in the format I want? Why is the char displaying like that?
If anybody could shed any light on this I would greatly appreciate it.

Thanks

Rob

Please post the code that produced the output that you quote

This is the code that gives me the following output

8
82
826
826
826 0
826 02
826 023
826 0232
826 02323
826 023239
826 0232392
826 02323920
826 023239200
826 0232392000
826 02323920000
826 023239200001
826 023239200001

// SoftwareSerial - Version: Latest 
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


SoftwareSerial RFID(8, 9); //pin2 Rx, pin3 Tx
SoftwareSerial EEBlue(5, 6); // Rx | Tx


int out_flag = 0;
char rfidData[16];
int readCount = 0;




void setup()
{
  Serial.begin(9600);
  EEBlue.begin(9600);
  
  
 
  RFID.begin(9600);
  delay(10);
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(10);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("TAG READER");
  display.display();
}

void loop()

{
    while (RFID.available()) {
    byte C = RFID.read();
   
    if(C != '\r' && C != '\n')
     {
      rfidData[readCount] = C;
      rfidData[3] = ' ';   // relaces underscore with blank space
      readCount++;
      rfidData[readCount] = '\0';
      out_flag = 1;      
      delay(5);
     }
     
     display.clearDisplay();
     display.setTextSize(2);
     display.setTextColor(WHITE);
     display.setCursor(0,0);
     display.println(rfidData);
     display.display();

   
    
          
     EEBlue.println(rfidData); // Send tag number to app
    
     
     Serial.println(rfidData);  //Display tag number on serial monitor
    
    
    
    
    
   
   
    out_flag = 1;
  }

     
     
  
}

Thanks

    EEBlue.println(rfidData); // Send tag number to app
    Serial.println(rfidData);  //Display tag number on serial monitor

Shouldn't you be doing this only when reading the RFID is complete and not for every character received ?

Yes that would probably help.

How would I go about doing that?

Please excuse my ignorance

I'm only guessing that it is a problem here, but using more than one instance of software serial will pretty surely get you into strife. The hardware serial is all but redundant anyway, so you might consider putting Bluetooth there. Just remember to disconnect it when uploading. Further, this enables you to debug Bluetooth traffic on the serial monitor which, right now, must be a pretty good idea.

As I understand it your output on the display is OK and the line that delivers is

    [b]display.[/b]println(rfidData);

I therefore suggest you should need

    [b]Serial.[/b]println(rfidData);

as the next line. This means it will show on serial monitor. Observe it there first, then connect Bluetooth. I recognise that this is the command you have already tried to use, but humour me.

Also, I am wondering if the junk you are getting is actually going to the display as well, but you cannot see it. Since your data is always the same length I submit

    display.clearDisplay();

is redundant. Also, I submit

    display.setTextSize(2);
     display.setTextColor(WHITE);

should be in setup, as you only need it once.
If you put a temporary

delay(5000;

at the bottom of the loop, you might see that the display is not altogether kosher. In that event, the real problem is in the data read, which I see is probably what is suggested in reply#3(!) I assume there is a standard procedure for reading RFID, but I would be suss about your out_flag commands. I suspect one is wrong, or both are redundant.

I have removed the bluetooth altogether but still get the same result.

I think you are right about the display but as it was originally that wasn't a problem, as the last line is the full and correct tag number and the display is only there as a visual reference so I can identify the animal quickly.

I still have to use

display.cleardisplay();

otherwise it displays on top of previous read.

You are correct about the out flags, they are redundant.

I think the rfid read is functioning properly as if I say

EEBlue.write(C); // Send tag number to app
Serial.write(C);  //Display tag number on serial monitor

The number is displayed on the serial monitor as 826_023239200001 which is exactly how it should. The same number is received by the app and also works as it should.

In the original sketch posted in my opening post, the char rfidData was originally only there to display the tag number on the OLED display in the correct way.

It is only when I try to use that char instead of byte C to

EEBlue.println(rfidData); // Send tag number to app
Serial.println(rfidData);  //Display tag number on serial monitor

that I have problems.

The reason for trying to do this is to remove or replace the unwanted underscore with

rfidData[3] = ' ';   // relaces underscore with blank space

How can I remove or replace the underscore and then send the tag number in the correct format over bluetooth? Can I replace a character in the byte C?

rharvey88:
Yes that would probably help.

How would I go about doing that?

Please excuse my ignorance

You are testing whether the input is complete using

    if (C != '\r' && C != '\n')

If it is complete, indicated by receipt of a carriage return or linefeed being received then that is the time to print the rfidData string and not after each character as you do now

I am using an Arduino nano every,

I'm only guessing that it is a problem here, but using more than one instance of software serial will pretty surely get you into strife. The hardware serial is all but redundant anyway, so you might consider putting Bluetooth there. Just remember to disconnect it when uploading. Further, this enables you to debug Bluetooth traffic on the serial monitor which, right now, must be a pretty good idea.

There are two hardware serial ports on the Nano Every. The USB connector goes to the Serial Monitor. Pins PC05 and PC04 are connected to the pins marked "RX0 and TX1" on the Nano Every's Arduino pin headers. These are Serial1. I would use this instead of Software Serial for the rfid reader. The HC05 can go on a Software Serial instance.

Noting Cattledog's piece, I see that I have fallen into the Nano Every trap, something I had forgotten about, and I withdraw my comment about putting Bluetooth on hardware serial! I'll blame you for that. Please use capital letters for names. With the language standards on this forum, every could mean anything. I think/hope the answer to your problem is in reply#7.