Advertisement
phoenixdigital

Particle IO sending to Splunk HTTP Event Collector (non enc)

Oct 12th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 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 "application.h"
  9. #include "HttpClient.h"
  10.  
  11. // Data wire is plugged into port 0 on the Arduino
  12. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  13. OneWire oneWire(D0 );
  14.  
  15. // Pass our oneWire reference to Dallas Temperature.
  16. DallasTemperature dallas(&oneWire);
  17.  
  18. // Create a variable that will store the temperature value
  19. double temperature = 0.0;
  20. double temperatureF = 0.0;
  21.  
  22. // HTTP Request Config
  23. HttpClient http;
  24.  
  25. // Headers currently need to be set at init, useful for API keys etc.
  26. http_header_t headers[] = {
  27.     //  { "Content-Type", "application/json" },
  28.     //  { "Accept" , "application/json" },
  29.     { "Accept" , "*/*"},
  30.     { "Authorization", "Splunk 867A2809-1AF9-4200-8916-14FF8B01EC37"},
  31.     { NULL, NULL } // NOTE: Always terminate headers will NULL
  32. };
  33.  
  34. http_request_t request;
  35. http_response_t response;
  36.  
  37. char httpPostPayload[128];
  38.  
  39. void setup()
  40. {
  41.   // Register a Particle variable here
  42.   Particle.variable("temperature", &temperature, DOUBLE);
  43.   Particle.variable("temperatureF", &temperatureF, DOUBLE);
  44.  
  45.   // setup the library
  46.   dallas.begin();
  47.  
  48.   // HTTP Request
  49.   Serial.begin(9600);
  50.  
  51. }
  52.  
  53. void loop()
  54. {
  55.   // Request temperature conversion
  56.  
  57.   dallas.requestTemperatures();
  58.  
  59.   // get the temperature in Celcius
  60.   float tempC = dallas.getTempCByIndex(0);
  61.   // convert to double
  62.   temperature = (double)tempC;
  63.  
  64.   // convert to Fahrenheit
  65.   float tempF = DallasTemperature::toFahrenheit( tempC );
  66.   // convert to double
  67.   temperatureF = (double)tempF;
  68.  
  69.   Serial.println();
  70.   Serial.println("Application>\tStart of Loop.");
  71.   // Request path and body can be set at runtime or at setup.
  72.   request.hostname = "192.168.64.62";
  73.   request.port = 8088;
  74.   request.path = "/services/collector/event";
  75.   sprintf(httpPostPayload, "{\"event\":\"temperature=%f,temperature_f=%f\"}",temperature, temperatureF);
  76.   request.body = httpPostPayload;
  77.  
  78.   // Get request
  79.   http.post(request, response, headers);
  80.   Serial.print("Application>\tResponse status: ");
  81.   Serial.println(response.status);
  82.  
  83.   Serial.print("Application>\tHTTP Response Body: ");
  84.   Serial.println(response.body);
  85.  
  86.   delay(60000);
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement