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 "httpsclient-particle.h"
- const bool g_https_trace = true; // This controls debug info print to Serial
- const char host [] = "192.168.64.62";
- const char endpoint [] = "/services/collector/event";
- const int g_port = 8088; // DoNOT change this unless you know what's up
- static unsigned int freemem;
- bool g_https_complete;
- uint32 g_bytes_received;
- TCPClient client;
- // Replace XXXX...XXX with base64 encoding if your gf username:password
- // If you don't know how to generate the base64 encoding go here:
- // http://www.tuxgraphics.org/toolbox/base64-javascript.html
- // CAUTION: Do NOT remove/replace the word Basic from the string above,
- // it's part of http standard.
- #define GF_JSON_SIZE 300
- unsigned char httpRequestContent[] = "POST %s HTTP/1.0\r\n"
- "Authorization: Splunk 867A2809-1AF9-4200-8916-14FF8B01EC37\r\n"
- "Accept: */*\r\n"
- "Content-Length: %d\r\n\r\n%s";
- // 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;
- 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
- // httpsclientSetup(host, endpoint);
- // Setup Serial Debugging
- Serial.begin(9600);
- }
- int g_connected;
- 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;
- // HTTP Request
- httpsclientSetup(host, endpoint);
- g_connected = client.connect(host, g_port);
- if (!g_connected) {
- client.stop();
- // If TCP Client can't connect to host, exit here.
- return;
- }
- // build data for POST
- sprintf(httpPostPayload, "{\"event\":\"temperature=%f,temperature_f=%f\"}",temperature, temperatureF);
- // size_t bufsize = top.printTo(httpPostPayload, sizeof(httpPostPayload));
- int rc;
- httpsclientSetPath(endpoint);
- Serial.println("httpsClientConnection Sending Payload ");
- // Serial.println("Headers %s",httpRequestContent);
- Serial.println(httpPostPayload);
- if ((rc = httpsClientConnection(httpRequestContent, 1000, httpPostPayload)) < 0) {
- // if ((rc = httpsClientConnection(httpRequestContent, 19, "{\"event\":\"this works\"}")) < 0) {
- // TODO: When massive FAIL
- if (g_https_trace) {
- Serial.print("httpsClientConnection Returned ");
- Serial.println(rc);
- }
- httpsclientCleanUp();
- }
- Serial.print("httpsClientConnection Returned ");
- Serial.println(rc);
- // finish comms
- client.stop();
- // close down HTTPS
- httpsclientCleanUp();
- Serial.println("httpsClientConnection Sent Payload ");
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement