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)
- 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]=2;
- tio.c_cc[VTIME]=5;
- mFd=open(portName.c_str(), O_RDWR | O_NONBLOCK);
- cfsetospeed(&tio,B115200); // 115200 baud
- cfsetispeed(&tio,B115200); // 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];
- }
- } while (bytesRead == sizeof(buffer));
- return returnString;
- }
- int main() {
- vector<string> sensorDataVect;
- 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());
- }
- 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