Advertisement
phoenixdigital

HTTPS Test

Oct 28th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. // This #include statement was automatically added by the Particle IDE.
  2. #include "OneWire.h"
  3.  
  4. // This #include statement was automatically added by the Particle IDE.
  5. #include "spark-dallas-temperature.h"
  6.  
  7. // Library for sending HTTP Requests
  8. #include "httpsclient-particle.h"
  9.  
  10. const bool g_https_trace = true;  // This controls debug info print to Serial
  11. const char host [] = "192.168.64.62";
  12. const char endpoint [] = "/services/collector/event";
  13. const int g_port = 8088; // DoNOT change this unless you know what's up
  14. static unsigned int freemem;
  15. bool g_https_complete;
  16. uint32 g_bytes_received;
  17.  
  18. TCPClient client;
  19.  
  20. // Replace XXXX...XXX with base64 encoding if your gf username:password
  21. // If you don't know how to generate the base64 encoding go here:
  22. //    http://www.tuxgraphics.org/toolbox/base64-javascript.html
  23. // CAUTION: Do NOT remove/replace the word Basic from the string above,
  24. //          it's part of http standard.
  25. #define GF_JSON_SIZE 300
  26. unsigned char httpRequestContent[] = "POST %s HTTP/1.0\r\n"
  27.   "Authorization: Splunk 867A2809-1AF9-4200-8916-14FF8B01EC37\r\n"
  28.   "Accept: */*\r\n"
  29.   "Content-Length: %d\r\n\r\n%s";
  30.  
  31. // Data wire is plugged into port 0 on the Arduino
  32. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  33. OneWire oneWire(D0 );
  34.  
  35. // Pass our oneWire reference to Dallas Temperature.
  36. DallasTemperature dallas(&oneWire);
  37.  
  38. // Create a variable that will store the temperature value
  39. double temperature = 0.0;
  40. double temperatureF = 0.0;
  41.  
  42. char httpPostPayload[128];
  43.  
  44. void setup()
  45. {
  46.   // Register a Particle variable here
  47.   Particle.variable("temperature", &temperature, DOUBLE);
  48.   Particle.variable("temperatureF", &temperatureF, DOUBLE);
  49.  
  50.   // setup the library
  51.   dallas.begin();
  52.  
  53.   // HTTP Request
  54. //  httpsclientSetup(host, endpoint);
  55.  
  56.   // Setup Serial Debugging
  57.   Serial.begin(9600);
  58.  
  59. }
  60.  
  61. int g_connected;
  62.  
  63. void loop()
  64. {
  65.   // Request temperature conversion
  66.  
  67.   dallas.requestTemperatures();
  68.  
  69.   // get the temperature in Celcius
  70.   float tempC = dallas.getTempCByIndex(0);
  71.   // convert to double
  72.   temperature = (double)tempC;
  73.  
  74.   // convert to Fahrenheit
  75.   float tempF = DallasTemperature::toFahrenheit( tempC );
  76.   // convert to double
  77.   temperatureF = (double)tempF;
  78.  
  79.   // HTTP Request
  80.   httpsclientSetup(host, endpoint);
  81.  
  82.   g_connected = client.connect(host, g_port);
  83.   if (!g_connected) {
  84.     client.stop();
  85.     // If TCP Client can't connect to host, exit here.
  86.     return;
  87.   }
  88.  
  89.   // build data for POST
  90.   sprintf(httpPostPayload, "{\"event\":\"temperature=%f,temperature_f=%f\"}",temperature, temperatureF);
  91. //  size_t bufsize = top.printTo(httpPostPayload, sizeof(httpPostPayload));
  92.  
  93.   int rc;
  94.   httpsclientSetPath(endpoint);
  95.   Serial.println("httpsClientConnection Sending Payload ");
  96. //  Serial.println("Headers %s",httpRequestContent);
  97.   Serial.println(httpPostPayload);
  98.   if ((rc = httpsClientConnection(httpRequestContent, 1000, httpPostPayload)) < 0) {
  99. //  if ((rc = httpsClientConnection(httpRequestContent, 19, "{\"event\":\"this works\"}")) < 0) {
  100.     // TODO: When massive FAIL
  101.     if (g_https_trace) {
  102.       Serial.print("httpsClientConnection Returned ");
  103.       Serial.println(rc);
  104.     }
  105.     httpsclientCleanUp();
  106.   }
  107.  
  108.   Serial.print("httpsClientConnection Returned ");
  109.   Serial.println(rc);
  110.  
  111.   // finish comms
  112.   client.stop();
  113.  
  114.   // close down HTTPS
  115.   httpsclientCleanUp();
  116.  
  117.   Serial.println("httpsClientConnection Sent Payload ");
  118.   delay(5000);
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement