Things/things.ino

189 lines
5.0 KiB
Arduino
Raw Normal View History

#include <Homie.h> // https://github.com/marvinroger/homie-esp8266
#include <DHT.h> // https://github.com/adafruit/DHT-sensor-library
#define HAS_LDR
#define HAS_LED
#undef HAS_DHT
// HAS_LED
#define PIN_LED_RED D8
#define PIN_LED_GREEN D6
#define PIN_LED_BLUE D7
HomieNode ledNode("led", "rgb");
int led_red = 0;
int led_green = 0;
int led_blue = 0;
// HAS_LDR
#define PIN_LDR A0
HomieNode ldrNode("ldr", "ldr");
const int INTERVAL_LDR = 60;
unsigned long lastSentLDR = 0;
int ldr = 0;
// HAS_DHT
#define PIN_DHT D4
#define TYPE_DHT DHT22
HomieNode humidityNode("humidity", "humidity");
HomieNode temperatureNode("temperature", "temperature");
HomieNode heatindexNode("heatindex", "heatindex");
const int INTERVAL_DHT = 60;
unsigned long lastSentDHT = 0;
DHT dht(PIN_DHT, TYPE_DHT);
float humidity, temperature; // raw values from the sensor
float heatindex; // computed value from the sensor
void setupHandler() {
#ifdef HAS_LDR
pinMode(PIN_LDR, INPUT);
#endif
#ifdef HAS_LED
pinMode(PIN_LED_RED, OUTPUT);
pinMode(PIN_LED_GREEN, OUTPUT);
pinMode(PIN_LED_BLUE, OUTPUT);
analogWrite(PIN_LED_RED, led_red);
analogWrite(PIN_LED_GREEN, led_green);
analogWrite(PIN_LED_BLUE, led_blue);
#endif
#ifdef HAS_DHT
dht.begin();
#endif
2016-02-23 10:12:04 +00:00
}
bool ledColorHandler(String message) {
DynamicJsonBuffer json_inBuffer;
JsonObject& json_in = json_inBuffer.parseObject(message);
if (json_in.success()) {
if (json_in.containsKey("red")) {
led_red = json_in["red"];
analogWrite(PIN_LED_RED, led_red);
}
if (json_in.containsKey("green")) {
led_green = json_in["green"];
analogWrite(PIN_LED_GREEN, led_green);
}
if (json_in.containsKey("blue")) {
led_blue = json_in["blue"];
analogWrite(PIN_LED_BLUE, led_blue);
}
} else {
Serial.println("parsing of JSON failed");
}
2016-02-23 10:12:04 +00:00
DynamicJsonBuffer json_outBuffer;
JsonObject& json_out = json_outBuffer.createObject();
json_out["red"] = led_red;
json_out["green"] = led_green;
json_out["blue"] = led_blue;
String response;
json_out.printTo(response);
Serial.print("led state: ");
Serial.println(response);
Homie.setNodeProperty(ledNode, "color", response);
return true;
2016-02-23 10:12:04 +00:00
}
2016-02-24 08:00:11 +00:00
// compare float values
bool isEqual(float a, float b, float epsilon=0.001) {
2016-02-24 06:43:01 +00:00
return fabs(a - b) <= epsilon * fabs(a);
}
void loopHandlerLDR() {
if (millis() - lastSentLDR >= INTERVAL_LDR * 1000UL || lastSentLDR == 0) {
int ldr_new = analogRead(PIN_LDR);
if (ldr_new != ldr) {
ldr = ldr_new;
float ldr_float = map(ldr, 0, 1023, 0, 10000) / 100.0;
Serial.print("LDR: ");
Serial.println(ldr_float);
if (!Homie.setNodeProperty(ldrNode, "value", String(ldr_float), true)) {
Serial.println("Sending failed");
}
} else {
Serial.println("LDR value unchanged");
}
lastSentLDR = millis();
}
2016-02-24 07:43:44 +00:00
}
void loopHandlerDHT() {
if (millis() - lastSentDHT >= INTERVAL_DHT * 1000UL || lastSentDHT == 0) {
2016-02-23 10:12:04 +00:00
float previousHumidity = humidity;
float previousTemperature = temperature;
float previousHeatindex = heatindex;
humidity = dht.readHumidity(); // read humidity as a percent
temperature = dht.readTemperature(); // read temperature as Celsius
heatindex = dht.computeHeatIndex(temperature, humidity, false);
// check if any reads failed and exit early
2016-02-23 10:12:04 +00:00
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if (!isEqual(humidity, previousHumidity)) {
Serial.print("humidity: ");
Serial.println(humidity);
if (!Homie.setNodeProperty(humidityNode, "value", String(humidity), true)) {
Serial.println("Sending failed");
}
} else {
Serial.println("humidity unchanged");
2016-02-23 10:12:04 +00:00
}
if (!isEqual(temperature, previousTemperature)) {
Serial.print("temperature: ");
Serial.println(temperature);
if (!Homie.setNodeProperty(temperatureNode, "value", String(temperature), true)) {
Serial.println("Sending failed");
}
} else {
Serial.println("temperature unchanged");
2016-02-23 10:12:04 +00:00
}
if (!isEqual(heatindex, previousHeatindex)) {
Serial.print("heatindex: ");
Serial.println(heatindex);
if (!Homie.setNodeProperty(heatindexNode, "value", String(heatindex), true)) {
Serial.println("Sending failed");
}
2016-02-23 10:12:04 +00:00
} else {
Serial.println("heatindex unchanged");
2016-02-23 10:12:04 +00:00
}
lastSentDHT = millis();
2016-02-23 10:12:04 +00:00
}
}
void loopHandler() {
#ifdef HAS_LDR
loopHandlerLDR();
#endif
#ifdef HAS_DHT
loopHandlerDHT();
#endif
2016-02-24 06:43:01 +00:00
}
2016-02-23 10:12:04 +00:00
void setup() {
Homie.setFirmware("things", "1.0.0");
#ifdef HAS_LDR
Homie.registerNode(ldrNode);
#endif
#ifdef HAS_LED
ledNode.subscribe("color", ledColorHandler);
Homie.registerNode(ledNode);
#endif
#ifdef HAS_DHT
Homie.registerNode(humidityNode);
Homie.registerNode(temperatureNode);
Homie.registerNode(heatindexNode);
#endif
Homie.setSetupFunction(setupHandler);
Homie.setLoopFunction(loopHandler);
Homie.setup();
2016-02-23 10:12:04 +00:00
}
void loop() {
Homie.loop();
2016-02-23 10:12:04 +00:00
}