Bluetooth Communication - BlueSmirf Silvers

Hey Guys,
So I'm trying to get 2 Arduino Nano Every's to communicate via BlueSmirf Silver Bluetooth (RN-42) units. Using the serial monitor on the master unit (display unit), the communication seems to get hung up on the BTInit() and no connection is established. Any help is greatly appreciated.

Display Code - master and receiver:

Display Code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SD.h>

#define OLED_ADDRESS 0x3C
Adafruit_SSD1306 display(OLED_ADDRESS);

int msgToken = 0;

int incomingByte = 0;
int angle = 0;
char gBtMsg[256];
char gBTAdr[13];
char gBtCmd[256];

int gBtKnownMACTotal = 2;
char* gBtKnownMAC[2];  //This is set to hold only two MAC adresses

//File gLogFile;
void SdInitLog(void) {
  SD.begin(10);
}

void SdLog(char* i_pBtCmd) {
  if (strlen(i_pBtCmd) > 0) {
    File gLogFile = SD.open("btlog.txt", FILE_WRITE);
    if (gLogFile) {
      gLogFile.println(i_pBtCmd);
      gLogFile.close();
    }
  }
}

void BtReceive(void) {
  bool keepReading = true;
  int index = 0;

  gBtMsg[0] = '\0';

  while (keepReading) {
    keepReading = false;

    if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();
      if (incomingByte != 13) {
        gBtMsg[index++] = incomingByte;
        keepReading = true;
      }
    }
  }

  gBtMsg[index] = '\0';
}

void BtSend(char* i_pBtCmd, bool i_ln = true) {

  if (i_ln) {
    Serial.println(i_pBtCmd);
  } else {
    Serial.print(i_pBtCmd);
  }

  delay(100);
  BtReceive();
}
void BtInit(void) {

  bool btConnect = false;


  gBtKnownMAC[0] = "0006664346C1";  // Change this to one of your MAC addresses
  gBtKnownMAC[1] = "00066643475A";  // and this one too

  SdInitLog();
  SdLog("#### start ####");

  Serial.begin(115200);
  BtSend("$$$", false);
  delay(1000);


  SdLog(">>>> Set To Master <<<<");

  BtSend("SM,1");
  delay(1000);
  BtSend("I,5");
  BtSend("---");

  while (!btConnect) {
    delay(100);
    BtReceive();
    int msgLen = strlen(gBtMsg);
    if (msgLen > 0) {

      if (msgLen >= 12) {
        char* doneMsg = &gBtMsg[msgLen - 12];

        gBtMsg[12] = '\0';
        bool foundKnownMAC = false;

        for (int i = 0; i < gBtKnownMACTotal && !foundKnownMAC; i++) {
          if (!strcmp(gBtMsg, gBtKnownMAC[i])) {
            foundKnownMAC = true;
          }
        }

        if (!strcmp(doneMsg, "Inquiry Done")) {

          SdLog(doneMsg);

          if (foundKnownMAC) {
            gBtCmd[0] = 'C';
            gBtCmd[1] = ',';

            for (int i = 0; i < 12; i++) {
              gBtCmd[i + 2] = gBtMsg[i];
            }

            gBtCmd[15] = '\0';

            BtSend("$$$", false);
            BtSend(gBtCmd);
            BtSend("---");

            delay(2000);


            while (!btConnect) {
              delay(1000);
              BtSend("$$$", false);
              BtSend("GK");

              int numVal = 0;

              if (strlen(gBtMsg) > 0) {
                numVal = atoi(gBtMsg);
              }

              if (numVal == 1) {
                btConnect = true;
                SdLog("Is connected !!!!!!");
              }
              BtSend("---");
            }
          }
        }
      }
    }
  }


  msgToken = 1;


  SdLog("#### end ####");
}

void setup() {
  BtInit();

  delay(500);

  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(32, 0);
  display.display();
}

void receiveMsg() {
  int angle;
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    if (incomingByte == 'R') {
      delay(50);
    } else {
      angle = incomingByte;
    }
  }

  delay(100);
}

void sendMsg() {

  delay(50);
  Serial.print("R");

  delay(50);
  Serial.print("R");

  delay(50);

  Serial.print("R");
}

void loop() {

  display.clearDisplay();
  display.setCursor(32, 0);
  display.print("Angle: ");
  display.print(angle);
  display.display();

  Serial.println(angle);
  delay(100);
}

Transmitter Code - slave and sensor read:

#include <SD.h>

int msgToken = 0;

int incomingByte = 0;
char gBtMsg[256];
char gBTAdr[13];
char gBtCmd[256];

int gBtKnownMACTotal = 2;
char* gBtKnownMAC[2];  //This is set to hold only two MAC adresses

//File gLogFile;

int pinA = 3;  // Connected to CLK on KY-040
int pinB = 4;  // Connected to DT on KY-040
int encoderPosCount = 0;
int pinALast;
int aVal;
boolean bCW;
float conv_ang = 0.1;
float angle;

void SdInitLog(void) {
  SD.begin(10);
}

void SdLog(char* i_pBtCmd) {
  if (strlen(i_pBtCmd) > 0) {
    File gLogFile = SD.open("btlog.txt", FILE_WRITE);
    if (gLogFile) {
      gLogFile.println(i_pBtCmd);
      gLogFile.close();
    }
  }
}

void BtReceive(void) {
  bool keepReading = true;
  int index = 0;

  gBtMsg[0] = '\0';

  while (keepReading) {
    keepReading = false;

    if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();
      if (incomingByte != 13) {
        gBtMsg[index++] = incomingByte;
        keepReading = true;
      }
    }
  }

  gBtMsg[index] = '\0';
}

void BtSend(char* i_pBtCmd, bool i_ln = true) {

  if (i_ln) {
    Serial.println(i_pBtCmd);
  } else {
    Serial.print(i_pBtCmd);
  }

  delay(100);
  BtReceive();
}


void BtInit(void) {

  bool btConnect = false;


  gBtKnownMAC[0] = "0006664346C1";  // Change this to one of your MAC addresses
  gBtKnownMAC[1] = "00066643475A";  // and this one too

  SdInitLog();
  SdLog("#### start ####");

  Serial.begin(115200);
  BtSend("$$$", false);


    SdLog(">>>> Set To Slave <<<<");
    BtSend("SM,0");
    BtSend("---");

    while (!btConnect) {
      delay(1000);

      BtSend("$$$", false);
      BtSend("GK");

      int numVal = 0;

      if (strlen(gBtMsg) > 0) {
        numVal = atoi(gBtMsg);
      }

      if (numVal == 1) {
        btConnect = true;
        SdLog("Is connected !!!!!!");
      }

      BtSend("---");
    }
  

  SdLog("#### end ####");
}

void setup() {
  pinMode(pinA, INPUT);
  pinMode(pinB, INPUT);
  /* Read Pin A Whatever state it's in will reflect the last position */
  pinALast = digitalRead(pinA);

  delay(500);

  BtInit();
}

void receiveMsg() {
  for (int i = 0; i < 10; i++) {
    if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();
      if (incomingByte == 'R') {

        delay(50);
      }
    }

    delay(100);
  }
}

void sendMsg() {

  delay(50);
  Serial.print("R");

  delay(50);
  Serial.print("R");

  delay(50);

  Serial.print("R");
}

void loop() {
  aVal = digitalRead(pinA);
  if (aVal != pinALast) {
    // Means the knob is rotating
    // if the knob is rotating, we need to determine direction
    // We do that by reading pin B.
    if (digitalRead(pinB) != aVal) {
      // Means pin A Changed first - We're Rotating Clockwise.
      encoderPosCount++;
      bCW = true;
    } else {
      // Otherwise B changed first and we're moving CCW
      bCW = false;
      encoderPosCount--;
    }
    angle = encoderPosCount * conv_ang;

    Serial.print("Angle transmitted: ");
    Serial.println(angle);
    BtSendAngle(angle);
  }
  pinALast = aVal;
  //delay(50);
}

void BtSendAngle(float angle) 
{
  char angleStr[10];
  dtostrf(angle, 4, 2, angleStr);
  strcat(angleStr, "\n");
  BtSend(angleStr, false);
}

I would think that the two BlueSmirf units are better connected to Serial1.

Serial1 is on the Tx and Rx pins labeled on the board and it is independent of the usb serial addressed as Serial.

@cattledog ,

You're correct, I had that epiphany last night that the serial monitor interferes with the tx,rx communicating. They are wired to tx,rx and I did run them with just external battery power but no connection was made.

I've independently tested the display master code to verify the display works. It does and the loop pulls up the display showing "Angle: 0"

With Bluetooth added in, the display never initializes so that leads me to think it's stuck in BTInit I think, but don't know why. I pulled the MAC addresses right off the side placard of the devices

Are the module to arduino connections Tx>Rx and Rx>Tx?

I would recommend that you start with a much simplified test regime and see if you can just get a single module communicating with the Nano Every with some simple command which gets a response, and then move on to getting communication between two BlueSmirfs.

So I'm using the most basic code I can find to control this module.

#include <SoftwareSerial.h>  

// Configure the serial communication pins
SoftwareSerial bluetooth(10, 11); // RX, TX


void setup()
{
  Serial.begin(9600);  // Begin the serial monitor at 9600bps

  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$$$");  // Print three times individually
  //bluetooth.print("$");
  //bluetooth.print("$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600);  // Start bluetooth serial at 9600
}

void loop()
{
  if(bluetooth.available())  // If the bluetooth sent any characters
  {
    // Send any characters the bluetooth prints to the serial monitor
    Serial.print((char)bluetooth.read());  
  }
  if(Serial.available())  // If stuff was typed in the serial monitor
  {
    // Send any characters the Serial monitor prints to the bluetooth
    bluetooth.print((char)Serial.read());
  }
  // and loop forever and ever!
}

I have the Bluetooth wired as TX -> Pin 10 and Rx -> Pin 11

I run the code and theoretically should be able to control the Bluetooth through the USB serial monitor on Arduino (doesn't work). Additionally, when I connect via my PC and the "Bluetooth Serial Terminal Program" I am able to connect to the module (light goes green) and send words that are parrotted back to me. However the serial monitor on the Arduino program outputs random symbols (diamond with a question mark in it or hollow square).

I can't seem to get a logical output that would allow me to then control the settings of the RN-42 so that I can code this any better. Any tips? I'm stumped

SoftwareSerial bluetooth(10, 11); // RX, TX
bluetooth.begin(115200);

Software Serial will not work at this high baud rate. It is marginal at 38K.

You are using Nano Everys. There is a hardware serial channel on pin0 and 1 which is independent of the usb serial, and it is what you should be using. Why are you not using the Tx an Rx pins which are addressed as Serial1?