Arduino - Bluesmirf: Trouble reading Serial

Tried it without the USB connected but going to change to the SoftwareSerial and see if that helps. I'll be back with news.

Thanks for now

Post the latest version of your two programs.

...R

EDIT:
I tried to disconnect the bluesmirf module and the code stills makes the same printout. so my bluetooth.available() and read() doesn't seem to read the bluetooth?

These are my codes that are in use right now, sorry about that.

So I'm now using SoftwareSerial and the devices connect fine, but I'm still having trouble reading values. This is what's coming out from the serial monitor on the master:

CODE FOR MASTER

#include <SoftwareSerial.h> 

int LED_RED = 6;
int LED_YELLOW = 7;

int buttonPin = 4;
int ledPin = 12;
int buttonState = 0;

int value_BT = 0;

int bluetoothTx = 9;  // TX-O pin of Bluesmirf silver, Arduino D9
int bluetoothRx = 10;  // RX-I pin of Bluesmirf silver, Arduino D10

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_RED, OUTPUT);

///Indicating that this is the master
  led_indicate_master();

  bluetooth.begin(115200);
  bluetooth.print("$$");
  delay(100);
  bluetooth.println("SM,1");
  delay(100);
  bluetooth.println("C,000666643C3D");
  delay(100);
  bluetooth.println("---");
  

// initialize LED pin and pusbutton
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

  Serial.begin(115200);
  Serial.println("Debugging works");
}

void loop() {

  if (bluetooth.available() > 0) {
    value_BT = bluetooth.read();
    delay(50);
    Serial.println(value_BT);
    delay(50);
  }

}


void led_indicate_master() {
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);

  
}

CODE FOR SLAVE

#include <SoftwareSerial.h> 

int LED_RED = 5;
int LED_GREEN = 6;

int value_BT = 0;

int ledPin = 11;

int bluetoothTx = 9;  // TX-O pin of bluetooth mate, Arduino D9
int bluetoothRx = 10;  // RX-I pin of bluetooth mate, Arduino D10

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);

///Indicating that this is the slave
 
  led_indicate_slave();
  
  bluetooth.begin(115200);
  bluetooth.print("$$");
  delay(100);
  bluetooth.println("SM,0");  
  delay(100);
  bluetooth.println("---");
  
  pinMode(ledPin, OUTPUT);

}

void loop() {
 
  delay(2000);
  bluetooth.println(1);
  delay(2000);
  
}

void led_indicate_slave() {
  digitalWrite(LED_GREEN,HIGH);
  digitalWrite(LED_RED,HIGH);
  delay(5000);
  digitalWrite(LED_GREEN,LOW);
  digitalWrite(LED_RED,LOW);
  delay(50);
}

I don't think SoftwareSerial will work at 115200 baud. Try 9600 and if that works try 19200 and maybe 38400.

...R

This is what's coming out from the serial monitor on the master:

That is TEXT. That is the LAST picture of text that you will post.

Robin2:
I don't think SoftwareSerial will work at 115200 baud. Try 9600 and if that works try 19200 and maybe 38400.

...R

My BT modules won't connect at 9600, 19200 or 38400. Only 115200:S

PaulS:
That is TEXT. That is the LAST picture of text that you will post.

Didn't get this at all, have I done something to upset you? (Is there a rule that images aren't allowed to be inserted? If so than I've totally missed that. But why the anger?)

I tried to replace bluetooth.print to digitalWrite(bluetoothRx, HIGH) and on the other arduino digitalRead(bluetoothTx) but I only get 1 all the time from the read.

Is there a rule that images aren't allowed to be inserted?

Images are fine. Pictures of text are not.

But why the anger?

Two reasons. You are wasting bandwidth. And, I can't read the text in the picture.

firmaj:
My BT modules won't connect at 9600, 19200 or 38400. Only 115200:S

Then you may have arrived at the end of the SoftwareSerial road.
The Micro uses the 32U4 MCU and has a spare HardwareSerial port on pins 0 and 1.

firmaj:
I tried to replace bluetooth.print to digitalWrite(bluetoothRx, HIGH) and on the other arduino digitalRead(bluetoothTx) but I only get 1 all the time from the read.

Why would you think that might do anything useful ?

...R

PaulS:
Images are fine. Pictures of text are not.
Two reasons. You are wasting bandwidth. And, I can't read the text in the picture.

Got it, my apologies!

Robin2:
Then you may have arrived at the end of the SoftwareSerial road.
The Micro uses the 32U4 MCU and has a spare HardwareSerial port on pins 0 and 1.

Why would you think that might do anything useful ?

...R

My problem exists when using the hardware serial as well. I can't read what I send. I get the same number combination when printing what is read. So there's no difference , thats the weird part

firmaj:
My problem exists when using the hardware serial as well.

Post the code that demonstrates that and we will see if we can get it working properly.

...R

So there's no way of getting the SoftwareSerial to work? I kinda need two modems on one of the arduinos. I think its weird that it can send and receive when modules are connecting to each other but not when I read them.

Anyways, here are the codes when using the Hardware Serial (RX/TX)

MASTER CODE

int LED_GREEN = 6;
int LED_YELLOW = 7;

int buttonPin = 4;
int ledPin = 12;
int buttonState = 0;

int val = 0;

void setup() {
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);

  Serial.begin(115200);
  Serial.print("$$");

  digitalWrite(LED_YELLOW, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  delay(50);

  delay(100);
  Serial.println("SM,1");
  delay(100);

  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_GREEN, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_GREEN, LOW);
  delay(50);

  Serial.println("C,000666643C3D");
  delay(100);
  Serial.println("---");


// initialize LED pin and pusbutton
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);


}

void loop() {
  if (Serial.available() > 0) {
    val = Serial.read();
    Serial.println(val);
    delay(2000);
  }  
}

SLAVE CODE

int LED_RED = 5;
int LED_GREEN = 6;

int value_BT = 0;

int ledPin = 11;

void setup() {
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  
  Serial.begin(115200);
  Serial.print("$$");
  
  digitalWrite(LED_GREEN,HIGH);
  delay(500);
  digitalWrite(LED_GREEN,LOW);
  delay(50);
  
  delay(100);
  Serial.println("SM,0");  
  delay(100);
  
  digitalWrite(LED_RED,HIGH);
  digitalWrite(LED_GREEN,HIGH);
  delay(500);
  digitalWrite(LED_RED,LOW);
  digitalWrite(LED_GREEN,LOW);
  delay(50);

  
  Serial.println("---");
  
  pinMode(ledPin, OUTPUT);

}

void loop() {
  delay(2000);
  Serial.print(1);
  delay(2000);

  
}

So I played around with the arduinos and modules and used the code below. If I press the button on the slave it should send int value 1 and light the LED on the board. The master will read and print this in the console and then light it's LED.

This works in the first few seconds and I've observed two weird things (not counting the fact that my modules can't be configured in another baud rate than 115200).

  • When I press the button (value=1 sent from the slave) my value that is displayed in the console is 76 and when not pressed (value=0 sent from the slave) it goes to 48-49.

  • The console stops updating and the LED on the master doesn't work after a few seconds.

MASTER CODE

#include <SoftwareSerial.h> 

int i = 0;

int LED_RED = 6;
int LED_YELLOW = 7;

int buttonPin = 4;
int ledPin = 12;
int buttonState = 0;

int value_BT = 0;

int bluetoothTx = 9;  // TX-O pin of Bluesmirf silver, Arduino D9
int bluetoothRx = 10;  // RX-I pin of Bluesmirf silver, Arduino D10

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_RED, OUTPUT);
  pinMode(bluetoothTx, INPUT);
  pinMode(bluetoothRx, OUTPUT);

///Indicating that this is the master
  led_indicate_master();

  Serial.begin(115200);
  bluetooth.begin(115200);
 
  bluetooth.print("$$");
  delay(100);
  bluetooth.println("SM,1");
  delay(100);
  bluetooth.println("C,000666643C3D");
  delay(100);
  bluetooth.println("---");
  

// initialize LED pin and pusbutton
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

  Serial.println("Debugging works");

}

void loop() {

  if (bluetooth.available() > 0) {
    value_BT = bluetooth.read();
    Serial.println(value_BT);

    if (value_BT >= 76) {
      digitalWrite(ledPin, HIGH);
    } else {
      digitalWrite(ledPin, LOW);
    }
  }
}


void led_indicate_master() {
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
}

SLAVE CODE

#include <SoftwareSerial.h> 

int LED_RED = 5;
int LED_GREEN = 6;

int value_BT = 0;

int ledPin = 11;
int buttonPin = 12;
int buttonState = 0;

int bluetoothTx = 9;  // TX-O pin of Bluesmirf silver, Arduino D9
int bluetoothRx = 10;  // RX-I pin of Bluesmirf silver, Arduino D10

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(bluetoothTx, INPUT);
  pinMode(bluetoothRx, OUTPUT);
  
///Indicating that this is the slave
  led_indicate_slave();
  
  bluetooth.begin(115200);
  bluetooth.print("$$");
  delay(100);
  bluetooth.println("SM,0");  
  delay(100);
  bluetooth.println("---");
  
  pinMode(ledPin, OUTPUT);

}

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    value_BT = 1;
    digitalWrite(ledPin, HIGH);
    bluetooth.print(value_BT);
  } else {
    value_BT = 0;
    digitalWrite(ledPin, LOW);
    bluetooth.print(value_BT);
  }
}


void led_indicate_slave() {
  digitalWrite(LED_GREEN,HIGH);
  digitalWrite(LED_RED,HIGH);
  delay(5000);
  digitalWrite(LED_GREEN,LOW);
  digitalWrite(LED_RED,LOW);
  delay(50);
}

Slow down a little. I can't keep up with you.

In Reply #13 I understood you to say that your code would not work with HardwareSerial. As far as I know you only have one Arduino that can use HardwareSerial with your Bluetooth module so I assumed you were trying to send / receive Bluetooth data to/from a PC for testing purposes.

In my opinion you should get that working before you try to communicate with another Arduino using Bluetooth.

...R

Ah no, I've been connecting from Bluesmirf to Bluesmirf all this time.

I'll go back to Bluesmirf - Mac later today and come back with results.

I was just about to start but don't get why it would do me any good. I need to use SoftwareSerieal and I have already successfully connected the two bluesmirfs which would suggest that the read and print works.

My problem is as I mentioned before: When I read the button and send it via the BT module to light a LED on the other unit. It only seems to work for 5-10s then it seems like the BT stops reading. Also the values I read are 76 and 48-49 when sending 1 and 0.

Do you still think I should go to arduino <--> Mac?

firmaj:
I was just about to start but don't get why it would do me any good.

If I am to provide any useful help I need to start from a working baseline.

If you already have a version that works with 2 Bluesmirfs post the code for that and all the connection details.

And then show the code that is causing a problem.

...R

ok, Robin2, lets start from the beginning with me giving all information :slight_smile: (Stupid that I didn't share the circuits from the beginning... sorry about that.

I tried my best to to these in an online circuit builder, tell me if you want me to do a schematic instead. I'd be happy to do it.

The modules connect fine to each other (both light up green) but I have get two problems that I don't get:

1 - the value i read from the bluetooth is appr. 48 when the button on the slave is NOT pushed and appear. 76 when pushed. I have no idea why.

2 - The values stop updating in the serial monitor i.e it doesn't read the bluetooth after a couple of seconds (5-10s i would guess). I tried to put in a count (count++; Serial.println(count) ) and showed that the read (or print from the other module, I have no idea) stops after 6500-700 iterations

So here are the connection details and codes:

MASTER

SLAVE

And the codes

MASTER

#include <SoftwareSerial.h> 

int LED_RED = 6;
int LED_YELLOW = 7;

int ledPin = 12;

int value_BT = 0;

int bluetoothTx = 9;  // TX-O pin of Bluesmirf silver, Arduino D9
int bluetoothRx = 10;  // RX-I pin of Bluesmirf silver, Arduino D10

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_RED, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(bluetoothTx, INPUT);
  pinMode(bluetoothRx, OUTPUT);

///Indicating that this is the master
  led_indicate_master();

  Serial.begin(115200);
  bluetooth.begin(115200);
 
  bluetooth.print("$$");
  delay(100);
  bluetooth.println("SM,1");
  delay(100);
  bluetooth.println("C,000666643C3D");
  delay(100);
  bluetooth.println("---");
  

  Serial.println("Debugging works");

}

void loop() {
  if (bluetooth.available() > 0) {
    value_BT = bluetooth.read();
    Serial.println(value_BT);
  }
}


void led_indicate_master() {
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
}

SLAVE

#include <SoftwareSerial.h> 

int LED_RED = 5;
int LED_GREEN = 6;

int value_BT = 0;

int ledPin = 11;
int buttonPin = 12;
int buttonState = 0;

int bluetoothTx = 9;  // TX-O pin of Bluesmirf silver, Arduino D9
int bluetoothRx = 10;  // RX-I pin of Bluesmirf silver, Arduino D10

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(bluetoothTx, INPUT);
  pinMode(bluetoothRx, OUTPUT);
  
///Indicating that this is the slave
  led_indicate_slave();
  
  bluetooth.begin(115200);
  bluetooth.print("$$");
  delay(100);
  bluetooth.println("SM,0");  
  delay(100);
  bluetooth.println("---");
  
  pinMode(ledPin, OUTPUT);

}

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    value_BT = 1;
    digitalWrite(ledPin, HIGH);
    bluetooth.print(value_BT);
  } else {
    value_BT = 0;
    digitalWrite(ledPin, LOW);
    bluetooth.print(value_BT);
  }
}


void led_indicate_slave() {
  digitalWrite(LED_GREEN,HIGH);
  digitalWrite(LED_RED,HIGH);
  delay(5000);
  digitalWrite(LED_GREEN,LOW);
  digitalWrite(LED_RED,LOW);
  delay(50);
}

firmaj:
ok, Robin2, lets start from the beginning with me giving all information

Thanks for all that. I will have a careful look at your code later today when I have some more time. In the meantime can you confirm that the code you have posted is the code that you referred to in Reply #19 as "successfully connected the two bluesmirfs which would suggest that the read and print works"
And (now that I have read that quotation more closely) can you confirm whether you have actually sent and received data with that program.

I have had a quick look at the code. I don't see how it can possibly work because it is trying to use SoftwareSerial at 115200 baud. I'm pretty sure I made that point earlier.

We need to start with a piece of code that actually works. One of your Arduinos has Serial1 (HardwareSerial) which will work at 115200 baud if that is essential. Start (as I said earlier) with some code that makes that Arduino work in conjunction with your PC.

When I say "something that works" I mean the ability to send and receive text - eg "Hello World"

If you think what I am saying is wrong (it would not be the first time) please explain your reasoning and provide the evidence.


Your diagrams seem to be clear. Many of the same type are not and a photo of a pencil-drawn schematic is usually the best way to show connections.

...R

Yes you did mention that earlier. I think I disregarded it because I thought all was good as it connected fine with the other bluetooth. But i'll try to take the default baud rate of the Bluesmirf modules down to 9600 and software serial.

I'll be back with the progress!