Template for zigbee serial readout
This commit is contained in:
parent
f8567a2bce
commit
021f99a603
|
@ -0,0 +1,21 @@
|
|||
#makefile for serial project
|
||||
#DE TTK - Villamosmérnök BSC - Baranyai Dávid
|
||||
|
||||
#Compiler:
|
||||
CC = gcc
|
||||
|
||||
#Compiler flags:
|
||||
CFLAGS = -lcurl
|
||||
TARGET = serial_zigbee
|
||||
SERIALHANDLER = serialhandler
|
||||
|
||||
all: $(TARGET) $(SERIALHANDLER)
|
||||
|
||||
$(SERIALHANDLER): $(SERIALHANDLER).c $(SERIALHANDLER).h
|
||||
$(CC) -c $(SERIALHANDLER).c -o $(SERIALHANDLER).o
|
||||
|
||||
$(TARGET): $(TARGET).c $(SERIALHANDLER)
|
||||
$(CC) -o $(TARGET) $(TARGET).c $(CFLAGS) $(SERIALHANDLER).o
|
||||
|
||||
clean:
|
||||
$(RM) $(TARGET)
|
|
@ -0,0 +1,161 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <curl/curl.h>
|
||||
#include "serialhandler.h"
|
||||
|
||||
#define ADDRESS "http://127.0.0.1:3000"
|
||||
#define packet_size 28
|
||||
|
||||
char *jsonString =
|
||||
"{\n\
|
||||
\t\"sensorid\": %d\n\
|
||||
}";
|
||||
|
||||
void sigintHandler(int sig_num)
|
||||
{
|
||||
signal(SIGINT, sigintHandler);
|
||||
curl_global_cleanup();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int send_curl_data(char *message)
|
||||
{
|
||||
|
||||
CURL *curl = curl_easy_init();
|
||||
if(curl)
|
||||
{
|
||||
CURLcode res;
|
||||
|
||||
struct curl_slist *headers = NULL;
|
||||
curl_slist_append(headers, "Accept: application/json");
|
||||
curl_slist_append(headers, "Content-Type: application/json");
|
||||
curl_slist_append(headers, "charset: utf-8");
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, ADDRESS);
|
||||
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, message);
|
||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcrp/0.1");
|
||||
/* Set the expected POST size. */
|
||||
//curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(jsonObj));
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
printf("Curl returned with code %d\n", res);
|
||||
if(res != CURLE_OK)
|
||||
fprintf(stderr, "curl_easy_perform() failed: %s\n",
|
||||
curl_easy_strerror(res));
|
||||
|
||||
/* always cleanup */
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
}
|
||||
|
||||
int read_packet(uint16_t size, uint8_t buff[], const int fd)
|
||||
{
|
||||
uint16_t i;
|
||||
|
||||
//find start
|
||||
do{ //olvasás amíg nem a start header(0xFB55) első bájtja
|
||||
//(0x55 a litle endian miatt(kisebb hejiértékü bájt érkezik hamarabb))
|
||||
|
||||
while(! (serialDataAvail(fd) > 0)) //vár amíg nincs adat
|
||||
{
|
||||
usleep(200);
|
||||
}
|
||||
|
||||
buff[0] = serialGetchar (fd);
|
||||
}while(buff[0] != 0x55); //ha start header eleje akkro tovább
|
||||
|
||||
|
||||
while(! (serialDataAvail(fd) > 0)) //vár amíg nincs adat
|
||||
{
|
||||
//thread sleep? kelhet
|
||||
}
|
||||
buff[1] = serialGetchar (fd);
|
||||
if(buff[1] != 0xFB) //nem start header return error
|
||||
return -1;
|
||||
|
||||
|
||||
//ha igen akkor beolvassa a maradék 22 bájtot
|
||||
while(! (serialDataAvail(fd) >= (size-2))) //vár amíg nincs adat
|
||||
{
|
||||
usleep(1);
|
||||
}
|
||||
|
||||
if(serialGetarray(fd, &buff[2], (size-2)) == -1) //beolvas 22 bájtott a bufferbe a header utánra
|
||||
{
|
||||
return -1; //uart hiba
|
||||
}
|
||||
|
||||
if(buff[size-2] != 0xAA | buff[size-1] != 0xFE) //ha az end header nem passzol error
|
||||
return -1;
|
||||
|
||||
//todo crc check
|
||||
|
||||
return 1; //adat beolvasva
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
signal(SIGINT, sigintHandler);
|
||||
uint8_t raw_buff[packet_size + 10]; //packet_size + egy kis tartalék, ha nincs hiba sehol akkor nem kell
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
char *device = "/dev/ttyACM0";
|
||||
int c, index;
|
||||
while((c = getopt(argc, argv, "d:")) != -1)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case 'd':
|
||||
device = (char*)malloc(sizeof(optarg));
|
||||
strcpy(device, optarg);
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
}
|
||||
for (index = optind; index < argc; index++) printf ("Non-option argument %s\n", argv[index]);
|
||||
|
||||
int fd = 0;
|
||||
int connect_retry_count = 0;
|
||||
|
||||
do
|
||||
{
|
||||
break;
|
||||
if (connect_retry_count == 0) //if not the first time
|
||||
{
|
||||
printf("Cannot open %s. Trying again... (%d)\n", device, connect_retry_count);
|
||||
sleep(1000);
|
||||
}
|
||||
fd=serialOpen(device, 115200);
|
||||
connect_retry_count++;
|
||||
} while (fd < 0);
|
||||
int stringSize = strlen(jsonString) + 200;
|
||||
char* jsonObj = (char*)malloc(stringSize); //"{ \"name\" : \"Pedro\" , \"age\" : \"22\" }";
|
||||
snprintf(jsonObj, stringSize, jsonString, 15);
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (1)//if(read_packet(packet_size, &raw_buff[0], fd) != -1)
|
||||
{
|
||||
printf("Sending: %s\n", jsonObj);
|
||||
send_curl_data(jsonObj);
|
||||
sleep(3);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,192 @@
|
|||
#include "serialhandler.h"
|
||||
|
||||
/*
|
||||
* serialOpen:
|
||||
* Open and initialise the serial port, setting all the right
|
||||
* port parameters - or as many as are required - hopefully!
|
||||
*********************************************************************************
|
||||
*/
|
||||
int serialOpen (const char *device, const int baud)
|
||||
{
|
||||
struct termios options ;
|
||||
speed_t myBaud ;
|
||||
int status, fd ;
|
||||
|
||||
switch (baud)
|
||||
{
|
||||
case 50: myBaud = B50 ; break ;
|
||||
case 75: myBaud = B75 ; break ;
|
||||
case 110: myBaud = B110 ; break ;
|
||||
case 134: myBaud = B134 ; break ;
|
||||
case 150: myBaud = B150 ; break ;
|
||||
case 200: myBaud = B200 ; break ;
|
||||
case 300: myBaud = B300 ; break ;
|
||||
case 600: myBaud = B600 ; break ;
|
||||
case 1200: myBaud = B1200 ; break ;
|
||||
case 1800: myBaud = B1800 ; break ;
|
||||
case 2400: myBaud = B2400 ; break ;
|
||||
case 4800: myBaud = B4800 ; break ;
|
||||
case 9600: myBaud = B9600 ; break ;
|
||||
case 19200: myBaud = B19200 ; break ;
|
||||
case 38400: myBaud = B38400 ; break ;
|
||||
case 57600: myBaud = B57600 ; break ;
|
||||
case 115200: myBaud = B115200 ; break ;
|
||||
case 230400: myBaud = B230400 ; break ;
|
||||
case 460800: myBaud = B460800 ; break ;
|
||||
|
||||
default:
|
||||
return -2 ;
|
||||
}
|
||||
|
||||
if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
|
||||
return -1 ;
|
||||
|
||||
fcntl (fd, F_SETFL, O_RDWR) ;
|
||||
|
||||
// Get and modify current options:
|
||||
|
||||
tcgetattr (fd, &options) ;
|
||||
|
||||
cfmakeraw (&options) ;
|
||||
cfsetispeed (&options, myBaud) ;
|
||||
cfsetospeed (&options, myBaud) ;
|
||||
|
||||
options.c_cflag |= (CLOCAL | CREAD) ;
|
||||
options.c_cflag &= ~PARENB ;
|
||||
options.c_cflag &= ~CSTOPB ;
|
||||
options.c_cflag &= ~CSIZE ;
|
||||
options.c_cflag |= CS8 ;
|
||||
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
|
||||
options.c_oflag &= ~OPOST ;
|
||||
|
||||
options.c_cc [VMIN] = 0 ;
|
||||
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
|
||||
|
||||
tcsetattr (fd, TCSANOW | TCSAFLUSH, &options) ;
|
||||
|
||||
ioctl (fd, TIOCMGET, &status);
|
||||
|
||||
status |= TIOCM_DTR ;
|
||||
status |= TIOCM_RTS ;
|
||||
|
||||
ioctl (fd, TIOCMSET, &status);
|
||||
|
||||
usleep (10000) ; // 10mS
|
||||
|
||||
return fd ;
|
||||
}
|
||||
|
||||
/*
|
||||
* serialFlush:
|
||||
* Flush the serial buffers (both tx & rx)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialFlush (const int fd)
|
||||
{
|
||||
tcflush (fd, TCIOFLUSH) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* serialClose:
|
||||
* Release the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialClose (const int fd)
|
||||
{
|
||||
close (fd) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* serialPutchar:
|
||||
* Send a single character to the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialPutchar (const int fd, const unsigned char c)
|
||||
{
|
||||
write (fd, &c, 1) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* serialPuts:
|
||||
* Send a string to the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialPuts (const int fd, const char *s)
|
||||
{
|
||||
write (fd, s, strlen (s)) ;
|
||||
}
|
||||
|
||||
/*
|
||||
* serialPrintf:
|
||||
* Printf over Serial
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialPrintf (const int fd, const char *message, ...)
|
||||
{
|
||||
va_list argp ;
|
||||
char buffer [1024] ;
|
||||
|
||||
va_start (argp, message) ;
|
||||
vsnprintf (buffer, 1023, message, argp) ;
|
||||
va_end (argp) ;
|
||||
|
||||
serialPuts (fd, buffer) ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* serialDataAvail:
|
||||
* Return the number of bytes of data avalable to be read in the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int serialDataAvail (const int fd)
|
||||
{
|
||||
int result ;
|
||||
|
||||
if (ioctl (fd, FIONREAD, &result) == -1)
|
||||
return -1 ;
|
||||
|
||||
return result ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* serialGetchar:
|
||||
* Get a single character from the serial device.
|
||||
* Note: Zero is a valid character and this function will time-out after
|
||||
* 10 seconds.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int serialGetchar (const int fd)
|
||||
{
|
||||
uint8_t x ;
|
||||
|
||||
if (read (fd, &x, 1) != 1)
|
||||
return -1 ;
|
||||
|
||||
return ((int)x) & 0xFF ;
|
||||
}
|
||||
|
||||
int serialGetarray(const int fd, uint8_t out[], unsigned int size)
|
||||
{
|
||||
int i;
|
||||
uint8_t x;
|
||||
if((out == 0) | (fd == 0)) return -1;
|
||||
if(size == 0) return -1;
|
||||
|
||||
for(i=0;i<size;i++)
|
||||
{
|
||||
|
||||
if(read (fd, &out[i], 1) != 1)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0; //nincs hiba
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
#ifndef SerialHandler_h
|
||||
#define SerialHandler_h
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
* serialOpen:
|
||||
* Open and initialise the serial port, setting all the right
|
||||
* port parameters - or as many as are required - hopefully!
|
||||
*********************************************************************************
|
||||
*/
|
||||
int serialOpen (const char *device, const int baud);
|
||||
|
||||
/*
|
||||
* serialFlush:
|
||||
* Flush the serial buffers (both tx & rx)
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialFlush (const int fd);
|
||||
|
||||
/*
|
||||
* serialClose:
|
||||
* Release the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialClose (const int fd);
|
||||
|
||||
/*
|
||||
* serialPutchar:
|
||||
* Send a single character to the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialPutchar (const int fd, const unsigned char c);
|
||||
|
||||
/*
|
||||
* serialPuts:
|
||||
* Send a string to the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialPuts (const int fd, const char *s);
|
||||
|
||||
/*
|
||||
* serialPrintf:
|
||||
* Printf over Serial
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
void serialPrintf (const int fd, const char *message, ...);
|
||||
|
||||
|
||||
/*
|
||||
* serialDataAvail:
|
||||
* Return the number of bytes of data avalable to be read in the serial port
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int serialDataAvail (const int fd);
|
||||
|
||||
|
||||
/*
|
||||
* serialGetchar:
|
||||
* Get a single character from the serial device.
|
||||
* Note: Zero is a valid character and this function will time-out after
|
||||
* 10 seconds.
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
int serialGetchar (const int fd);
|
||||
|
||||
int serialGetarray(const int fd, uint8_t out[], unsigned int size);
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue