Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "xively.h"
- #include "xi_err.h"
- #include <stdio.h>
- #include <string>
- #include <termios.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <iostream>
- #include <fstream>
- #include <sys/poll.h>
- #include <sys/unistd.h>
- #include <vector>
- using namespace std;
- #define XI_FEED_ID 1051974661 // set Xively Feed ID (numerical, no quoutes
- #define XI_API_KEY "AJ8ALmfikG9p34xpRhV5YPP1nKjkFlocaHU9gFSRe2OJt3FY" // set Xively API key (double-quoted string)
- #define BAUDRATE B9600
- #define MODEMDEVICE "/dev/ttyACM0"
- #define _POSIX_SOURCE 1 /* POSIX compliant source */
- #define FALSE 0
- #define TRUE 1
- volatile int STOP=FALSE;
- int openPort(std::string portName)
- {
- struct termios tio;
- int mFd=-1;
- // 8N1
- memset(&tio,0,sizeof(tio));
- tio.c_iflag=0;
- tio.c_oflag=0;
- tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
- tio.c_lflag=0;
- tio.c_cc[VMIN]=0;
- tio.c_cc[VTIME]=3;
- mFd=open(portName.c_str(), O_RDWR | O_NONBLOCK);
- cfsetospeed(&tio,B9600); // 115200 baud
- cfsetispeed(&tio,B9600); // 115200 baud
- if(tcsetattr(mFd,TCSANOW,&tio) < 0)
- {
- perror("init_serialport: Couldn't set term attributes");
- return -1;
- }
- else
- cout<<"InitSerialPort: serial port initialized successfully!\n";
- return mFd;
- }
- std::string readData(int serialPort)
- {
- char buffer[1];
- int bytesRead = 0;
- std::string returnString;
- do
- {
- bytesRead = read(serialPort, &buffer, 1);
- if(bytesRead > 0)
- {
- returnString += buffer[0];
- cout<<"t"<<endl;
- }
- }
- while (bytesRead == sizeof(buffer));
- cout<<returnString<<endl;
- return returnString;
- }
- string ReadDataFromSerial()
- {
- int fd,c, res;
- struct termios oldtio,newtio;
- char buf[255];
- fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
- if (fd <0)
- {
- perror(MODEMDEVICE);
- exit(-1);
- }
- tcgetattr(fd,&oldtio); /* save current port settings */
- bzero(&newtio, sizeof(newtio));
- newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
- newtio.c_iflag = IGNPAR;
- newtio.c_oflag = 0;
- /* set input mode (non-canonical, no echo,...) */
- newtio.c_lflag = 0;
- newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
- newtio.c_cc[VMIN] = 5; /* blocking read until 5 chars received */
- tcflush(fd, TCIFLUSH);
- tcsetattr(fd,TCSANOW,&newtio);
- char buffer[1];
- int bytesRead = 0;
- std::string returnString;
- do
- {
- bytesRead = read(fd, &buffer, 1);
- if(bytesRead > 0)
- {
- returnString = buffer[0];
- cout<<".";
- }
- }
- while (returnString!="\x02");
- returnString="";
- STOP=FALSE;
- while (STOP==FALSE) /* loop for input */
- {
- bytesRead = read(fd, &buffer, 1);
- if(bytesRead > 0)
- {
- if (buffer[0]=='\x03')
- {
- STOP=TRUE;
- continue;
- }
- returnString+= buffer[0];
- cout<<buffer[0]<<endl;
- }
- }
- tcsetattr(fd,TCSANOW,&oldtio);
- close(fd);
- return returnString;
- }
- int main()
- {
- vector<string> sensorDataVect;
- while(true)
- {
- cout<<ReadDataFromSerial()<<endl;
- sleep(1);
- }
- /*
- int port=openPort("/dev/ttyACM0");
- string s="";
- s=readData(port);
- string delimiter=",";
- size_t pos=0;
- string token;
- while ((pos=s.find(delimiter))!=string::npos)
- {
- token=s.substr(0,pos);
- sensorDataVect.push_back(token);
- s.erase(0,pos+delimiter.length());
- cout<<token<<endl;
- }
- sensorDataVect.push_back(s);
- close(port);
- cout<<s<<endl;
- */
- /*
- xi_feed_t feed;
- memset( &feed, NULL, sizeof( xi_feed_t ) );
- feed.feed_id = XI_FEED_ID;
- feed.datastream_count = 9;
- feed.datastreams[0].datapoint_count = 1;
- xi_datastream_t* dewpoint_datastream = &feed.datastreams[0];
- strcpy( dewpoint_datastream->datastream_id, "DewPoint" );
- xi_datapoint_t* current_dewpoint = &dewpoint_datastream->datapoints[0];
- feed.datastreams[1].datapoint_count = 1;
- xi_datastream_t* heatindex_datastream = &feed.datastreams[1];
- strcpy( heatindex_datastream->datastream_id, "HeatIndex" );
- xi_datapoint_t* current_heatindex = &heatindex_datastream->datapoints[0];
- feed.datastreams[1].datapoint_count = 1;
- xi_datastream_t* humidity_datastream = &feed.datastreams[2];
- strcpy( humidity_datastream->datastream_id, "Humidity" );
- xi_datapoint_t* current_humidity = &humidity_datastream->datapoints[0];
- feed.datastreams[1].datapoint_count = 1;
- xi_datastream_t* light_datastream = &feed.datastreams[3];
- strcpy( light_datastream->datastream_id, "Light" );
- xi_datapoint_t* current_light = &light_datastream->datapoints[0];
- feed.datastreams[1].datapoint_count = 1;
- xi_datastream_t* outdoortemperature_datastream = &feed.datastreams[4];
- strcpy( outdoortemperature_datastream->datastream_id, "OutdoorTemperature" );
- xi_datapoint_t* current_outdoortemperature = &outdoortemperature_datastream->datapoints[0];
- feed.datastreams[1].datapoint_count = 1;
- xi_datastream_t* rainfall_datastream = &feed.datastreams[5];
- strcpy( rainfall_datastream->datastream_id, "RainFall" );
- xi_datapoint_t* current_rainfall = &rainfall_datastream->datapoints[0];
- feed.datastreams[1].datapoint_count = 1;
- xi_datastream_t* uvindex_datastream = &feed.datastreams[6];
- strcpy( uvindex_datastream->datastream_id, "UV-Index" );
- xi_datapoint_t* current_uvindex = &uvindex_datastream->datapoints[0];
- feed.datastreams[1].datapoint_count = 1;
- xi_datastream_t* winddirection_datastream = &feed.datastreams[7];
- strcpy( winddirection_datastream->datastream_id, "WindDirection" );
- xi_datapoint_t* current_winddirection = &winddirection_datastream->datapoints[0];
- feed.datastreams[1].datapoint_count = 1;
- xi_datastream_t* windspeed_datastream = &feed.datastreams[8];
- strcpy( windspeed_datastream->datastream_id, "WindSpeed" );
- xi_datapoint_t* current_windspeed = &windspeed_datastream->datapoints[0];
- // create the xively library context
- xi_context_t* xi_context
- = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
- // check if everything works
- if( xi_context == NULL )
- {
- return -1;
- }
- xi_set_value_str( current_dewpoint, "123456789" );
- xi_set_value_f32( current_windspeed, 532 );
- xi_feed_update(xi_context, &feed);
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment