Advertisement
phoenixdigital

Sample Particle IO Sensor to Splunk HTTP Event Collector

Oct 16th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. // This #include statement was automatically added by the Particle IDE.
  2. #include "OneWire.h"
  3.  
  4. // Temperature Sensor Library
  5. #include "spark-dallas-temperature.h"
  6.  
  7. // Humidity Sensor Library
  8. #include "Adafruit_DHT.h"
  9.  
  10. // Library for sending HTTP Requests
  11. #include "application.h"
  12. #include "HttpClient.h"
  13.  
  14. // Define Send Data Frequency Seconds
  15. #define DATA_FREQ 60
  16.  
  17. // Define PIR pin
  18. #define PIRPIN A0
  19.  
  20. // Define Audio Sensor Pin
  21. #define AUDIOPIN 2
  22.  
  23. // Define DHT Pin
  24. #define DHTPIN 1     // what pin we're connected to
  25. #define DHTTYPE DHT22       // DHT 22 (AM2302)
  26. DHT dht(DHTPIN, DHTTYPE);
  27.  
  28. // Define the Air Quality Pins
  29. #define AIRLED 3
  30. #define AIRINPUT A1
  31.  
  32. // Data wire is plugged into port 0 on the Particle
  33. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  34. OneWire oneWire(D0 );
  35.  
  36. // Pass our oneWire reference to Dallas Temperature sensor.
  37. DallasTemperature dallas(&oneWire);
  38.  
  39. // Create a variable that will store the temperature value
  40. double temperature = 0.0;
  41. double temperatureF = 0.0;
  42.  
  43. // HTTP Request Config
  44. HttpClient http;
  45.  
  46. // Headers currently need to be set at init, useful for API keys etc.
  47. http_header_t headers[] = {
  48.     //  { "Content-Type", "application/json" },
  49.     //  { "Accept" , "application/json" },
  50.     { "Accept" , "*/*"},
  51.     { "Authorization", "Splunk 867A2809-1AF9-4200-8916-14FF8B01EC37"},
  52.     { NULL, NULL } // NOTE: Always terminate headers will NULL
  53. };
  54.  
  55. http_request_t request;
  56. http_response_t response;
  57.  
  58. char httpPostPayload[256];
  59.  
  60. int timer;
  61. int pirReadingCache;
  62. int AudioReadingCache;
  63.  
  64. int airQualityCache;
  65.  
  66. void setup()
  67. {
  68.   // Register a Particle variable here (this just optionally sends data to the cloud on particle servers so you can view remotely. Not required for internal config.)
  69.   Particle.variable("temperature", &temperature, DOUBLE);
  70.   Particle.variable("temperatureF", &temperatureF, DOUBLE);
  71.  
  72.   // setup the library
  73.   dallas.begin();
  74.  
  75.   // HTTP Request
  76.   Serial.begin(9600);
  77.  
  78.   timer = 0;
  79.   pirReadingCache = 0;
  80.   AudioReadingCache = 1;
  81.  
  82. }
  83.  
  84. void loop()
  85. {
  86.  
  87.   // increment timer
  88.   timer++;
  89.  
  90.   // ************* Motion and Audio Sensor Code keeping the highest value over a 1 second period****************
  91.   for(int i = 0; i < 200; i += 1)
  92.   {
  93.  
  94.       // Read PID sensor and check if it is higher than previous read
  95.       int pirRead = analogRead(PIRPIN);
  96.       if (pirRead > pirReadingCache)
  97.       {
  98.           pirReadingCache = pirRead;
  99.       }
  100.  
  101.       // Read Audio sensor and check if it is lower than previous read (0 if noise detected signal)
  102.       int audioRead = digitalRead(AUDIOPIN);
  103.       if (audioRead < AudioReadingCache)
  104.       {
  105.           AudioReadingCache = audioRead;
  106.       }
  107.  
  108.       delay(5);
  109.   }
  110.  
  111.   // Do not send data to HTTP Event Collector unless DATA_FREQ seconds have passed (1 second delay performed by PIR loop code above)
  112.   if (timer > DATA_FREQ)
  113.   {
  114.  
  115.     //**************************************
  116.     // Air Quality Measurement
  117.  
  118.     // Turn on LED
  119.     digitalWrite(AIRLED, HIGH);
  120.  
  121.     // Wait .28 ms
  122.     delay(28);
  123.  
  124.     // Measure Output
  125.     airQualityCache = analogRead(AIRINPUT);
  126.  
  127.     // Turn off LED
  128.     delay(4);
  129.     digitalWrite(AIRLED, LOW);
  130.  
  131.     //**************************************
  132.  
  133.     // Invert Audio Value as it is True when no noise detected and False when triggered by sound.
  134.     if (AudioReadingCache == 0)
  135.     {
  136.       AudioReadingCache = 1;
  137.     }
  138.     else{
  139.       AudioReadingCache = 0;
  140.     }
  141.  
  142.     // ************* DHT Humidity Probe ****************
  143.     float humidity = dht.getHumidity();
  144.     float tempc = dht.getTempCelcius();
  145.     float dewptc = dht.getDewPoint();
  146.  
  147.     // ************* Temperature Probe ****************
  148.     // Request temperature conversion
  149.     dallas.requestTemperatures();
  150.  
  151.     // get the temperature in Celcius
  152.     float tempC = dallas.getTempCByIndex(0);
  153.     // convert to double
  154.     temperature = (double)tempC;
  155.  
  156.     // ************* Write data to HTTP Event Collector ****************
  157.     Serial.println();
  158.     Serial.println("Application>\tWriting to HTTP event Collector.");
  159.     // Request path and body can be set at runtime or at setup.
  160.     request.hostname = "192.168.64.62";
  161.     request.port = 8088;
  162.     request.path = "/services/collector/event";
  163.     sprintf(httpPostPayload, "{\"event\":\"temperature=%f,humidity=%f,hTemp=%f,dewpoint=%f,pid=%d,audio=%d,air=%d\"}",temperature,humidity,tempc,dewptc,pirReadingCache,AudioReadingCache,airQualityCache);
  164.   //  Serial.println("Payload: %s",httpPostPayload);
  165.     request.body = httpPostPayload;
  166.  
  167.     // Get request
  168.     http.post(request, response, headers);
  169.     Serial.print("Application>\tResponse status: ");
  170.     Serial.println(response.status);
  171.  
  172.     Serial.print("Application>\tHTTP Response Body: ");
  173.     Serial.println(response.body);
  174.  
  175.     // reset PIR value
  176.     pirReadingCache = 0;
  177.  
  178.       // Reset Audio detection
  179.     AudioReadingCache = 1;
  180.  
  181.       // reset timer
  182.     timer = 0;
  183.   }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement