Guest User

Untitled

a guest
May 29th, 2016
178
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Uncomment the following line to debug, connecting to a local server (SocketTestV3, HW-Group's Herculys, etc)
  2. //#define DEBUG_LOCAL_HOST
  3.  
  4. #define IP_ADDR 0xc0a8018c // 192.168.1.140
  5. #define PORT_NUM 80
  6. #define BUF_SIZE 1400
  7. #define NO_OF_PACKETS 1000
  8.  
  9. #define MAX_POST_STRING_SIZE 512
  10. #define FILENAME "filename\0"
  11. #define FILEOPTIONS "overwrite\0"
  12. #define USERNAME "username12\0"
  13. #define API_KEY "553vuihwpo\0"
  14. #define NUM_TRACES 2
  15. #define MAX_POINTS 100
  16. #define STREAM_TOKEN_1 "t9hm12343s\0"
  17. #define STREAM_TOKEN_2 "5678q5n4ve\0"
  18.  
  19. char stream_tokens_ [ NUM_TRACES ] [ 11 ] = { STREAM_TOKEN_1 , STREAM_TOKEN_2 };
  20.  
  21.  
  22.  
  23. static _i32 BsdTcpClient(_u16 Port)
  24. {
  25. SlSockAddrIn_t Addr;
  26. _u16 idx = 0;
  27. _u16 AddrSize = 0;
  28. _i16 SockID = 0;
  29. _i16 Status = 0;
  30. _u16 LoopCount = 0;
  31. unsigned long DestinationIP = IP_ADDR;
  32.  
  33. for (idx=0 ; idx<BUF_SIZE ; idx++)
  34. {
  35. uBuf.BsdBuf[idx] = (_u8)(idx % 10);
  36. }
  37.  
  38. #ifndef DEBUG_LOCAL_HOST
  39. int iRet;
  40. alt_u8 uWClientIp[4];
  41.  
  42. //Use the netapp api to resolve an IP for the requested hostname
  43. iRet = sl_NetAppDnsGetHostByName("plot.ly", strlen("plot.ly"), &DestinationIP, SL_AF_INET);
  44. IPAddress__longToArray ( sl_Htonl(DestinationIP), uWClientIp ); // Get IP in form of u32/char*
  45. #endif
  46. // IP
  47. Addr.sin_family = SL_AF_INET;
  48. Addr.sin_port = sl_Htons((_u16)Port);
  49. Addr.sin_addr.s_addr = sl_Htonl((_u32)DestinationIP);
  50. AddrSize = sizeof(SlSockAddrIn_t);
  51.  
  52. SockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
  53. if( SockID < 0 )
  54. {
  55. printf(" [TCP Client] Create socket Error \n\r");
  56. ASSERT_ON_ERROR(SockID);
  57. //TODO: return -1; // ??
  58. }
  59.  
  60. printf(" Establishing connection with TCP server \n\r");
  61.  
  62. Status = sl_Connect(SockID, ( SlSockAddr_t *)&Addr, AddrSize);
  63. if( Status < 0 )
  64. {
  65. sl_Close(SockID);
  66. printf(" [TCP Client] TCP connection Error \n\r");
  67. ASSERT_ON_ERROR(Status);
  68. }
  69.  
  70. }
  71.  
  72.  
  73.  
  74. void openStream ( _i32 socketIDD )
  75. {
  76. // print_(F("POST / HTTP/1.1\r\n"));
  77. // print_(F("Host: arduino.plot.ly\r\n"));
  78. // print_(F("User-Agent: Arduino\r\n"));
  79. // print_(F("Transfer-Encoding: chunked\r\n"));
  80. // print_(F("Connection: close\r\n"));
  81. // if(convertTimestamp){
  82. // print_(F("plotly-convertTimestamp: \""));
  83. // print_(timezone);
  84. // print_(F("\"\r\n"));
  85. // }
  86. // print_(F("\r\n"));
  87.  
  88. volatile _i32 strParamLen;
  89. volatile _i16 Status;
  90. int maxsize = 256;
  91. const char PLOTLY_INIT[] = "POST / HTTP/1.1\r\nHost: arduino.plot.ly\r\nUser-Agent: Arduino\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n";
  92. strParamLen = strnlen ( PLOTLY_INIT , maxsize);
  93. Status = sl_Send(socketIDD, PLOTLY_INIT, strParamLen, 0 );
  94. }
  95.  
  96.  
  97.  
  98. void postInit ( _i32 socketIDD )
  99. {
  100. volatile _i32 strParamLen;
  101. volatile _i32 i;
  102. volatile _i16 Status;
  103. int maxsize = 256;
  104. char charBuffer [ 32 ];
  105. alt_16 maxpoints = MAX_POINTS;
  106. char PLOTLY_PARAM[ MAX_POST_STRING_SIZE ]; //339
  107.  
  108. // *** SEND INIT ***
  109. const char PLOTLY_INIT[] = "POST /clientresp HTTP/1.1\r\nHost: plot.ly:80\r\nUser-Agent: Arduino/0.6.0\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: "; // Append dec string of strnlen ( PLOTLY_PARAM ); I
  110. strParamLen = strnlen ( PLOTLY_INIT , maxsize); // 137
  111. Status = sl_Send(socketIDD, PLOTLY_INIT, strParamLen, 0 );
  112. if( Status < 0 )
  113. {
  114. printf(" Error ");
  115. }
  116. printf("Transmitting: \r\n\0"); printf(PLOTLY_INIT); printf("\r\n\0");
  117.  
  118. // *** PREPARE PARAMTER STRING ***
  119.  
  120. strcpy(PLOTLY_PARAM, "version=2.3&origin=plot&platform=arduino&un=\0");
  121. strcat(PLOTLY_PARAM, USERNAME);
  122. strcat(PLOTLY_PARAM, "&key=\0");
  123. strcat(PLOTLY_PARAM, API_KEY);
  124. strcat(PLOTLY_PARAM, "&args=[\0");
  125.  
  126. // print a trace for each token supplied
  127. for(i=0; i<NUM_TRACES; i++){
  128. strcat(PLOTLY_PARAM, "{\"y\": [], \"x\": [], \"type\": \"scatter\", \"stream\": {\"token\": \"\0");
  129. strcat(PLOTLY_PARAM, stream_tokens_[i]);
  130. strcat(PLOTLY_PARAM, "\", \"maxpoints\": \0");
  131. itoa( maxpoints , charBuffer , BASE_10 );
  132. strcat(PLOTLY_PARAM, charBuffer );
  133. strcat(PLOTLY_PARAM, "}}\0");
  134. if(NUM_TRACES > 1 && i != NUM_TRACES-1){
  135. strcat(PLOTLY_PARAM, ", \0");
  136. }
  137. }
  138. strcat(PLOTLY_PARAM, "]&kwargs={\"fileopt\": \"\0");
  139. strcat(PLOTLY_PARAM, FILEOPTIONS);
  140. strcat(PLOTLY_PARAM, "\", \"filename\": \"\0");
  141. strcat(PLOTLY_PARAM, FILENAME );
  142. strcat(PLOTLY_PARAM, "\", \"world_readable\": \0");
  143. strcat(PLOTLY_PARAM, "true\0"); // 4 characters for true, 5 for false
  144. strcat(PLOTLY_PARAM, "}\0");
  145.  
  146.  
  147. // *** SEND CONTENT LENGTH HEADER AND NEW LINES *****
  148.  
  149. strParamLen = strnlen ( PLOTLY_PARAM , MAX_POST_STRING_SIZE);
  150. i = itoa( strParamLen , charBuffer , BASE_10 );
  151. charBuffer [ i++ ] = 13; charBuffer [ i++ ] = 10; charBuffer [ i++ ] = 13; charBuffer [ i++ ] = 10; charBuffer [ i ] = 0;
  152. Status = sl_Send(socketIDD, charBuffer, i, 0 );
  153.  
  154. // *** SEND PARAM ***
  155. Status = sl_Send(socketIDD, PLOTLY_PARAM, strParamLen, 0 );
  156.  
  157. // Terminate Post with "\r\n"
  158. charBuffer [ 0 ] = 13; charBuffer [ 1 ] = 10; charBuffer [ 2 ] = 0;
  159. Status = sl_Send(socketIDD, charBuffer, 2, 0 );
  160.  
  161. // Check For Plotly Response,
  162. /*
  163. HTTP/1.1 200 OK
  164. Cache-control: no-cache="set-cookie"
  165. Content-Language: en
  166. Content-Type: application/json
  167. Date: Sat, 28 May 2016 00:09:08 GMT
  168. Server: nginx/1.8.0
  169. Set-Cookie: csrftoken=YXh28cKDhaEWLsb87ezxtVwuqqMMrN0X; expires=Mon, 29-May-2017 00:09:08 GMT; Max-Age=31622400; Path=/; secure
  170. Set-Cookie: anoncsrf=p9FWIjACYRDnqYgghr3oCKu5PQCL63Kg; expires=Sun, 29-May-2016 00:09:08 GMT; httponly; Max-Age=86400; Path=/
  171. Set-Cookie: AWSELB=296F2D2B16D851992A5FF5CDA5674849B81CD605B11519415FBC954993113ADF0668CCD11E10F9604DC50D23A226DC9AE224F0BD9B8FBB7CD57BC683345C082DE71B16C4DD;PATH=/
  172. Vary: Accept-Encoding
  173. Vary: Cookie, Accept-Language
  174. x-content-type-options: nosniff
  175. X-Frame-Options: SAMEORIGIN
  176. x-xss-protection: 1; mode=block
  177. Content-Length: 177
  178. Connection: keep-alive
  179.  
  180. {"stream-host": "stream.plot.ly", "stream-status": "All Streams Go!", "url": "http://plot.ly/~username/160", "filename": "filename", "warning": "", "error": "", "message": ""}
  181. */
  182. Status = sl_Recv(socketIDD, &(uBuf.BsdBuf[0]), BUF_SIZE, 0);
  183.  
  184. // ???The Host then Closes the Connection After this transmission??!!
  185.  
  186. }
  187.  
  188.  
  189.  
  190.  
  191. void plot(_i32 socketIDD, unsigned long x, int y, char *token){
  192. volatile _i32 strParamLen;
  193. _i16 i;
  194. volatile _i16 Status;
  195. char charBuffer [ 32 ];
  196. char PLOTLY_PARAM[ MAX_POST_STRING_SIZE ]; //339
  197.  
  198. #if 0
  199. // Re-Connect if Disconnected by sending
  200. if (!connected()){
  201. // Close Stream
  202. print_(F("0\r\n\r\n"));
  203. client.stop();
  204.  
  205. // Open Stream
  206. while ( !client.connect(arduino.plot.ly, 80) );
  207. openStream();
  208. }
  209. #endif
  210.  
  211. // Print the length of the message in hex:
  212. // 15 char for the json that wraps the data: {"x": , "y": }\n
  213. // + 23 char for the token: , "token": "abcdefghij"
  214. // = 38
  215. // ex.
  216.  
  217. //34
  218. //{"x": 4541, "y": 5000, "streamtoken": "2m5675n4ve"}
  219. strcpy(PLOTLY_PARAM , "\r\n{\"x\": \0");
  220. itoa( x , charBuffer , BASE_10 );
  221. strcat(PLOTLY_PARAM, charBuffer);
  222. strcat(PLOTLY_PARAM, ", \"y\": \0");
  223. itoa( y , charBuffer , BASE_10 );
  224. strcat(PLOTLY_PARAM, charBuffer);
  225. strcat(PLOTLY_PARAM, ", \"streamtoken\": \"\0"); // strcat(PLOTLY_PARAM, ", \"token\": \"\0");
  226. strcat(PLOTLY_PARAM, token);
  227. strcat(PLOTLY_PARAM, "\"}\n\r\n\0");
  228.  
  229. strParamLen = strnlen ( PLOTLY_PARAM , MAX_POST_STRING_SIZE);
  230. i = itoa( strParamLen-4 , charBuffer , BASE_16 ); charBuffer [ i ] = 0; // charBuffer [ i++ ] = 13; charBuffer [ i++ ] = 10; charBuffer [ i ] = 0;
  231. Status = sl_Send(socketIDD, charBuffer, i, 0 );
  232. Status = sl_Send(socketIDD, PLOTLY_PARAM, strParamLen, 0 );
  233. }
  234.  
  235.  
  236.  
  237. int main (void)
  238. {
  239. _i32 retVal = -1;
  240. _i32 socketIDD;
  241. char dataStream[64];
  242. int xVal = 0;
  243. int yVal = 0;
  244.  
  245. /* Initializing the CC3100 device */
  246. retVal = sl_Start(0, 0, 0);
  247.  
  248. /* Connecting to WLAN AP */
  249. retVal = establishConnectionWithAP();
  250.  
  251. /*Before proceeding, make sure there's a valid server waiting on PORT_NUM */
  252. socketIDD = BsdTcpClient(PORT_NUM);
  253.  
  254. // Now Send Pre-Data Stream
  255. postInit ( socketIDD );
  256.  
  257. // Plotly closes connection, so re-open????
  258. socketIDD = BsdTcpClient(PORT_NUM); ????
  259. openStream ( socketIDD );
  260.  
  261. // *** Once connected to a TCP Server, client should send it packets to the SocketID returned by BsdTcpClient
  262. while ( 1 )
  263. {
  264. xVal++;
  265. yVal = rand();
  266.  
  267. //ex:
  268. //34
  269. //{"x": 4541, "y": 5000, "streamtoken": "2z5i44n4ve"}
  270. plot(socketIDD, 7223, 1000, stream_tokens_[0]);
  271. plot(socketIDD, 7645, 5000, stream_tokens_[1]);
  272. DelayMs(20);
  273. }
  274.  
  275. return 0;
  276. }
RAW Paste Data