Advertisement
Guest User

sendcarritos-arduino

a guest
May 24th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. #include "SendCarriots.h"
  2. #include <Ethernet.h>
  3.  
  4. String dataString = "";
  5. EthernetClient client;
  6. IPAddress server(82,223,244,60);
  7.  
  8. /**
  9.     Sends an HTTP request to the Carriots system after constructing a JSON
  10.  
  11.     @param array contains the key-value pairs that will be sent in the JSON
  12.     @param elements contains the number of columns in the array
  13.     @param apikey contains the apikey of the Carriots user
  14.     @param device contains the device id of the Carriots user
  15.     @return gives the message sent from the Carriots server
  16. */
  17.  
  18. String SendCarriots::send(String array[][2], int elements, String apikey, String device){
  19.   String key;
  20.   String value;
  21.   String msg;
  22.    //See if the connection is successful
  23.    if (client.connect(server, 80)) {
  24.     Serial.println(F("Sender conectado"));
  25.     //Construct the JSON from the array passed to the method
  26.     dataString = "{\"protocol\":\"v2\",\"checksum\":\"\",\"device\":\"";
  27.     dataString += device;
  28.     dataString += "\",\"at\":\"now\",\"data\":{";
  29.     //Run through the bucle add each key-value in the array to the JSON
  30.     for (int i=0; i<elements;i++){
  31.       key=array[i][0];
  32.       value=array[i][1];
  33.       dataString += "\""+key+"\":";
  34.       dataString += "\""+value+"\"";
  35.       if (elements>1 && i!=elements-1){
  36.          dataString += ",";
  37.       }
  38.     }
  39.     dataString += "}}";
  40.     Serial.println("\n\nDataString:\n"+dataString);
  41.  
  42.     //Make an HTTP request to the Carriots server
  43.     client.println("POST /streams HTTP/1.1");
  44.     client.println("Host: api.carriots.com");
  45.     client.println("Accept: application/json");
  46.     client.println("User-Agent: Arduino-Carriots");
  47.     client.println("Content-Type: application/json");
  48.     client.print("carriots.apikey: ");
  49.     client.println(apikey);
  50.     client.print("Content-Length: ");
  51.     int thisLength = dataString.length();
  52.     client.println(thisLength);
  53.     client.println("Connection: close");
  54.     client.println();
  55.     client.println(dataString);
  56.  
  57.    }
  58.   //Store incoming data from the net connection to send back to the sketch
  59.   while (client.available()) {
  60.       char c = client.read();
  61.       msg += c;
  62.   }
  63.       client.stop();
  64.   return msg;
  65. }
  66.  
  67. String SendCarriots::get(String array[][2], int elements, String apikey, String device){
  68.   String key;
  69.   String value;
  70.   String msg;
  71.    //See if the connection is successful
  72.    if (client.connect(server, 80)) {
  73.     Serial.println(F("Sender conectado"));
  74.     //Construct the JSON from the array passed to the method
  75.     dataString = "{\"sort\":\"at\",\"order\":\"-1\"}\"";
  76.     Serial.println("\n\nDataString:\n"+dataString);
  77.  
  78.     //Make an HTTP request to the Carriots server
  79.     client.println("GET /devices/"+device+"/streams/ HTTP/1.1");
  80.     client.println("Host: api.carriots.com");
  81.     client.println("Accept: application/json");
  82.     client.println("User-Agent: Arduino-Carriots");
  83.     client.println("Content-Type: application/json");
  84.     client.print("carriots.apikey: ");
  85.     client.println(apikey);
  86.     client.print("Content-Length: ");
  87.     int thisLength = dataString.length();
  88.     client.println(thisLength);
  89.     client.println("Connection: close");
  90.     client.println();
  91.     client.println(dataString);
  92.  
  93.    }
  94.   //Store incoming data from the net connection to send back to the sketch
  95.   while (client.available()) {
  96.       char c = client.read();
  97.       msg += c;
  98.   }
  99.       client.stop();
  100.   return msg;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement