Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This #include statement was automatically added by the Particle IDE.
- #include "OneWire.h"
- // Temperature Sensor Library
- #include "spark-dallas-temperature.h"
- // Humidity Sensor Library
- #include "Adafruit_DHT.h"
- // Library for sending HTTP Requests
- #include "application.h"
- #include "HttpClient.h"
- // Define Send Data Frequency Seconds
- #define DATA_FREQ 60
- // Define PIR pin
- #define PIRPIN A0
- // Define Audio Sensor Pin
- #define AUDIOPIN 2
- // Define DHT Pin
- #define DHTPIN 1 // what pin we're connected to
- #define DHTTYPE DHT22 // DHT 22 (AM2302)
- DHT dht(DHTPIN, DHTTYPE);
- // Define the Air Quality Pins
- #define AIRLED 3
- #define AIRINPUT A1
- // Data wire is plugged into port 0 on the Particle
- // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
- OneWire oneWire(D0 );
- // Pass our oneWire reference to Dallas Temperature sensor.
- DallasTemperature dallas(&oneWire);
- // Create a variable that will store the temperature value
- double temperature = 0.0;
- double temperatureF = 0.0;
- // HTTP Request Config
- HttpClient http;
- // Headers currently need to be set at init, useful for API keys etc.
- http_header_t headers[] = {
- // { "Content-Type", "application/json" },
- // { "Accept" , "application/json" },
- { "Accept" , "*/*"},
- { "Authorization", "Splunk 867A2809-1AF9-4200-8916-14FF8B01EC37"},
- { NULL, NULL } // NOTE: Always terminate headers will NULL
- };
- http_request_t request;
- http_response_t response;
- char httpPostPayload[256];
- int timer;
- int pirReadingCache;
- int AudioReadingCache;
- int airQualityCache;
- void setup()
- {
- // 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.)
- Particle.variable("temperature", &temperature, DOUBLE);
- Particle.variable("temperatureF", &temperatureF, DOUBLE);
- // setup the library
- dallas.begin();
- // HTTP Request
- Serial.begin(9600);
- timer = 0;
- pirReadingCache = 0;
- AudioReadingCache = 1;
- }
- void loop()
- {
- // increment timer
- timer++;
- // ************* Motion and Audio Sensor Code keeping the highest value over a 1 second period****************
- for(int i = 0; i < 200; i += 1)
- {
- // Read PID sensor and check if it is higher than previous read
- int pirRead = analogRead(PIRPIN);
- if (pirRead > pirReadingCache)
- {
- pirReadingCache = pirRead;
- }
- // Read Audio sensor and check if it is lower than previous read (0 if noise detected signal)
- int audioRead = digitalRead(AUDIOPIN);
- if (audioRead < AudioReadingCache)
- {
- AudioReadingCache = audioRead;
- }
- delay(5);
- }
- // Do not send data to HTTP Event Collector unless DATA_FREQ seconds have passed (1 second delay performed by PIR loop code above)
- if (timer > DATA_FREQ)
- {
- //**************************************
- // Air Quality Measurement
- // Turn on LED
- digitalWrite(AIRLED, HIGH);
- // Wait .28 ms
- delay(28);
- // Measure Output
- airQualityCache = analogRead(AIRINPUT);
- // Turn off LED
- delay(4);
- digitalWrite(AIRLED, LOW);
- //**************************************
- // Invert Audio Value as it is True when no noise detected and False when triggered by sound.
- if (AudioReadingCache == 0)
- {
- AudioReadingCache = 1;
- }
- else{
- AudioReadingCache = 0;
- }
- // ************* DHT Humidity Probe ****************
- float humidity = dht.getHumidity();
- float tempc = dht.getTempCelcius();
- float dewptc = dht.getDewPoint();
- // ************* Temperature Probe ****************
- // Request temperature conversion
- dallas.requestTemperatures();
- // get the temperature in Celcius
- float tempC = dallas.getTempCByIndex(0);
- // convert to double
- temperature = (double)tempC;
- // ************* Write data to HTTP Event Collector ****************
- Serial.println();
- Serial.println("Application>\tWriting to HTTP event Collector.");
- // Request path and body can be set at runtime or at setup.
- request.hostname = "192.168.64.62";
- request.port = 8088;
- request.path = "/services/collector/event";
- sprintf(httpPostPayload, "{\"event\":\"temperature=%f,humidity=%f,hTemp=%f,dewpoint=%f,pid=%d,audio=%d,air=%d\"}",temperature,humidity,tempc,dewptc,pirReadingCache,AudioReadingCache,airQualityCache);
- // Serial.println("Payload: %s",httpPostPayload);
- request.body = httpPostPayload;
- // Get request
- http.post(request, response, headers);
- Serial.print("Application>\tResponse status: ");
- Serial.println(response.status);
- Serial.print("Application>\tHTTP Response Body: ");
- Serial.println(response.body);
- // reset PIR value
- pirReadingCache = 0;
- // Reset Audio detection
- AudioReadingCache = 1;
- // reset timer
- timer = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement