element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
RIoTboard
  • Products
  • Dev Tools
  • Single-Board Computers
  • RIoTboard
  • More
  • Cancel
RIoTboard
Blog Riotboard Android bluetooth hc-05 communication
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join RIoTboard to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: tusharp
  • Date Created: 24 Feb 2015 4:53 PM Date Created
  • Views 835 views
  • Likes 0 likes
  • Comments 0 comments
  • android
  • driver
  • bluetooth
  • tusharp
  • Ubuntu
  • freescale
  • imx6
  • mx-6
  • riotboard
  • embedded
  • setup
  • riot
  • blueterm
  • uart
  • cortex-a9
  • hc05
  • hc-05
  • bsp
  • arm
  • arm9
  • linux
Related
Recommended

Riotboard Android bluetooth hc-05 communication

tusharp
tusharp
24 Feb 2015

If you are looking for an inexpensive way to connect your Riotboard to Android then this guide will help you.

 

The Internet is packed with tutorials explaining how to connect various SBC & Arduino to Android handhelds.

 

Here we interface Riotboard to Android using HC-05  uart bluetooth module .

The HC-05 is uart based low-cost bluetooth solution suitable for hobbyist and amateurs.

 

The HC-05 has a default serial baud rate of 9600.

 

1pz6hj.jpg

 

The module has 6pins in total as shown below.

Some modules have a KEY pin instead of WAKEUP , that pin will not be used in this blog.

 

Note: the KEY pin is used to enter HC-05 AT mode by pulling it high before power on the module.

 

2rmxk5l.jpg

 

Next we will be connecting the HC05 to Riotboard using below pin mapping.

xm6782.jpg

below is how it looks after hardware setup.

wlob5d.jpg

 

Next install blueterm in your android phone.

 

With blueterm and hardware setup complete , we need an app that will respond to bluetooth serial commands and control something like LED image.

 

I wrote the below code to switch D45 (user led) on off according to the command set in android.

 

/*

HC05 - Riotboard Application

Author: tusharp@element14.com

*/

 

#include <stdio.h> 

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/wait.h>

#include <fcntl.h> 

#include <termios.h>

#include <errno.h> 

#include <getopt.h>

#include <string.h>

 

#define FALSE 1

#define TRUE 0

 

void set_speed(int fd, int speed)

{

    int   i;

    int   status;

    struct termios   Opt;

    tcgetattr(fd, &Opt);

    tcflush(fd, TCIOFLUSH);

    cfsetispeed(&Opt, B9600);

    cfsetospeed(&Opt, B9600);

    status = tcsetattr(fd, TCSANOW, &Opt);

    if  (status != 0)

    {

        perror("tcsetattr fd1");

        return;

    }

    tcflush(fd,TCIOFLUSH);

}

 

int set_Parity(int fd,int databits,int stopbits,int parity, int flowctrl)

{

    struct termios options;

    if  ( tcgetattr( fd,&options)  !=  0)

    {

        perror("SetupSerial 1");

        return(FALSE);

    }

    options.c_cflag &= ~CSIZE ;

    options.c_cflag |= CS8;

    options.c_cflag &= ~PARENB;

    options.c_iflag &= ~INPCK;

       options.c_cflag &= ~CSTOPB;

    options.c_cflag &= ~CRTSCTS;

       options.c_iflag |= INPCK;

      options.c_cc[VTIME] = 150;

    options.c_cc[VMIN] = 0;

    options.c_lflag &= ~(ECHO | ICANON);

      tcflush(fd,TCIFLUSH);

      if (tcsetattr(fd,TCSANOW,&options) != 0)

    {

        perror("SetupSerial 3");

          return (FALSE);

     }

    return (TRUE);

}

 

int main(int argc, char *argv[])

{

    int  fd, next_option, havearg = 0;

    char *device = "/dev/ttymxc2"; /* Default device */

    int speed = 9600;

    int flowctrl = 0;

     int nread;       

     char buff1[2];

    char buff2[10];

    int counter=0;   

    int c;

 

     sleep(1);

 

    fd = open(device, O_RDWR );

     if (-1 == fd)

    {

           perror("Can't Open Serial Port");

           return -1;

    }

     sleep(1);

 

    set_speed(fd,speed);

 

    if (set_Parity(fd,8,1,'N', flowctrl)== FALSE) {

        fprintf(stderr, "Set Parity Error\n");

        close(fd);

          exit(1);

       }

 

//program core

        counter=0;

        bzero(buff1,sizeof(buff1));

        bzero(buff2,sizeof(buff2));

 

        while(1)

        {

            nread = read(fd, buff1, sizeof(buff1));

 

            if (nread == 1)

            {

                    buff2[counter]=buff1[0];

                    ++counter;

                    c = buff1[0];

                if(c==10)

                {

                    buff2[counter]='\0';

 

                    if(strncmp(buff2,"on",2)==0)

                    {

                        printf("\n ON received\n");

                        system("echo 1 > /sys/class/leds/user_led/brightness");

                    }

                    if(strncmp(buff2,"off",3)==0)

                    {

                        printf("\n OFF received\n");

                        system("echo 0 > /sys/class/leds/user_led/brightness");

                    }

 

                    counter = 0;

                    bzero(buff2,sizeof(buff2));

                }

            }

            else

            {

            }

        }

//program core

 

    close(fd);

    exit(0);

}

 

 

The above source can be found in attachments.

 

Now you have to compile the source and copy the binary to sdcard and run the above application on Riotboard.

I am using the kernel sources available here. To setup this BSP check this and this document.

 

check if we got the uart3 device in terminal :

root@riotboard:~# ls /dev/ttymxc*

zjuhlk.jpg

you will get uart3 as ttymxc2,  which indicates the uart3 device availability.

Run the binary (bluetooth_read) and continue for Android setup.

 

In Android open blueterm & connect to your HC05 device.

(in my case it shows blue2, i renamed the device)

2a0f9lg.jpg

 

Type on & hit ENTER, a message will display with ON received, the D45 led (near 5V power pin) will switch ON.

23r9xz.jpg

 

Type off & hit ENTER, a message will display with OFF received, the D45 led (near 5V power pin) will switch OFF.

2crki0l.jpg

 

So we learned how to command Riotboard from Android phone.

 

You can customise the code to control a Servo or Play a media file on HDMI....

 

Next time we shall capture some real-time sensor data to android image.

Attachments:
bluetooth_read.c.zip
  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube