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.
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.
Next we will be connecting the HC05 to Riotboard using below pin mapping.
below is how it looks after hardware setup.
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 .
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*
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)
Type on & hit ENTER, a message will display with ON received, the D45 led (near 5V power pin) will switch ON.
Type off & hit ENTER, a message will display with OFF received, the D45 led (near 5V power pin) will switch OFF.
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 .