Advertisement
phoenixdigital

Particle IO sending to Splunk HTTP Event Collector (HTTPS)

Oct 12th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 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.  
  80.   g_connected = client.connect(host, g_port);
  81.   if (!g_connected) {
  82.     client.stop();
  83.     // If TCP Client can't connect to host, exit here.
  84.     return;
  85.   }
  86.  
  87.   // build data for POST
  88.   sprintf(httpPostPayload, "{\"event\":\"temperature=%f,temperature_f=%f\"}",temperature, temperatureF);
  89. //  size_t bufsize = top.printTo(httpPostPayload, sizeof(httpPostPayload));
  90.  
  91.   int rc;
  92.   httpsclientSetPath(endpoint);
  93.   Serial.println("httpsClientConnection Sending Payload ");
  94. //  Serial.println("Headers %s",httpRequestContent);
  95.   Serial.println(httpPostPayload);
  96.   if ((rc = httpsClientConnection(httpRequestContent, 128, httpPostPayload)) < 0) {
  97. //  if ((rc = httpsClientConnection(httpRequestContent, 19, "{\"event\":\"this works\"}")) < 0) {
  98.     // TODO: When massive FAIL
  99.     if (g_https_trace) {
  100.       Serial.print("httpsClientConnection Returned ");
  101.       Serial.println(rc);
  102.     }
  103.     httpsclientCleanUp();
  104.   }
  105.   client.stop();
  106.   Serial.println("httpsClientConnection Sent Payload ");
  107.   delay(5000);
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement