Last active
September 29, 2021 01:02
-
-
Save Maddosaurus/228ae0e3006223562a93eca18a64e2d9 to your computer and use it in GitHub Desktop.
ESP8266 DH22 Sensor Data to Splunk HEC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
} |
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 :)
why I can't run
[image: image.png]
Matthias ***@***.***> 於 2021年9月27日 週一 上午3:07寫道:
… ***@***.**** commented on this gist.
------------------------------
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 <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 :)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/228ae0e3006223562a93eca18a64e2d9#gistcomment-3905778>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AR7ZHEQR7FKTGFK4QH5OYWDUD5VNPANCNFSM5EYWUDLA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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