Esempio n. 1
0
void setup() {
    // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages) 
  WiServer.init(NULL);
  
  Serial.begin(57600);

  // Have the processData function called when data is returned by the server
  getWeather.setReturnFunc(printData);
}
Esempio n. 2
0
void loop(){

  // Check if it's time to get an update
  if (millis() >= updateTime) {
    getWeather.submit();    
    // Get another update one hour from now
    updateTime += 1000 * 60 * 60;
  }
  
  // Run WiServer
  WiServer.server_task();
 
  delay(10);
}
Esempio n. 3
0
/**
 * Handle client communications
 */
void client_task_impl() {

	uip_tcp_appstate_t *app = &(uip_conn->appstate);
	GETrequest *req = (GETrequest*)app->request;

	if (uip_connected()) {

		if (verbose) {
			Serial.print("Connected to ");
			Serial.println(req->hostName);
		}
		app->ackedCount = 0;
		sendRequest();
	}

	// Did we get an ack for the last packet?
	if (uip_acked()) {
		// Record the bytes that were successfully sent
		app->ackedCount += app->sentCount;
		app->sentCount = 0;

		// Check if we're done or need to send more content for this
		// request
		if (app->ackedCount != (int)app->cursor) {
			// Generate the post again to send the next packet of data
			sendRequest();
		}
	}

	if (uip_rexmit()) {
		sendRequest();
	}

 	if (uip_newdata())  {
 		setRXPin(HIGH);

		if (verbose) {
			Serial.print("RX ");
			Serial.print(uip_datalen());
			Serial.print(" bytes from ");
			Serial.println(req->hostName);
		}

		// Check if the sketch cares about the returned data
	 	if ((req->returnFunc) && (uip_datalen() > 0)){
			// Call the sketch's callback function
	 		req->returnFunc((char*)uip_appdata, uip_datalen());
	 	}
 	}

	if (uip_aborted() || uip_timedout() || uip_closed()) {
		if (req != NULL) {
			if (verbose) {
				Serial.print("Ended connection with ");
				Serial.println(req->hostName);
			}

			if (req->returnFunc) {
				// Call the sketch's callback function with 0 bytes to indicate End Of Data
				req->returnFunc((char*)uip_appdata, 0);
			}
			// Remove the request from the connection
			app->request = NULL;
			// Request is no longer active
			req->active = false;
		}
	}
}
Esempio n. 4
0
/**
 * Sends the request for the current connection
 */
void sendRequest() {

	uip_tcp_appstate_t *app = &(uip_conn->appstate);
	GETrequest *req = (GETrequest*)app->request;

	// Reset the virtual buffer
	app->cursor = 0;

	// Indicates if this is a POST request (instead of a GET)
	// Main difference is that POST requests have a body and a
	// callback function to generate said body
	bool isPost = req->body != NULL;

	// Write out the request header
	WiServer.print_P(isPost ? post : get);
	WiServer.print(req->URL);
	WiServer.println_P(http10);

	// Host name
	WiServer.print_P(host);
	WiServer.println(req->hostName);

	// Auth data (if applicable)
	if (req->auth) {
		WiServer.print_P(authBasic);
		WiServer.println(req->auth);
	}

	// User agent (WiServer, of course!)
	WiServer.println_P(userAgent);

	if (isPost) {
		// Since a post has a body after the blank header line, it has to include
		// an accurate content length so that the server knows when it has received
		// all of the body data.
		char* lengthFieldPos; // Cursor position where the content length place holder starts
		char* contentStart; // Start of the body
		char* contentEnd; // End of the body

		// Just form data for now
		WiServer.println_P(contentTypeForm);

		// Content length line (with 4-space placeholder for the value)
		WiServer.println_P(contentLength);
		// Make a note of where the place holder is so we can fill it in later
		lengthFieldPos = app->cursor - 6; // 6 bytes for CR, LF, and 4 spaces

		// Blank line to indicate end of header
		WiServer.println();

		// Body starts here
		contentStart = app->cursor;

		// Print the body preamble if the request has one
		if (req->bodyPreamble) {
			WiServer.print(req->bodyPreamble);
		}

		// Have the sketch provide the body for the POST
		req->body();

		// Body ends here
		contentEnd = app->cursor;

		// Move the cursor back to the content length value and write in the real length
		app->cursor = lengthFieldPos;
		WiServer.print((int)(contentEnd - contentStart));

		// Put the cursor back at the end of the body so that all of the data gets sent
		app->cursor = contentEnd;

	} else {
		// Blank line to indicate end of GET header
		WiServer.println();
	}

	// Send the 'real' bytes in the buffer
	send();
}
/**
 * Handle client communications
 */
void client_task_impl() {

	uip_tcp_appstate_t *app = &(uip_conn->appstate);
	GETrequest *req = (GETrequest*)app->request;

	if (uip_connected()) {

		TCP_client_connected = 1;		// JM - indicate that an end point client connection is in progress
		TCP_new_connection = 1;		// JM - indicate a new connection was made - cleared by application
		#ifdef DEBUG
			DebugPrintFO(f_ct); 	// Serial.print("-->Connected to ");
			DebugPrint((char *)(req->hostName));
        #endif
		app->ackedCount = 0;
		sendRequest();
	}

	// Did we get an ack for the last packet?
	if (uip_acked()) {
		// Record the bytes that were successfully sent
		app->ackedCount += app->sentCount;
		app->sentCount = 0;

		// Check if we're done or need to send more content for this
		// request
		if (app->ackedCount != (int)app->cursor) {
			// Generate the post again to send the next packet of data
			sendRequest();
		}
	}

	if (uip_rexmit()) {
		sendRequest();
	}

 	if (uip_newdata())  {
 		setRXPin(HIGH);

		#ifdef DEBUG
			DebugPrintFO(f_rx);			// Serial.print("\n<--RX ");
			Serial.print(uip_datalen());
			DebugPrintFO(f_bf);			// Serial.print(" bytes from ");
			DebugPrint((char *)req->hostName);
        #endif
		// Check if the sketch cares about the returned data
	 	if ((req->returnFunc) && (uip_datalen() > 0)){
			// Call the sketch's callback function
	 		req->returnFunc((char*)uip_appdata, uip_datalen());
	 	}
 	}

	if (uip_aborted() || uip_timedout() || uip_closed()) {
		if (req != NULL) {
			TCP_client_connected = 0;			// JM end point connection no longer in progress
			TCP_connection_terminated = 1;		// JM indicate a connection attempt or connection terminated
			#ifdef DEBUG
				DebugPrintFO(f_ec);
				DebugPrint((char *)req->hostName);
            #endif

			if (req->returnFunc) {
				// Call the sketch's callback function with 0 bytes to indicate End Of Data
				req->returnFunc((char*)uip_appdata, 0);
			}
			// Remove the request from the connection
			app->request = NULL;
			// Request is no longer active
			req->active = false;
		}
	}
}