Skip to content

Instantly share code, notes, and snippets.

@Maddosaurus
Last active September 29, 2021 01:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Maddosaurus/228ae0e3006223562a93eca18a64e2d9 to your computer and use it in GitHub Desktop.
Save Maddosaurus/228ae0e3006223562a93eca18a64e2d9 to your computer and use it in GitHub Desktop.
ESP8266 DH22 Sensor Data to Splunk HEC
// AS LONG AS PULL REQUEST 2821 IS NOT MERGED, YOU NEED TO PATCH THE ESP8266 BOARD LIB!
// have a look here: https://www.esp8266.com/viewtopic.php?f=29&t=16473
// and here: https://github.com/esp8266/Arduino/pull/2821
#include "DHT.h"
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#define DHTPIN 2 // This is silkscreened as ~D4 on my board
#define DHTTYPE DHT22 // Sensor type
DHT dht(DHTPIN, DHTTYPE);
// WiFi to connect to
const char* ssid = "xxxxxx";
const char* password = "xxxxxxx";
// Splunk server to send data to
// Get this: openssl s_client -connect host.local:port | openssl x509 -fingerprint -noout
const char* splunk_collector_cert = "AA:BB:CC:DD:EE"; // This is the SHA-1 hash of the cert
const char* splunk_collector_url = "https://your.splunk.target:8088/services/collector";
const char* splunk_HEC_Token = "Splunk abcde-splunk-hec-token-asdg"; // IMPORTANT: Leave the "Spunk " there!
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Serial.println("Starting ESP temp serve");
Serial.printf("Connecting to %s \n", ssid);
digitalWrite(LED_BUILTIN, LOW);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println("connected!");
digitalWrite(LED_BUILTIN, HIGH);
dht.begin();
Serial.printf("Local IP: %s\n", WiFi.localIP().toString().c_str());
}
void loop() {
if(WiFi.status()== WL_CONNECTED){ // Only send on running WiFi
// get all dem sensor readouts
float temp = dht.readTemperature();
float humid = dht.readHumidity();
if(isnan(temp)) { temp = -1.0; }
if(isnan(humid)) { humid = -1.0; }
float hi = dht.computeHeatIndex(temp, humid, false);
// prepare the JSON data structure
StaticJsonBuffer<300> JSONbuffer; //Declaring static JSON buffer
JsonObject& JSONencoder = JSONbuffer.createObject();
JsonObject& eventJSON = JSONencoder.createNestedObject("event");
eventJSON["sensorLocation"] = "Sensor Location in the Room";
eventJSON["temperature"] = temp;
eventJSON["humidity"] = humid;
eventJSON["heatindex"] = hi;
char JSONmessageBuffer[300];
JSONencoder.printTo(JSONmessageBuffer);
Serial.println(JSONmessageBuffer);
// and now build and send the HTTP POST
HTTPClient http;
http.setIgnoreTLSVerifyFailure(true); // this ONLY works with the patched version of PR 2821!
http.begin(splunk_collector_url, splunk_collector_cert);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", splunk_HEC_Token);
int httpStatus = http.POST(JSONmessageBuffer);
String payload = http.getString();
Serial.println(httpStatus); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end();
delay(60000); // wait for 60 seconds
}
}
@eric693
Copy link

eric693 commented Sep 26, 2021

How can I built HTTP POST

// and now build and send the HTTP POST
HTTPClient http;
http.setIgnoreTLSVerifyFailure(true); // this ONLY works with the patched version of PR 2821

@Maddosaurus
Copy link
Author

Please be aware that this gist is more than 3 years old at this point.
Also, the library used here has been updated last year ( esp8266/Arduino#4980 ), so this information is not only outdated, but very likely to not work at all.
Please consult the official documentation of the Arduino lib (https://github.com/esp8266/Arduino) to learn how to do this in a safe and functional way.

P.S.: It would also be lovely if you could invest a bit more time into explaining what exactly your problem is or what you are trying to do ( see also this guide: https://opensource.com/life/16/10/how-ask-technical-questions ), this helps everyone reading this exchange understand better and quicker :)

@eric693
Copy link

eric693 commented Sep 29, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment