Bluetooth programation - nataliacatalan/Arduino-Project GitHub Wiki
To connect the Bluetooth, we started by watching different [blogs] (http://www.martyncurrey.com/arduino-with-hc-05-bluetooth-module-at-mode/) about how to program a Bluetooth sensor. In order to connect them, we have try many options, and the following two were successful.
Types of connection we have checked:
Once we had the Bluetooth connected, we learned that by pressing the button on the board the mode can change form slave to master and vice versa. Some videos where really helpul, like [this] (https://www.youtube.com/watch?v=Vr4cdpsoVEo) or [this] (https://www.youtube.com/watch?v=xwu33aA89qQ).
The problem we had is that once the bluetooth was turned on, it didn't recieve any AT command even if the channel was the correct one according to the maker (9600). As existed the possibility of an error on the fabrication, we investigated a possible code to find which was the correct "communication channel". We found the following code by greekart at Forum Arduino that checks the correct channel, but the result was that no channel was able to connect. The following code is the one we've use to check it.
#include <SoftwareSerial.h>
SoftwareSerial bluetooth_serial(10, 11);
volatile boolean bluetooth_serial_rate_detected = false;
long serial_rates[] = {
9600, 57600, 115200,
19200, 38400, 4800, 2400, 1200, 230400, 250000, 300, 74880
};
bool discover_bluetooth_serial_rate() {
for (int i = 0; i < (sizeof(serial_rates)/sizeof(long)); i++) {
Serial.print(".");
long rate = serial_rates[i];
bluetooth_serial.begin(rate);
bluetooth_serial.write("AT");
bluetooth_serial.flush();
if (bluetooth_serial.readString() == "OK") {
Serial.println("\nBluetooth Serial Rate detected: " + String(rate));
return true;
} else {
bluetooth_serial.end();
}
}
Serial.println("\nBluetooth Serial Rate not detected.");
return false;
}
void setup() {
Serial.begin(9600);
bluetooth_serial_rate_detected = discover_bluetooth_serial_rate();
}
void loop() {
if(bluetooth_serial_rate_detected) {
if (bluetooth_serial.available()) {
String s = "";
char c;
while((c = bluetooth_serial.read()) != -1) {
s += c;
delay(5);
}
Serial.println("Received: " + s);
}
if (Serial.available()) {
String s = "";
char c;
while((c = Serial.read()) != -1) {
s += c;
delay(5);
}
Serial.println("Sent: " + s);
bluetooth_serial.print(s);
}
}
}
So, as there's no possible communication between us and the Bluetooth sensor, we can't program it to connect with the slave sensor.