Sending Data with HC-05 and Trinket M0

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
simonsterbb
 
Posts: 1
Joined: Thu Sep 29, 2022 5:12 pm

Sending Data with HC-05 and Trinket M0

Post by simonsterbb »

I am trying to use the Trinket M0 and the HC-05 to communicate with another Trinket/HC-05. Before getting them to communicate with each other, I am trying to separately send and receive data from my phone to the microcontroller.

I have succeeded in sending a signal from my phone to the Trinket over Bluetooth, but I have not been able to send a signal from the Trinket to my phone. When I try to Serial.write(), the signal is successfully transmitted to the serial monitor, but is not transmitted via the HC-05. The apps I have tried for receiving the signal are "Bluetooth Terminal" and "ArduTooth", which worked for sending the signal. Is there another library I should use? Below is a simplified version of my code.

Code: Select all

#define SWITCH_PIN 2
int pin_value = LOW;
int old_pin_value = LOW;

void setup() {
  pinMode(SWITCH_PIN, INPUT);
  Serial.begin(9600);
}

void loop() {
      pin_value = digitalRead(SWITCH_PIN);
      delay(100);
     
    // If switch flips on, write "a"
    if (pin_value == HIGH && old_pin_value == LOW){
        Serial.write("a");
    } 
    
    // If switch flips off, write "b"
    else if (pin_value == LOW && old_pin_value == HIGH){
        Serial.write("b");
    }

    old_pin_value = pin_value;
}

User avatar
adafruit_support_mike
 
Posts: 67391
Joined: Thu Feb 11, 2010 2:51 pm

Re: Sending Data with HC-05 and Trinket M0

Post by adafruit_support_mike »

You're probably getting caught by buffering: every transmission protocol wraps the data you want to send (aka: 'in-band data' or IBD) in an 'envelope' of protocol data (aka: 'out-of-band data' or OOBD).

Adding a whole envelope's worth of OOBD to every byte of IBD is as inefficient as it's possible to be, so protocols try to be efficient by collecting fixed-size blocks of IBD in a buffer and sending a full buffer per transmission.

The .write() method puts data into the buffer, and the underlying code transmits the buffer when it's full. That means .write()s don't guarantee immediate transmission.

If you want to send less than a full buffer of data, use the .flush() method. That forces the underlying system to transmit and empty the buffer immediately.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”