Friday, October 24, 2014

More Fun with HM 10

I got more curious about how to use the HM 10 BLE(Bluetooth low energy) module and asked myself if it will really be able to pair with arduino and instruct the arduino to do some tasks via an android phone. So did some research if anyone from the internet has posted similar project and I was so lucky that I found one that I thought is easy and would simply answer my question.

The original post can be found through this link: Control RGB lights from Android with Arduino & Bluetooth LE (BLE)

Aside from following the processes exactly as described in the article, I also did some twist to adapt it to what is available in my chest. The first thing I did is to deactivate the ibeacon function by issuing the following AT command:
AT+IBEA0
To check whether ibeacon function is activated or not, this AT command is used:
AT+IBEA? 
HM-10 will respond: OK Get 1|0

Ibeacon is activated if it returns 1 and deactivated when it returns 0.

Here are the materials and components I used:
1. HM10 BLE module
2. Arduino Pro Mini 8Mhz and 3.3V
3. Dupont wires
4. The RS232-TTL Adaptor that I used to upload sketch on the Pro Mini
5. Li-Poly Battery
6. BreadBoard

I will still be using the same connection just like what I did in my previous testing of HM-10 and was described in detail in my previous post. If you haven't read it yet, you may click on this link: HM10 BLE Module with Ibeacon .

With that set-up, all I need is to upload the sketch as described in the reference article but since I don't have an RGB LED, I could just use the LED in the arduino pro mini which is connected to digital pin 13. Digital pin 13 is not a PWM pin so I can only turn it on or off and not able to dim it.

I also downloaded the android app in the reference article and installed on my android phone.

Test Result:
It was awesome!!!

Here's the video of my testing:

This is the sketch that I used:
#include <SoftwareSerial.h> 
int bluetoothTx = 3;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 2;  // RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
const int redPin = 13;
 const int greenPin = 5;
 const int bluePin = 6;
void setup() {
   // initialize serial:
   Serial.begin(9600);
   // make the pins outputs:
   pinMode(redPin, OUTPUT);
   pinMode(greenPin, OUTPUT);
   pinMode(bluePin, OUTPUT);
  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600);
}
void loop() {
   // if there's any serial available, read it:
  
     if(bluetooth.available())  // If the bluetooth sent any characters
  {
  while (bluetooth.available() > 0) {
     // look for the next valid integer in the incoming serial stream:
     int red = bluetooth.parseInt();
     // do it again:
     int green = bluetooth.parseInt();
     // do it again:
     int blue = bluetooth.parseInt();
     // look for the newline. That's the end of your
     // sentence:
     if (bluetooth.read() == '\n') {
       // constrain the values to 0 - 255 and invert
       // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
       red = 255 - constrain(red, 0, 255);
       green = 255 - constrain(green, 0, 255);
       blue = 255 - constrain(blue, 0, 255);
       // fade the red, green, and blue legs of the LED:
       analogWrite(redPin, red);
       analogWrite(greenPin, green);
       analogWrite(bluePin, blue); 
 }
 }
}
}

I am still using the software serial library because that was my previous setup and I wanted to use the original serial port to communicate with the pc when ever it is needed or necessary.

No comments:

Post a Comment