NOTE: This is an old article. Please go to the Meshlium development section in order to get the latest tutorials.

Receiving Bluetooth sensor data frames in Meshlium

This code example has been tested and improved to be executed in Meshlium with the data coming from a Wireless Sensor Network created with Waspmote.


This program reads through a serial port the information incoming from a Bluetooth Serial Port device.


File:
"sniffer.c"

Compilation on Meshlium:
gcc sniffer.c -o sniffer

Usage:
sniffer rfcomm0 38400

Note:
Serial port speed should be set on sniffer execution.


Code



/*
 *  Copyright (C) 2008 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Version 0.1
 *  Author: Octavio Benedi Sanchez
 */


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <termios.h> /* Terminal control library (POSIX) */

#define MAX 100

int main(int argc, char *argv[]) {
    int sd = 3;
    char *serialPort = "";
    char *BTserialPort0 = "/dev/rfcomm0";
    char *BTserialPort1 = "/dev/rfcomm1";
    char *BTserialPort2 = "/dev/rfcomm2";
    char valor[MAX] = "";
    char c;
    struct termios opciones;
    char *bt0 = "rfcomm0";
    char *bt1 = "rfcomm1";
    char *bt2 = "rfcomm2";

    int speed = B19200;

    typedef struct {
        char *name;
        int flag;
    } speed_spec;

    speed_spec speeds[] ={
        {"1200", B1200},
        {"2400", B2400},
        {"4800", B4800},
        {"9600", B9600},
        {"19200", B19200},
        {"38400", B38400},
        {"57600", B57600},
        {"115200", B115200},
        {NULL, 0}
    };

    if ((argc != 2) && (argc != 3)) {
        fprintf(stderr, "Usage: %s port\nOr: %s port speed\nDefault speed value is 19200\nAllowed ports: rfcomm0 rfcomm1 rfcomm2\nAllowed speed values: 1200 2400 4800 9600 19200 38400 57600 115200\n", argv[0], argv[0]);
        fprintf(stderr, "Author: Octavio Benedi Sanchez\n");
        exit(0);
    }
    if (argc == 3) {
        speed_spec *s;
        for (s = speeds; s->name; s++) {
            if (strcmp(s->name, argv[2]) == 0) {
                speed = s->flag;
                fprintf(stderr, "setting speed %s\n", s->name);
                break;
            }
        }
    }

    if (!strcmp(argv[1], bt0)) {
        //fprintf(stderr,"rfcomm0 chosen\n...");
        serialPort = BTserialPort0;
    }
    if (!strcmp(argv[1], bt1)) {
        //fprintf(stderr,"rfcomm1 chosen\n...");
        serialPort = BTserialPort1;
    }
    if (!strcmp(argv[1], bt2)) {
        //fprintf(stderr,"rfcomm2 chosen\n...");
        serialPort = BTserialPort2;
    }
    if (!strcmp(serialPort, "")) {
        fprintf(stderr, "Choose a valid port (rfcomm0, rfcomm1, rfcomm2)\n");
        exit(0);
    }

    if ((sd = open(serialPort, O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1) {
        fprintf(stderr, "Unable to open the serial port %s - \n", serialPort);
        exit(-1);
    } else {
        if (!sd) {
            /*Sometimes the first time you call open it does not return the
             * right value (3) of the free file descriptor to use, for this
             * reason you can set manually the sd value to 3 or call again
             * the open function (normally returning 4 to sd), advised!*/
            sd = open(serialPort, O_RDWR | O_NOCTTY | O_NONBLOCK);
        }
        //fprintf(stderr,"Serial Port open at: %i\n", sd);
        fcntl(sd, F_SETFL, 0);
    }
    tcgetattr(sd, &opciones);
    cfsetispeed(&opciones, speed);
    cfsetospeed(&opciones, speed);
    opciones.c_cflag |= (CLOCAL | CREAD);
    /*No parity*/
    opciones.c_cflag &= ~PARENB;
    opciones.c_cflag &= ~CSTOPB;
    opciones.c_cflag &= ~CSIZE;
    opciones.c_cflag |= CS8;
    /*raw input:
     * making the applycation ready to receive*/
    opciones.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    /*Ignore parity errors*/
    opciones.c_iflag |= ~(INPCK | ISTRIP | PARMRK);
    opciones.c_iflag |= IGNPAR;
    opciones.c_iflag &= ~(IXON | IXOFF | IXANY | IGNCR | IGNBRK);
    opciones.c_iflag |= BRKINT;
    /*raw output
     * making the applycation ready to transmit*/
    opciones.c_oflag &= ~OPOST;
    /*aply*/
    tcsetattr(sd, TCSANOW, &opciones);
    int j = 0;
    while (1) {
        read(sd,&c,1);
        valor[j]=c;
        j++;

        //We start filling the string untill the end of line char arrives
        //or we reach the end of the string. Then we write it on the screen.

        if((c=='\n') || (j==(MAX-1)))
        {
            int x;
            for(x=0;x<j;x++)
            {
                write(2,&valor[x],1);
                valor[x]='\0';
            }
            j=0;
        }
    }
    close(sd);
}

Download code

You can download the code of this tutorial.


© Libelium Comunicaciones Distribuidas S.L.

| Terms of use