(C) HTTP POST from Arduino (WiFly Library)

This snippet will send an HTTP POST from an Arduino using the WiFly library. The library is in early alpha, and is a port from the ethernet shield library. The code here should work with either. The critical part is the size of the data to send, and the line-breaks in the right places.

  1. #include "WiFly.h"
  2. #include "Credentials.h"
  3.  
  4. Client client( "ketnerlake.com", 80 );
  5.  
  6. int looped = 0;
  7.  
  8. void setup()
  9. {
  10.   Serial.begin( 9600 );
  11.   WiFly.begin();
  12.  
  13.   if( !WiFly.join( ssid, passphrase ) )
  14.   {
  15.     Serial.println( "Association failed." );
  16.    
  17.     while( 1 )
  18.     {
  19.       // Hang on failure.
  20.     }
  21.   }
  22. }
  23.  
  24. void loop()
  25. {
  26.   String data = "data=$,WEATHER,79.5,51,59.7,29.366,2.8,0.0,270,0.00,0.00,*";
  27.  
  28.   if( client.connect() )
  29.   {
  30.     Serial.println( data );
  31.  
  32.     client.println( "POST /sensors/intercept.cfm HTTP/1.1" );
  33.     client.println( "Host: www.ketnerlake.com" );
  34.     client.println( "Content-Type: application/x-www-form-urlencoded" );
  35.     client.println( "Connection: close" );
  36.     client.print( "Content-Length: " );
  37.     client.println( data.length() );
  38.     client.println();
  39.     client.print( data );
  40.     client.println();
  41.   }
  42.  
  43.   delay( 5000 );
  44. }
This entry was posted in C and tagged , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?

  • Advertisement

  • Categories