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 the standard input and parses strings received.
For this example the frame will have this structure:
ID_<mote>#Sensor_value
A valid frame can be:
ID_12#239
The objetive os this tutorial is to provide a basic example for frame parsing.
File:
"basic_parser.c"
Compilation on Meshlium:
gcc basic_parser.c -o basic_parser
Usage:
basic_parser
Note:
This code works at 19200 bauds, you can change it on code. Just look for string B19200 and replace 19200 with the speed you need.
/*
* 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) */
#include <ctype.h>
#define MAX 500
#define MAX_SQL 700
main(int argc, char *argv[])
{
int j=0;
int ret;
int mote_id;
int sensor_value;
char valor[100];
char c;
while(1)
{
read(0,&c,1);
fprintf(stderr,"%c",c);
valor[j]=c;
j++;
if((c=='\n') || (j==(99)))
{
valor[j]='\0';
ret=sscanf(valor,"ID_%d#%d",&mote_id,&sensor_value);
if (ret==2)
{
fprintf(stderr,"%s\n%d\n%d\n%d\n",valor,mote_id,sensor_value,ret);
}
else
{
fprintf(stderr,"%s\n%d\n%d\n%d\n",valor,mote_id,sensor_value,ret);
fprintf(stderr,"Bad frame\n");
}
j=0;
valor[j]='\0';
}
}
}
You can download the code of this tutorial.