hawxgamer

Untitled

Sep 9th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 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. #define BAUDRATE B9600
  23. #define MODEMDEVICE "/dev/ttyACM0"
  24. #define _POSIX_SOURCE 1 /* POSIX compliant source */
  25. #define FALSE 0
  26. #define TRUE 1
  27.  
  28. volatile int STOP=FALSE;
  29.  
  30.  
  31. int openPort(std::string portName)
  32. {
  33. struct termios tio;
  34. int mFd=-1;
  35.  
  36.  
  37. // 8N1
  38. memset(&tio,0,sizeof(tio));
  39. tio.c_iflag=0;
  40. tio.c_oflag=0;
  41. tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
  42. tio.c_lflag=0;
  43. tio.c_cc[VMIN]=0;
  44. tio.c_cc[VTIME]=3;
  45.  
  46. mFd=open(portName.c_str(), O_RDWR | O_NONBLOCK);
  47. cfsetospeed(&tio,B9600); // 115200 baud
  48. cfsetispeed(&tio,B9600); // 115200 baud
  49.  
  50.  
  51.  
  52. if(tcsetattr(mFd,TCSANOW,&tio) < 0)
  53. {
  54. perror("init_serialport: Couldn't set term attributes");
  55. return -1;
  56. }
  57. else
  58. cout<<"InitSerialPort: serial port initialized successfully!\n";
  59.  
  60. return mFd;
  61. }
  62.  
  63. std::string readData(int serialPort)
  64. {
  65. char buffer[1];
  66. int bytesRead = 0;
  67. std::string returnString;
  68.  
  69. do
  70. {
  71. bytesRead = read(serialPort, &buffer, 1);
  72.  
  73. if(bytesRead > 0)
  74. {
  75. returnString += buffer[0];
  76. cout<<"t"<<endl;
  77. }
  78.  
  79. }
  80. while (bytesRead == sizeof(buffer));
  81. cout<<returnString<<endl;
  82. return returnString;
  83. }
  84.  
  85. string ReadDataFromSerial()
  86. {
  87. int fd,c, res;
  88. struct termios oldtio,newtio;
  89. char buf[255];
  90.  
  91. fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
  92. if (fd <0)
  93. {
  94. perror(MODEMDEVICE);
  95. exit(-1);
  96. }
  97.  
  98. tcgetattr(fd,&oldtio); /* save current port settings */
  99.  
  100. bzero(&newtio, sizeof(newtio));
  101. newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
  102. newtio.c_iflag = IGNPAR;
  103. newtio.c_oflag = 0;
  104.  
  105. /* set input mode (non-canonical, no echo,...) */
  106. newtio.c_lflag = 0;
  107.  
  108. newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
  109. newtio.c_cc[VMIN] = 5; /* blocking read until 5 chars received */
  110.  
  111. tcflush(fd, TCIFLUSH);
  112. tcsetattr(fd,TCSANOW,&newtio);
  113.  
  114.  
  115. char buffer[1];
  116. int bytesRead = 0;
  117. std::string returnString;
  118.  
  119. do
  120. {
  121. bytesRead = read(fd, &buffer, 1);
  122.  
  123. if(bytesRead > 0)
  124. {
  125. returnString = buffer[0];
  126. cout<<".";
  127. }
  128.  
  129. }
  130.  
  131. while (returnString!="\x02");
  132. returnString="";
  133. STOP=FALSE;
  134.  
  135. while (STOP==FALSE) /* loop for input */
  136. {
  137. bytesRead = read(fd, &buffer, 1);
  138.  
  139. if(bytesRead > 0)
  140. {
  141. if (buffer[0]=='\x03')
  142. {
  143. STOP=TRUE;
  144. continue;
  145. }
  146. returnString+= buffer[0];
  147. cout<<buffer[0]<<endl;
  148. }
  149.  
  150.  
  151. }
  152. tcsetattr(fd,TCSANOW,&oldtio);
  153.  
  154. close(fd);
  155.  
  156. return returnString;
  157. }
  158.  
  159.  
  160.  
  161. int main()
  162. {
  163. vector<string> sensorDataVect;
  164.  
  165. while(true)
  166. {
  167. cout<<ReadDataFromSerial()<<endl;
  168. sleep(1);
  169.  
  170. }
  171.  
  172. /*
  173.  
  174. int port=openPort("/dev/ttyACM0");
  175.  
  176. string s="";
  177. s=readData(port);
  178.  
  179.  
  180. string delimiter=",";
  181.  
  182. size_t pos=0;
  183.  
  184. string token;
  185. while ((pos=s.find(delimiter))!=string::npos)
  186. {
  187. token=s.substr(0,pos);
  188. sensorDataVect.push_back(token);
  189. s.erase(0,pos+delimiter.length());
  190. cout<<token<<endl;
  191.  
  192. }
  193. sensorDataVect.push_back(s);
  194.  
  195.  
  196. close(port);
  197. cout<<s<<endl;
  198. */
  199.  
  200.  
  201. /*
  202.  
  203. xi_feed_t feed;
  204. memset( &feed, NULL, sizeof( xi_feed_t ) );
  205.  
  206. feed.feed_id = XI_FEED_ID;
  207. feed.datastream_count = 9;
  208.  
  209. feed.datastreams[0].datapoint_count = 1;
  210. xi_datastream_t* dewpoint_datastream = &feed.datastreams[0];
  211. strcpy( dewpoint_datastream->datastream_id, "DewPoint" );
  212. xi_datapoint_t* current_dewpoint = &dewpoint_datastream->datapoints[0];
  213.  
  214. feed.datastreams[1].datapoint_count = 1;
  215. xi_datastream_t* heatindex_datastream = &feed.datastreams[1];
  216. strcpy( heatindex_datastream->datastream_id, "HeatIndex" );
  217. xi_datapoint_t* current_heatindex = &heatindex_datastream->datapoints[0];
  218.  
  219. feed.datastreams[1].datapoint_count = 1;
  220. xi_datastream_t* humidity_datastream = &feed.datastreams[2];
  221. strcpy( humidity_datastream->datastream_id, "Humidity" );
  222. xi_datapoint_t* current_humidity = &humidity_datastream->datapoints[0];
  223.  
  224. feed.datastreams[1].datapoint_count = 1;
  225. xi_datastream_t* light_datastream = &feed.datastreams[3];
  226. strcpy( light_datastream->datastream_id, "Light" );
  227. xi_datapoint_t* current_light = &light_datastream->datapoints[0];
  228.  
  229. feed.datastreams[1].datapoint_count = 1;
  230. xi_datastream_t* outdoortemperature_datastream = &feed.datastreams[4];
  231. strcpy( outdoortemperature_datastream->datastream_id, "OutdoorTemperature" );
  232. xi_datapoint_t* current_outdoortemperature = &outdoortemperature_datastream->datapoints[0];
  233.  
  234. feed.datastreams[1].datapoint_count = 1;
  235. xi_datastream_t* rainfall_datastream = &feed.datastreams[5];
  236. strcpy( rainfall_datastream->datastream_id, "RainFall" );
  237. xi_datapoint_t* current_rainfall = &rainfall_datastream->datapoints[0];
  238.  
  239. feed.datastreams[1].datapoint_count = 1;
  240. xi_datastream_t* uvindex_datastream = &feed.datastreams[6];
  241. strcpy( uvindex_datastream->datastream_id, "UV-Index" );
  242. xi_datapoint_t* current_uvindex = &uvindex_datastream->datapoints[0];
  243.  
  244. feed.datastreams[1].datapoint_count = 1;
  245. xi_datastream_t* winddirection_datastream = &feed.datastreams[7];
  246. strcpy( winddirection_datastream->datastream_id, "WindDirection" );
  247. xi_datapoint_t* current_winddirection = &winddirection_datastream->datapoints[0];
  248.  
  249. feed.datastreams[1].datapoint_count = 1;
  250. xi_datastream_t* windspeed_datastream = &feed.datastreams[8];
  251. strcpy( windspeed_datastream->datastream_id, "WindSpeed" );
  252. xi_datapoint_t* current_windspeed = &windspeed_datastream->datapoints[0];
  253.  
  254. // create the xively library context
  255. xi_context_t* xi_context
  256. = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
  257.  
  258. // check if everything works
  259. if( xi_context == NULL )
  260. {
  261. return -1;
  262. }
  263.  
  264. xi_set_value_str( current_dewpoint, "123456789" );
  265.  
  266. xi_set_value_f32( current_windspeed, 532 );
  267.  
  268. xi_feed_update(xi_context, &feed);
  269. */
  270.  
  271. return 0;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment