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"
- // This #include statement was automatically added by the Particle IDE.
- #include "spark-dallas-temperature.h"
- // Library for sending HTTP Requests
- #include "application.h"
- #include "HttpClient.h"
- // Data wire is plugged into port 0 on the Arduino
- // 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.
- 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[128];
- void setup()
- {
- // Register a Particle variable here
- Particle.variable("temperature", &temperature, DOUBLE);
- Particle.variable("temperatureF", &temperatureF, DOUBLE);
- // setup the library
- dallas.begin();
- // HTTP Request
- Serial.begin(9600);
- }
- void loop()
- {
- // Request temperature conversion
- dallas.requestTemperatures();
- // get the temperature in Celcius
- float tempC = dallas.getTempCByIndex(0);
- // convert to double
- temperature = (double)tempC;
- // convert to Fahrenheit
- float tempF = DallasTemperature::toFahrenheit( tempC );
- // convert to double
- temperatureF = (double)tempF;
- Serial.println();
- Serial.println("Application>\tStart of Loop.");
- // 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,temperature_f=%f\"}",temperature, temperatureF);
- 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);
- delay(60000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement