hawxgamer

Untitled

Aug 28th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. #include "xively.h"
  2. #include "xi_err.h"
  3.  
  4. #include <stdio.h>
  5. #include <string>
  6. #include <termios.h>
  7. #include <fcntl.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <iostream>
  12. #include <fstream>
  13. #include <sys/poll.h>
  14. #include <sys/unistd.h>
  15. #include <vector>
  16.  
  17. using namespace std;
  18.  
  19. #define XI_FEED_ID 1051974661 // set Xively Feed ID (numerical, no quoutes
  20. #define XI_API_KEY "AJ8ALmfikG9p34xpRhV5YPP1nKjkFlocaHU9gFSRe2OJt3FY" // set Xively API key (double-quoted string)
  21.  
  22.  
  23. int openPort(std::string portName)
  24. {
  25. struct termios tio;
  26. int mFd=-1;
  27.  
  28.  
  29. // 8N1
  30. memset(&tio,0,sizeof(tio));
  31. tio.c_iflag=0;
  32. tio.c_oflag=0;
  33. tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
  34. tio.c_lflag=0;
  35. tio.c_cc[VMIN]=2;
  36. tio.c_cc[VTIME]=5;
  37.  
  38. mFd=open(portName.c_str(), O_RDWR | O_NONBLOCK);
  39. cfsetospeed(&tio,B115200); // 115200 baud
  40. cfsetispeed(&tio,B115200); // 115200 baud
  41.  
  42.  
  43.  
  44. if(tcsetattr(mFd,TCSANOW,&tio) < 0)
  45. {
  46. perror("init_serialport: Couldn't set term attributes");
  47. return -1;
  48. }
  49. else
  50. cout<<"InitSerialPort: serial port initialized successfully!\n";
  51.  
  52. return mFd;
  53. }
  54.  
  55. std::string readData(int serialPort)
  56. {
  57. char buffer[1];
  58. int bytesRead = 0;
  59. std::string returnString;
  60.  
  61. do
  62. {
  63. bytesRead = read(serialPort, &buffer, 1);
  64.  
  65. if(bytesRead > 0)
  66. {
  67. returnString += buffer[0];
  68. }
  69.  
  70. } while (bytesRead == sizeof(buffer));
  71.  
  72. return returnString;
  73. }
  74.  
  75.  
  76.  
  77. int main() {
  78. vector<string> sensorDataVect;
  79.  
  80. int port=openPort("/dev/ttyACM0");
  81.  
  82. string s="";
  83. s=readData(port);
  84.  
  85.  
  86. string delimiter=",";
  87.  
  88. size_t pos=0;
  89.  
  90. string token;
  91. while ((pos=s.find(delimiter))!=string::npos)
  92. {
  93. token=s.substr(0,pos);
  94. sensorDataVect.push_back(token);
  95. s.erase(0,pos+delimiter.length());
  96.  
  97. }
  98. sensorDataVect.push_back(s);
  99.  
  100.  
  101. close(port);
  102. cout<<s<<endl;
  103.  
  104.  
  105.  
  106. xi_feed_t feed;
  107. memset( &feed, NULL, sizeof( xi_feed_t ) );
  108.  
  109. feed.feed_id = XI_FEED_ID;
  110. feed.datastream_count = 9;
  111.  
  112. feed.datastreams[0].datapoint_count = 1;
  113. xi_datastream_t* dewpoint_datastream = &feed.datastreams[0];
  114. strcpy( dewpoint_datastream->datastream_id, "DewPoint" );
  115. xi_datapoint_t* current_dewpoint = &dewpoint_datastream->datapoints[0];
  116.  
  117. feed.datastreams[1].datapoint_count = 1;
  118. xi_datastream_t* heatindex_datastream = &feed.datastreams[1];
  119. strcpy( heatindex_datastream->datastream_id, "HeatIndex" );
  120. xi_datapoint_t* current_heatindex = &heatindex_datastream->datapoints[0];
  121.  
  122. feed.datastreams[1].datapoint_count = 1;
  123. xi_datastream_t* humidity_datastream = &feed.datastreams[2];
  124. strcpy( humidity_datastream->datastream_id, "Humidity" );
  125. xi_datapoint_t* current_humidity = &humidity_datastream->datapoints[0];
  126.  
  127. feed.datastreams[1].datapoint_count = 1;
  128. xi_datastream_t* light_datastream = &feed.datastreams[3];
  129. strcpy( light_datastream->datastream_id, "Light" );
  130. xi_datapoint_t* current_light = &light_datastream->datapoints[0];
  131.  
  132. feed.datastreams[1].datapoint_count = 1;
  133. xi_datastream_t* outdoortemperature_datastream = &feed.datastreams[4];
  134. strcpy( outdoortemperature_datastream->datastream_id, "OutdoorTemperature" );
  135. xi_datapoint_t* current_outdoortemperature = &outdoortemperature_datastream->datapoints[0];
  136.  
  137. feed.datastreams[1].datapoint_count = 1;
  138. xi_datastream_t* rainfall_datastream = &feed.datastreams[5];
  139. strcpy( rainfall_datastream->datastream_id, "RainFall" );
  140. xi_datapoint_t* current_rainfall = &rainfall_datastream->datapoints[0];
  141.  
  142. feed.datastreams[1].datapoint_count = 1;
  143. xi_datastream_t* uvindex_datastream = &feed.datastreams[6];
  144. strcpy( uvindex_datastream->datastream_id, "UV-Index" );
  145. xi_datapoint_t* current_uvindex = &uvindex_datastream->datapoints[0];
  146.  
  147. feed.datastreams[1].datapoint_count = 1;
  148. xi_datastream_t* winddirection_datastream = &feed.datastreams[7];
  149. strcpy( winddirection_datastream->datastream_id, "WindDirection" );
  150. xi_datapoint_t* current_winddirection = &winddirection_datastream->datapoints[0];
  151.  
  152. feed.datastreams[1].datapoint_count = 1;
  153. xi_datastream_t* windspeed_datastream = &feed.datastreams[8];
  154. strcpy( windspeed_datastream->datastream_id, "WindSpeed" );
  155. xi_datapoint_t* current_windspeed = &windspeed_datastream->datapoints[0];
  156.  
  157. // create the xively library context
  158. xi_context_t* xi_context
  159. = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
  160.  
  161. // check if everything works
  162. if( xi_context == NULL )
  163. {
  164. return -1;
  165. }
  166.  
  167. xi_set_value_str( current_dewpoint, "123456789" );
  168.  
  169. xi_set_value_f32( current_windspeed, 532 );
  170.  
  171. xi_feed_update(xi_context, &feed);
  172.  
  173.  
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment