Ingo Lohs
Published

Bluetooth HM-10 Module

How to control a component via Bluetooth using an HM-10 module.

BeginnerProtip30 minutes5,603
Bluetooth HM-10 Module

Things used in this project

Hardware components

Bluetooth Module HM-10
×1
Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
LED (generic)
LED (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
4 pieces
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Control your Arduino with BLE HM-10

C/C++
//////////////////////////////////////////////////
//
// Kleines interaktives Demo
// (c) 2016, Michael Stal
//  Nutzung:
//     Sketch auf Arduino laden
//     An Arduino HM-10 anschließen
//     
//     Über BLE App serielle Verbindung aufbauen,
//     "ein" oder "aus" ins Textfeld eingeben, 
//     und LED beobachten.
//
///////////////////////////////////////////////////


// Wir verwenden Software Serial
#define softserial

// Eingebaute LED nutzen:
const int LED  = 13;
const int LED_ext  = A0;


#ifdef softserial
  #include <SoftwareSerial.h>
  const int BTRX = 10;  // 11
  const int BTTX = 11;  // 10
  SoftwareSerial SerialBT(BTRX, BTTX);
#else 
  HardwareSerial SerialBT = Serial1;
#endif


// Die versendete Nachricht:
String msg; 


///////////////////////////////////////////////////
//
// setup
//    Verbindung mit HM-10 aufbauen
//
///////////////////////////////////////////////////

void setup() {
  SerialBT.begin(9600);
  SerialBT.println("Bluetooth-Verbindung steht");
  pinMode(LED, OUTPUT);
}

///////////////////////////////////////////////////
//
// loop
//    In jeder Iteration auf Nachricht warten,
//    Nachricht analysieren,
//    Aktion auslösen (LED ein/aus)
//
///////////////////////////////////////////////////

void loop() {
  if (SerialBT.available()){      // Daten liegen an
     msg = SerialBT.readString(); // Nachricht lesen
     if (msg == "ein") {
         digitalWrite(LED, HIGH);
         digitalWrite(LED_ext, HIGH);
         SerialBT.print("LED an Pin ");
         SerialBT.print(LED);
         SerialBT.println(" ist eingeschaltet!");
      } 
      else
      if (msg == "aus") {
         digitalWrite(LED, LOW);
         digitalWrite(LED_ext, LOW);
         SerialBT.print("LED an Pin ");
         SerialBT.print(LED);
         SerialBT.println(" ist ausgeschaltet!");
      }
      else {
         SerialBT.print("Kommando <");
         SerialBT.print(msg);
         SerialBT.println("> nicht bekannt");
      }
    }
}                                          

Credits

Ingo Lohs

Ingo Lohs

81 projects • 110 followers
I am well over 40 years and come from the middle of Germany.

Comments