Created
June 29, 2017 10:18
-
-
Save itskenny0/6c834a7f5af215c846ba2585cdd8aed3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <ESP8266WiFi.h> | |
#include <WiFiUDP.h> | |
#include <Adafruit_Sensor.h> | |
#include <DHT.h> | |
#include <DHT_U.h> | |
WiFiUDP Udp; | |
const char* ssid = "ky-fi devices"; | |
const char* password = "passwordremovedyayayaya"; | |
const String prefix = "sensors.tempmon02."; | |
const char* statsd = "grafana.kenny.cat"; | |
const int port = 8125; | |
const int mindelay = 10000; | |
#define DHTPIN D5 | |
#define DHTTYPE DHT22 | |
DHT_Unified dht(DHTPIN, DHTTYPE); | |
uint32_t delayMS; | |
void logn(String mesg) { | |
Serial.print("[+"); | |
Serial.print(millis() / 1000); | |
Serial.print("] "); | |
Serial.print(mesg); | |
} | |
void log(String mesg) { | |
Serial.println(""); | |
logn(mesg); | |
} | |
void loga(String mesg) { | |
Serial.print(mesg); | |
} | |
bool statsdSubmit(String key, String value) { | |
String pkt = prefix + key + ":" + value + "|g"; | |
Udp.beginPacket(statsd, port); | |
Udp.write(pkt.c_str()); | |
Udp.endPacket(); | |
//log("Submit: " + pkt); | |
return true; | |
} | |
void setup() { | |
Serial.begin(115200); | |
log("WiFi: Trying "); | |
loga(ssid); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
loga("."); | |
} | |
IPAddress ipaddr = WiFi.localIP(); | |
loga(" OK: "); | |
loga(ipaddr.toString()); | |
pinMode(D6, OUTPUT); | |
digitalWrite(D6, HIGH); | |
dht.begin(); | |
sensor_t sensor; | |
dht.temperature().getSensor(&sensor); | |
dht.humidity().getSensor(&sensor); | |
delayMS = sensor.min_delay / 1000; | |
if(delayMS < mindelay) { | |
delayMS = mindelay; | |
} | |
log("DHT22 initialized with delay of "); | |
loga(String(delayMS)); | |
} | |
void loop() { | |
delay(delayMS); | |
sensors_event_t event; | |
char temp[10]; | |
char hum[10]; | |
log("Read:"); | |
/* Get temperature */ | |
loga(" T:"); | |
dht.temperature().getEvent(&event); | |
if (!isnan(event.temperature)) { | |
dtostrf(event.temperature, 4, 2, temp); | |
statsdSubmit("temperature", temp); | |
loga(temp); | |
} else { | |
loga("FAIL"); | |
} | |
/* Get humidity */ | |
loga(" H:"); | |
dht.humidity().getEvent(&event); | |
if (!isnan(event.relative_humidity)) { | |
dtostrf(event.relative_humidity, 4, 2, hum); | |
statsdSubmit("humidity", hum); | |
loga(hum); | |
} else { | |
loga("FAIL"); | |
} | |
statsdSubmit("uptime", String(millis() / 1000)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment