ScottG

HTTP POST not robust

Nov 30th, 2013
61
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Sketch isn't very robust.  The HTTP Post to pushingbox seems to get hungup sometimes because
  3. client.stop() isn't regularly executed
  4. */
  5.  
  6. #include <Ethernet.h>
  7. #include <SPI.h>
  8. #include "Tokens.h"  // holds DEVID_FTTEST
  9.  
  10. uint32_t time = 0;
  11.  
  12. byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  13. byte ip[] =  { 192, 168, 216, 50 };   // IP address of Ethernet Shield
  14.  
  15. char serverName[] = "api.pushingbox.com";
  16. char url[] = "/pushingbox";
  17. EthernetClient client; // create a client that connects to Google
  18. boolean lastConnected = false; // State of the connection last time through the main loop
  19.  
  20. byte temp[4];  // hold dummy data
  21.  
  22. void setup()
  23. {  
  24.   Serial.begin(9600);
  25.   delay(1000);
  26.   Serial.println(F("Begin Setup"));
  27.  
  28.   Ethernet.begin(mac, ip);
  29.   delay(1000); // give the Ethernet shield a second to initialize
  30.   Serial.print(F("My IP address: "));
  31.   Serial.println(Ethernet.localIP());
  32. }
  33.  
  34.  
  35. void loop()
  36. {
  37.   static byte cnt;
  38.   int inChar;
  39.   int connectLoop = 0;
  40.  
  41.   if ( millis() > time + 30000)
  42.   {
  43.     client.stop();
  44.     if( client.connect(serverName, 80) )
  45.     {    
  46.       Serial.println(F("Client Connected :)"));
  47.  
  48.       // create some dummy test data
  49.       temp[0] = cnt++;
  50.       temp[1] = 61;
  51.       temp[2] = 92;
  52.       temp[3] = (byte) millis();
  53.       char sensorTemp[10];
  54.       char feedData[150];
  55.       strcpy(feedData, DEVID_FTTEST);
  56.       for(int i=0; i<4; i++)
  57.       {
  58.         sprintf(sensorTemp, "&T%d=%d", i+1, temp[i]);
  59.         strcat(feedData, sensorTemp);
  60.       }
  61.       postRequest(serverName, url, feedData);
  62.       time = millis();
  63.     }
  64.     else
  65.     {
  66.       Serial.println(F("Client connection failed :("));
  67.       client.stop();    
  68.     }
  69.   }
  70.  
  71.   // print the response from server
  72.   // Should get HTTP/1.1 200 OK
  73.   if (client.available())
  74.   {
  75.     char c = client.read();
  76.     Serial.print(c);
  77.   }
  78.  
  79.   // if there's no net connection, but there was one last time
  80.   // through the loop, then stop the client:
  81.   if (!client.connected() && lastConnected)
  82.   {
  83.     Serial.println(F("disconnecting..."));
  84.     client.stop();
  85.   }
  86.   lastConnected = client.connected();
  87.  
  88. }
  89.  
  90. void postRequest(char *hostName, char *url, String feedData)
  91. {
  92.   String buf = "POST " + String(url) + " HTTP/1.1";
  93.  
  94.   Serial.println(F("Data sent to server: "));
  95.   Serial.println(buf);
  96.   Serial.println("Host: " + String(hostName));
  97.   Serial.println("Content-Type: application/x-www-form-urlencoded");
  98.   Serial.println("Content-Length: " + String(feedData.length()));
  99.   Serial.println("");
  100.   Serial.println(feedData);
  101.   Serial.println("");
  102.   Serial.println("");
  103.  
  104.   client.println(buf);
  105.   client.println("Host: " + String(hostName));
  106.   client.println("Content-Type: application/x-www-form-urlencoded");
  107.   client.println("Content-Length: " + String(feedData.length()));
  108.   client.println("");
  109.   client.println(feedData);
  110.   client.println("");
  111.   client.println("");
  112. }
RAW Paste Data