Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Uncomment the following line to debug, connecting to a local server (SocketTestV3, HW-Group's Herculys, etc)
- //#define DEBUG_LOCAL_HOST
- #define IP_ADDR 0xc0a8018c // 192.168.1.140
- #define PORT_NUM 80
- #define BUF_SIZE 1400
- #define NO_OF_PACKETS 1000
- #define MAX_POST_STRING_SIZE 512
- #define FILENAME "filename\0"
- #define FILEOPTIONS "overwrite\0"
- #define USERNAME "username12\0"
- #define API_KEY "553vuihwpo\0"
- #define NUM_TRACES 2
- #define MAX_POINTS 100
- #define STREAM_TOKEN_1 "t9hm12343s\0"
- #define STREAM_TOKEN_2 "5678q5n4ve\0"
- char stream_tokens_ [ NUM_TRACES ] [ 11 ] = { STREAM_TOKEN_1 , STREAM_TOKEN_2 };
- static _i32 BsdTcpClient(_u16 Port)
- {
- SlSockAddrIn_t Addr;
- _u16 idx = 0;
- _u16 AddrSize = 0;
- _i16 SockID = 0;
- _i16 Status = 0;
- _u16 LoopCount = 0;
- unsigned long DestinationIP = IP_ADDR;
- for (idx=0 ; idx<BUF_SIZE ; idx++)
- {
- uBuf.BsdBuf[idx] = (_u8)(idx % 10);
- }
- #ifndef DEBUG_LOCAL_HOST
- int iRet;
- alt_u8 uWClientIp[4];
- //Use the netapp api to resolve an IP for the requested hostname
- iRet = sl_NetAppDnsGetHostByName("plot.ly", strlen("plot.ly"), &DestinationIP, SL_AF_INET);
- IPAddress__longToArray ( sl_Htonl(DestinationIP), uWClientIp ); // Get IP in form of u32/char*
- #endif
- // IP
- Addr.sin_family = SL_AF_INET;
- Addr.sin_port = sl_Htons((_u16)Port);
- Addr.sin_addr.s_addr = sl_Htonl((_u32)DestinationIP);
- AddrSize = sizeof(SlSockAddrIn_t);
- SockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
- if( SockID < 0 )
- {
- printf(" [TCP Client] Create socket Error \n\r");
- ASSERT_ON_ERROR(SockID);
- //TODO: return -1; // ??
- }
- printf(" Establishing connection with TCP server \n\r");
- Status = sl_Connect(SockID, ( SlSockAddr_t *)&Addr, AddrSize);
- if( Status < 0 )
- {
- sl_Close(SockID);
- printf(" [TCP Client] TCP connection Error \n\r");
- ASSERT_ON_ERROR(Status);
- }
- }
- void openStream ( _i32 socketIDD )
- {
- // print_(F("POST / HTTP/1.1\r\n"));
- // print_(F("Host: arduino.plot.ly\r\n"));
- // print_(F("User-Agent: Arduino\r\n"));
- // print_(F("Transfer-Encoding: chunked\r\n"));
- // print_(F("Connection: close\r\n"));
- // if(convertTimestamp){
- // print_(F("plotly-convertTimestamp: \""));
- // print_(timezone);
- // print_(F("\"\r\n"));
- // }
- // print_(F("\r\n"));
- volatile _i32 strParamLen;
- volatile _i16 Status;
- int maxsize = 256;
- 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";
- strParamLen = strnlen ( PLOTLY_INIT , maxsize);
- Status = sl_Send(socketIDD, PLOTLY_INIT, strParamLen, 0 );
- }
- void postInit ( _i32 socketIDD )
- {
- volatile _i32 strParamLen;
- volatile _i32 i;
- volatile _i16 Status;
- int maxsize = 256;
- char charBuffer [ 32 ];
- alt_16 maxpoints = MAX_POINTS;
- char PLOTLY_PARAM[ MAX_POST_STRING_SIZE ]; //339
- // *** SEND INIT ***
- 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
- strParamLen = strnlen ( PLOTLY_INIT , maxsize); // 137
- Status = sl_Send(socketIDD, PLOTLY_INIT, strParamLen, 0 );
- if( Status < 0 )
- {
- printf(" Error ");
- }
- printf("Transmitting: \r\n\0"); printf(PLOTLY_INIT); printf("\r\n\0");
- // *** PREPARE PARAMTER STRING ***
- strcpy(PLOTLY_PARAM, "version=2.3&origin=plot&platform=arduino&un=\0");
- strcat(PLOTLY_PARAM, USERNAME);
- strcat(PLOTLY_PARAM, "&key=\0");
- strcat(PLOTLY_PARAM, API_KEY);
- strcat(PLOTLY_PARAM, "&args=[\0");
- // print a trace for each token supplied
- for(i=0; i<NUM_TRACES; i++){
- strcat(PLOTLY_PARAM, "{\"y\": [], \"x\": [], \"type\": \"scatter\", \"stream\": {\"token\": \"\0");
- strcat(PLOTLY_PARAM, stream_tokens_[i]);
- strcat(PLOTLY_PARAM, "\", \"maxpoints\": \0");
- itoa( maxpoints , charBuffer , BASE_10 );
- strcat(PLOTLY_PARAM, charBuffer );
- strcat(PLOTLY_PARAM, "}}\0");
- if(NUM_TRACES > 1 && i != NUM_TRACES-1){
- strcat(PLOTLY_PARAM, ", \0");
- }
- }
- strcat(PLOTLY_PARAM, "]&kwargs={\"fileopt\": \"\0");
- strcat(PLOTLY_PARAM, FILEOPTIONS);
- strcat(PLOTLY_PARAM, "\", \"filename\": \"\0");
- strcat(PLOTLY_PARAM, FILENAME );
- strcat(PLOTLY_PARAM, "\", \"world_readable\": \0");
- strcat(PLOTLY_PARAM, "true\0"); // 4 characters for true, 5 for false
- strcat(PLOTLY_PARAM, "}\0");
- // *** SEND CONTENT LENGTH HEADER AND NEW LINES *****
- strParamLen = strnlen ( PLOTLY_PARAM , MAX_POST_STRING_SIZE);
- i = itoa( strParamLen , charBuffer , BASE_10 );
- charBuffer [ i++ ] = 13; charBuffer [ i++ ] = 10; charBuffer [ i++ ] = 13; charBuffer [ i++ ] = 10; charBuffer [ i ] = 0;
- Status = sl_Send(socketIDD, charBuffer, i, 0 );
- // *** SEND PARAM ***
- Status = sl_Send(socketIDD, PLOTLY_PARAM, strParamLen, 0 );
- // Terminate Post with "\r\n"
- charBuffer [ 0 ] = 13; charBuffer [ 1 ] = 10; charBuffer [ 2 ] = 0;
- Status = sl_Send(socketIDD, charBuffer, 2, 0 );
- // Check For Plotly Response,
- /*
- HTTP/1.1 200 OK
- Cache-control: no-cache="set-cookie"
- Content-Language: en
- Content-Type: application/json
- Date: Sat, 28 May 2016 00:09:08 GMT
- Server: nginx/1.8.0
- Set-Cookie: csrftoken=YXh28cKDhaEWLsb87ezxtVwuqqMMrN0X; expires=Mon, 29-May-2017 00:09:08 GMT; Max-Age=31622400; Path=/; secure
- Set-Cookie: anoncsrf=p9FWIjACYRDnqYgghr3oCKu5PQCL63Kg; expires=Sun, 29-May-2016 00:09:08 GMT; httponly; Max-Age=86400; Path=/
- Set-Cookie: AWSELB=296F2D2B16D851992A5FF5CDA5674849B81CD605B11519415FBC954993113ADF0668CCD11E10F9604DC50D23A226DC9AE224F0BD9B8FBB7CD57BC683345C082DE71B16C4DD;PATH=/
- Vary: Accept-Encoding
- Vary: Cookie, Accept-Language
- x-content-type-options: nosniff
- X-Frame-Options: SAMEORIGIN
- x-xss-protection: 1; mode=block
- Content-Length: 177
- Connection: keep-alive
- {"stream-host": "stream.plot.ly", "stream-status": "All Streams Go!", "url": "http://plot.ly/~username/160", "filename": "filename", "warning": "", "error": "", "message": ""}
- */
- Status = sl_Recv(socketIDD, &(uBuf.BsdBuf[0]), BUF_SIZE, 0);
- // ???The Host then Closes the Connection After this transmission??!!
- }
- void plot(_i32 socketIDD, unsigned long x, int y, char *token){
- volatile _i32 strParamLen;
- _i16 i;
- volatile _i16 Status;
- char charBuffer [ 32 ];
- char PLOTLY_PARAM[ MAX_POST_STRING_SIZE ]; //339
- #if 0
- // Re-Connect if Disconnected by sending
- if (!connected()){
- // Close Stream
- print_(F("0\r\n\r\n"));
- client.stop();
- // Open Stream
- while ( !client.connect(arduino.plot.ly, 80) );
- openStream();
- }
- #endif
- // Print the length of the message in hex:
- // 15 char for the json that wraps the data: {"x": , "y": }\n
- // + 23 char for the token: , "token": "abcdefghij"
- // = 38
- // ex.
- //34
- //{"x": 4541, "y": 5000, "streamtoken": "2m5675n4ve"}
- strcpy(PLOTLY_PARAM , "\r\n{\"x\": \0");
- itoa( x , charBuffer , BASE_10 );
- strcat(PLOTLY_PARAM, charBuffer);
- strcat(PLOTLY_PARAM, ", \"y\": \0");
- itoa( y , charBuffer , BASE_10 );
- strcat(PLOTLY_PARAM, charBuffer);
- strcat(PLOTLY_PARAM, ", \"streamtoken\": \"\0"); // strcat(PLOTLY_PARAM, ", \"token\": \"\0");
- strcat(PLOTLY_PARAM, token);
- strcat(PLOTLY_PARAM, "\"}\n\r\n\0");
- strParamLen = strnlen ( PLOTLY_PARAM , MAX_POST_STRING_SIZE);
- i = itoa( strParamLen-4 , charBuffer , BASE_16 ); charBuffer [ i ] = 0; // charBuffer [ i++ ] = 13; charBuffer [ i++ ] = 10; charBuffer [ i ] = 0;
- Status = sl_Send(socketIDD, charBuffer, i, 0 );
- Status = sl_Send(socketIDD, PLOTLY_PARAM, strParamLen, 0 );
- }
- int main (void)
- {
- _i32 retVal = -1;
- _i32 socketIDD;
- char dataStream[64];
- int xVal = 0;
- int yVal = 0;
- /* Initializing the CC3100 device */
- retVal = sl_Start(0, 0, 0);
- /* Connecting to WLAN AP */
- retVal = establishConnectionWithAP();
- /*Before proceeding, make sure there's a valid server waiting on PORT_NUM */
- socketIDD = BsdTcpClient(PORT_NUM);
- // Now Send Pre-Data Stream
- postInit ( socketIDD );
- // Plotly closes connection, so re-open????
- socketIDD = BsdTcpClient(PORT_NUM); ????
- openStream ( socketIDD );
- // *** Once connected to a TCP Server, client should send it packets to the SocketID returned by BsdTcpClient
- while ( 1 )
- {
- xVal++;
- yVal = rand();
- //ex:
- //34
- //{"x": 4541, "y": 5000, "streamtoken": "2z5i44n4ve"}
- plot(socketIDD, 7223, 1000, stream_tokens_[0]);
- plot(socketIDD, 7645, 5000, stream_tokens_[1]);
- DelayMs(20);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement