1 Reply Latest reply on May 24, 2015 3:04 AM by ericLeongZc

    HTTP request doesn't repeat itself

    ericLeongZc

      HI there, I am doing an HTTP request repeating to an webclient server. But what I get is one the http request is done 1 time.

       

      this is the code that i am using, I deleted the client.disconnect.

       

      /*

          Carriots.com

          Created 27 Dec 2012

       

       

          This sketch sends streams to Carriots according to the values read by a LDR sensor

       

       

      */

      #include <SPI.h>

      #include <Ethernet.h>

       

       

       

       

      const String APIKEY = "be3aa593d7316149b57a1d49f1813f8223542bdfb632e1f770e544d162287dd4"; // Replace with your Carriots apikey

      const String DEVICE = "Sensors@eric3231559.eric3231559"; // Replace with the id_developer of your device

       

       

      // Enter a MAC address for your controller below.

      // Newer Ethernet shields have a MAC address printed on a sticker on the shield

      byte mac[] = {   0x98, 0x4F, 0xEE, 0x01, 0xBE, 0x0A  };

      IPAddress server(82,223,244,60);  // api.carriots.com IP Address

      EthernetClient client; // Initialize the library instance

       

       

      int txt = 0;

       

       

       

       

      // The setup routine runs once when you press reset

      void setup() {

        Serial.begin(9600);             // Start serial port

       

      delay(5000);

        Serial.println(F("Starting"));

       

       

        Ethernet.begin(mac);         // Start the Ethernet connection

        delay(5000);                    // Give the Ethernet shield a second to initialize

      }

       

       

      // The loop routine runs over and over again forever

      void loop() {

       

       

        sendStream();

        delay(1000);

        while (client.available()) {

          char c = client.read();

          Serial.print(c);

        }

       

       

       

       

       

        delay(5000);

      }

       

       

      // Send stream to Carriots

      void sendStream()

      {

        txt++;

         if (client.connect(server, 80)) {   // If there's a successful connection

          Serial.println(F("connected"));

          // Build the data field

          String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":\"now\",\"data\":{\"light\":"+txt+"}}";

       

       

          // Make a HTTP request

          client.println("POST /streams HTTP/1.1");

          client.println("Host: api.carriots.com");

          client.println("Accept: application/json");

          client.println("User-Agent: Arduino-Carriots");

          client.println("Content-Type: application/json");

          client.print("carriots.apikey: ");

          client.println(APIKEY);

          client.print("Content-Length: ");

          int thisLength = json.length();

          client.println(thisLength);

          client.println("Connection: close");

          client.println();

       

       

          client.println(json);

        }

        else {

          // If you didn't get a connection to the server:

          Serial.println(F("connection failed"));

          Ethernet.begin(mac);

        }

       

       

       

       

      }

         

      What i can see in serial monitor is this..

       

      Starting

      connected

      HTTP/1.1 200 OK

      Date: Sun, 24 May 2015 06:05:41 GMT

      Content-Type: application/vnd.carriots.api.v2+json

      Content-Length: 20

      Connection: close

      Server: Carriots REST API

      Access-Control-Allow-Origin: *

      Access-Control-Allow-Credentials: true

      Access-Control-Expose-Headers: Date,Server,Allow,Connection,Content-Length,Content-Type,Cache-Control

       

       

      { "response": "OK" }connection failed

      connection failed

      connection failed

      connection failed

      connection failed

      connection failed

       

       

      i need help.