Bluetooth Communications Problem

SOLVED

In the Arduino code I was required to change

BTserial.print(10)

to

BTserial.print(“10”)

Also I needed to have both Read and Write information as STRINGS rather than BYTES.

I also required using both the standard Bluetooth Client AND the BluetoothLE extension. Using the Bluetooth client allows for a setting to be changed to set read/write strings as UTF-8.

If anyone has the same issues, feel free to PM me for more information.

Hello all, I have a wee issue that is causing me some grief, and unfortunately my Google-Fu is failing me.

I have built a small relay control module with a HM-10 BLE and a NANO, to control three relays via an app on my phone.

I can connect, disconnect, and control all three relays as well as I could hope, however I am seemingly unable to send any information back from the NANO to the app for feedback.

So, This part works fine.

i.e. ‘turn on relay 1’ → ‘relay 1 turns on’

However this part does not.

i.e. ‘relay 1 has turned on, send value to app’ → ‘receive value, change indication to ON’

Does anyone have an idea where I have gone wrong? As shown in the code below I have tried using Print, Write, and Println commands to send the value. All three have no effect.

Also would anyone be able to tell me how the data is likely to be sent over the bluetooth, as binary, string, byte, bit, UTF-8, UTF-16 etc.

Any hints or advice would be greatly appreciated.

Regards.

My arduino code is as follows

//BLUETOOTH RX,8 TX,9
//MUST HAVE ARDUINO_TX TO BT_RX THROUGH A VOLTAGE DIVIDER!!!!! 
//               TX_100OHM_RX_200OHM_GND
//
//RELAY 1 - PIN 4
//RELAY 2 - PIN 5
//RELAY 3 - PIN 6


#include "Arduino.h"
#include <SoftwareSerial.h>
  #define r1 4
  #define r2 5
  #define r3 6
    SoftwareSerial BTserial(8, 9); // RX , TX
      byte LEDpin = 8;

void setup() {
  
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(r1, OUTPUT);
    pinMode(r2, OUTPUT);
    pinMode(r3, OUTPUT);
    digitalWrite(r1, LOW);
    digitalWrite(r2, LOW);
    digitalWrite(r3, LOW);
  
   Serial.begin(9600);
   Serial.println("Set up complete");
  
   BTserial.begin(9600);
   
   delay(100);
 
}

void loop() 
{

 if (BTserial.available() > 0) 
 {
    delay(500);
 char data = (char) BTserial.read();

   switch (data) 
   {
      
      case '0':     
        Serial.println("R1 OFF");
        BTserial.print (10);
        digitalWrite(r1, LOW);        
          digitalWrite(LED_BUILTIN, HIGH);
          delay(250);
          digitalWrite(LED_BUILTIN, LOW);
        break;
          
   case '1':     
   Serial.println("R1 ON");
      BTserial.print (11);
   digitalWrite(r1, HIGH);       
          digitalWrite(LED_BUILTIN, HIGH);
          delay(250);
          digitalWrite(LED_BUILTIN, LOW);
   break;
  
      case '2':      
        Serial.println("R2 OFF");
        BTserial.write(12);
        digitalWrite(r2, LOW);        
          digitalWrite(LED_BUILTIN, HIGH);
          delay(250);
          digitalWrite(LED_BUILTIN, LOW);
        break;
        
      case '3':      
        Serial.println("R2 ON");
        BTserial.write(13);
        digitalWrite(r2, HIGH);        
          digitalWrite(LED_BUILTIN, HIGH);
          delay(250);
          digitalWrite(LED_BUILTIN, LOW);
        break;
  
      case '4':      
        Serial.println("R3 OFF");
        BTserial.println(14);
        digitalWrite(r3, LOW);        
          digitalWrite(LED_BUILTIN, HIGH);
          delay(250);
          digitalWrite(LED_BUILTIN, LOW);
        break;
        
      case '5':      
        Serial.println("R3 ON");
        BTserial.println(15);
        digitalWrite(r3, HIGH);        
          digitalWrite(LED_BUILTIN, HIGH);
          delay(250);
          digitalWrite(LED_BUILTIN, LOW);
        break;  
  
   default:
   Serial.print("NOT RECOGNISED: ");
   Serial.println(data);
   BTserial.print("Error!");
 }
 }
}

Bluetooth_Relay.ino (2.48 KB)

Bluetooth (at least on devices like the HC-05) uses an 8 bit serial protocol.

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The “Code: [Select]” feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

aarg:
Bluetooth (at least on devices like the HC-05) uses an 8 bit serial protocol.

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The “Code: [Select]” feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

My bad, sorry.

Thanks for the prompt correction. We need to know what your app expects as input before advising about what should be sent.

aarg:
Thanks for the prompt correction. We need to know what your app expects as input before advising about what should be sent.

So using App Inventor 2, I am using the WriteBytes function of the BluetoothLE1 extension to send either 0/1, 2/3, 4/5 to the NANO. (R1 ON/OFF R2 ON/OFF R3 ON/OFF)

Then I am attempting the use the ReadBytes function to receive either a 10, 12 or 14 for Relay ON and 11, 13 or 15 for Relay OFF (Three values for R1, R2 and R3 respectively)

The NANO quite happily takes the input and functions correctly, but does not appear to send any signal in return.

0 in ASCII is 0x30

Since Arduino can receive data, your wiring is at least 50% kosher.

FreelancerNZ:
but does not appear to send any signal in return.

BTserial.print(dadedah)
is clearly legitimate code, and there is nothing to suggest Arduino is not sending it. This is likely not an Arduino problem. Have you tried using a standard terminal on the phone?

Another possibility is that HN-10 is not configured for SPP mode, which is probably required for this sort of operation.

ieee488:
0 in ASCII is 0x30

So, maybe I should be looking to receive

'0x31 0x30' instead of '10'?

Nick_Pyner:
Since Arduino can receive data, your wiring is at least 50% kosher. BTserial.print(dadedah)
is clearly legitimate code, and there is nothing to suggest Arduino is not sending it. This is likely not an Arduino problem. Have you tried using a standard terminal on the phone?

Another possibility is that HN-10 is not configured for SPP mode, which is probably required for this sort of operation.

Would you happen to have a link for any documents on SPP mode? I haven't found anything like that in my hunt.

You might try the Martyn Currey website, which is regularly updated. Also here

which may or may not be relevant, as I have no real idea of what they get up to in the iWorld.

Probably more important is your conspicuous lack of response in the matter of using a standard terminal. If you don't, this thread could end up being rather short.

Nick_Pyner:
You might try the Martyn Currey website, which is regularly updated. Also here
https://engineersportal.com/blog/2017/9/20/hm-10-bluetooth-module?rq=hm%2010
which may or may not be relevant, as I have no real idea of what they get up to in the iWorld.

Probably more important is your conspicuous lack of response in the matter of using a standard terminal. If you don't, this thread could end up being rather short.

Yeah I've been reading everything of Martyn Currey I can find, been extremely helpful. In this context what is a 'standard terminal' on a phone?

FreelancerNZ:
what is a 'standard terminal' on a phone?

One that doesn't involve you fartarsing about with AppInventor. There is a swag of them in the Play Store, and I'm sure any of them can do what you propose, thereby proving where your problem isn't. You can receive data OK, but that is usually the hard part(!)