Hello,
I am trying to use a Bluetooth module DSD Tech HC-08) over a serial port (brand new arduino mega), and then use both the Bluetooth module to communicate with a serial device (on Serial1) once I get the serial ports working. The problem I am facing is the serial ports will transmit data, but will not receive data. The second issue, is that serial1, serial2, and serial3 are not working as well.
I tried the following code to test the serial0 vs serial1 and the code works on serial0 (only transmitting data) but not serial1 (or 2 or 3):
int blePin = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(blePin, OUTPUT);
delay(100);
digitalWrite(blePin, HIGH);
delay(100); // wait for outputs and serial monitor to settle
}
void loop() {
// put your main code here, to run repeatedly:
// serial test
if (digitalRead(blePin) == HIGH) {
// HEX CODE
Serial.write(0x46); // motor controller address
Serial.write(0x43); // device address
Serial.write(0x16); // read/write
Serial.write(0x12); // command ------------------
Serial.write(0x00); // unknown
Serial.write(0x01); // data length
Serial.write(0xF1); // actual data
Serial.write(0x2B); // check sum / data 2nd byte
Serial.write(0x26); // check sum
// Serial.write(0x8B); // check sum for 2 byte data
}
else if (digitalRead(blePin) == LOW) {
// HEX CODE
Serial.write(0x46); // motor controller address
Serial.write(0x43); // device address
Serial.write(0x16); // read/write
Serial.write(0x12); // command ------------------
Serial.write(0x00); // unknown
Serial.write(0x01); // data length
Serial.write(0xF0); // actual data
Serial.write(0x3B); // check sum / data 2nd byte
Serial.write(0x07); // check sum
// Serial.write(0x8B); // check sum for 2 byte data
}
} // end program loop
Serial1 test code:
int blePin = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600);
pinMode(blePin, OUTPUT);
delay(100);
digitalWrite(blePin, LOW);
delay(100); // wait for outputs and serial monitor to settle
}
void loop() {
// put your main code here, to run repeatedly:
// serial test
if (digitalRead(blePin) == HIGH) {
// HEX CODE
Serial1.write(0x46); // motor controller address
Serial1.write(0x43); // device address
Serial1.write(0x16); // read/write
Serial1.write(0x12); // command ------------------
Serial1.write(0x00); // unknown
Serial1.write(0x01); // data length
Serial1.write(0xF1); // actual data
Serial1.write(0x2B); // check sum / data 2nd byte
Serial1.write(0x26); // check sum
// Serial1.write(0x8B); // check sum for 2 byte data
}
else if (digitalRead(blePin) == LOW) {
// HEX CODE
Serial1.write(0x46); // motor controller address
Serial1.write(0x43); // device address
Serial1.write(0x16); // read/write
Serial1.write(0x12); // command ------------------
Serial1.write(0x00); // unknown
Serial1.write(0x01); // data length
Serial1.write(0xF0); // actual data
Serial1.write(0x3B); // check sum / data 2nd byte
Serial1.write(0x07); // check sum
// Serial1.write(0x8B); // check sum for 2 byte data
}
} // end program loop
The code is written to turn a light on or off depending on the pin 10 state, eventually to be controlled by a Bluetooth module. Serial1 does not do anything when the device is connected to those pins, but it will work on the Serial pins.
The second issue is that the serial receiver does not seem to be working on the Mega or my Uno... When I connect the Bluetooth module to the Serial ports and connect. y phone to the. Bluetooth module with a serial terminal app (I've tried 4 apps), I can receive a message on my phone that I type in the computer's serial terminal, but I cannot send anything from my phone
Example code I found online to test my Bluetooth module: (Computer to phone works, phone to computer does not)
// Arduino code to send messages from smartphone to Arduino
String str_ii = "";
int ii_0 = 0;
void setup() {
Serial.begin(9600);
delay(100);
str_ii = "";
delay(500);
}
void loop() {
while (Serial.available()){
char in_char = Serial.read();
if (int(in_char)!=-1 and int(in_char)!=42){
str_ii+=in_char;
}
if (in_char=='\n'){
delay(20);
String msg = "Msg: ";
msg+=str_ii;
Serial.print(msg);
str_ii = "";
}
}
}
String ble_cmd(String cmd_str,String desc_str){
str_ii = "";
unsigned long t1 = millis();
Serial.println(cmd_str);
while (true){
if ((millis()-t1)>2000){ // 2 sec timeout
return "Err";
}
char in_char = Serial.read();
if (int(in_char)==-1 or int(in_char)==42){
continue;
}
if (in_char=='\n'){
return str_ii;
}
str_ii+=in_char;
}
}
Thank you anyone who is available to assist me, it is greatly appreciated!! I haven't used the arduino in a few years so I am a little rough with my coding skills. I've tried many example codes and can't get anything to work.