How to change Bluetooth HM-10 name by receiving new name from Bluetooth

The code I write seems to be attempting to receive a new Bluetooth device name from the Bluetooth HM-10 and then change the name of the HM-10 Bluetooth module accordingly. However, there might be some issues in the code that could be affecting its functionality.

#include <SoftwareSerial.h>
#include <Stream.h>
#include <Print.h>
SoftwareSerial hm10(4, 5); // RX, TX pins of Arduino connected to RX, TX pins of HM-10 module
#define blePo 7
bool flag = true;
void setup() {
  Serial.begin(38400);
  hm10.begin(9600); // HM-10 module baud rate

  delay(1000);
  pinMode(blePo,OUTPUT);
  digitalWrite(blePo,HIGH);
  Serial.println("Sending AT commands to change Bluetooth device name...");
  // hm10.write("AT+NAMEKUNAL\r\n"); // Replace with the name you want
  delay(1000);
  Serial.println("Bluetooth device name changed successfully!");
}

void loop() {
  // Your main code goes here
  
  // 
  String name = "AT+NAME";
  // delay(1000);
  if(hm10.available() && flag){
    // Serial.println("IN");
    // flag = false;
    while(hm10.available()){
      char c = char(hm10.read());
      name += c;
      delay(10);
      // hm10.write(Serial.read());
    }
    Serial.println(name);
  }
  // Serial.println(name);
  delay(1000);
  if(name.length() > 8){
    // flag = false;
    Serial.println("HII");
    char buf[30];
    name.toCharArray(buf, name.length()+1);
    Serial.println(buf);
    digitalWrite(blePo,LOW);
    digitalWrite(4,LOW);
    digitalWrite(5,LOW);
    hm10.end();
    delay(1000);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(blePo,HIGH);
    hm10.begin(9600);
    delay(15000);
    // int n = name.length()+1;
    hm10.write(buf);
    delay(1000);
    Serial.println(buf);
  }
}
//

This is my code which is able to receive the new device name from hm-10 but not able to change the device name.. plz suggest changes…

Leave a Comment