Hi everyone.
Been wanting to implement some mysensors goodness for awhile now, and I finally got around to building the first node for my home automation project.
I'm learning as I go, so things aren't pretty yet.
Before I continue, I wanted to start by thanking everyone on here, I've learned so much by reading your posts.
This first node is called "BasementMonitor". It'll eventually be mounted to the wall under the stairs in the basement. It's purpose is to scan the following and report them to my (soon to be built) Raspberry Pi OpenHAB.
- Temperature
- Humidity
- Gas (CO2)
- Ambient light (to know if the lights were left on)
- Flame
- Water (on the floor)
Since this is my first build it's not pretty... basically stuff hot glued into a tupperware container. I'll pretty it up once I'm done all my testing.
Here is my sketch so far. It's still pretty raw and I've got a bunch more things to implement, but it works
#define MY_NODE_ID 2
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define CHILD_ID_LIGHT 2
#define CHILD_ID_FLAME 3
#define CHILD_ID_WATER 4
#define CHILD_ID_GAS 5
#include <SPI.h>
#include <MySensor.h>
#include <DHT.h>
#define HUMIDITY_SENSOR_DIGITAL_PIN 5
#define LIGHT_SENSOR_ANALOG_PIN A2
#define FLAME_SENSOR_DIGITAL_PIN 3
#define WATER_SENSOR_ANALOG_PIN A0
#define GAS_SENSOR_ANALOG_PIN A1
MySensor gw;
DHT dht;
float lastTemp;
float lastHum;
boolean metric = true;
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgLight(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
MyMessage msgFlame(CHILD_ID_FLAME, V_STATUS);
MyMessage msgWater(CHILD_ID_WATER, V_LEVEL);
MyMessage msgGas(CHILD_ID_GAS, V_LEVEL);
unsigned long pMillis_Photo = 0;
unsigned long pMillis_WS = 0;
unsigned long pMillis_DHT = 0;
unsigned long pMillis_GS = 0;
unsigned long pMillis_FS = 0;
long sCheck_Photo = 60000;
long sCheck_WS = 60000;
long sCheck_DHT = 60000;
long sCheck_GS = 20000;
long sCheck_FS = 20000;
int lastLightLevel;
int lastFlameLevel;
int lastWaterLevel;
int lastGasLevel;
void setup(){
gw.begin(NULL, MY_NODE_ID, true);
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
gw.sendSketchInfo("Basement Monitor", "1.0");
gw.present(CHILD_ID_HUM, S_HUM);
gw.present(CHILD_ID_TEMP, S_TEMP);
gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
gw.present(CHILD_ID_FLAME, S_LIGHT);
gw.present(CHILD_ID_WATER, S_MOISTURE);
gw.present(CHILD_ID_GAS, S_AIR_QUALITY);
metric = gw.getConfig().isMetric;
}
void loop(){
unsigned long currentMillis = millis();
// Check Temp and Humidity
if (currentMillis - pMillis_DHT >= sCheck_DHT){
pMillis_DHT = currentMillis;
float temperature = dht.getTemperature();
if (isnan(temperature)) {
Serial.println("Failed reading temperature from DHT");
} else if (temperature != lastTemp) {
lastTemp = temperature;
if (!metric) {
temperature = dht.toFahrenheit(temperature);
}
gw.send(msgTemp.set(temperature, 1));
Serial.print("T: ");
Serial.println(temperature);
}
float humidity = dht.getHumidity();
if (isnan(humidity)) {
Serial.println("Failed reading humidity from DHT");
} else if (humidity != lastHum) {
lastHum = humidity;
gw.send(msgHum.set(humidity, 1));
Serial.print("H: ");
Serial.println(humidity);
}
}
// Check light level
if (currentMillis - pMillis_Photo >= sCheck_Photo){
pMillis_Photo = currentMillis;
//int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
int lightLevel = analogRead(LIGHT_SENSOR_ANALOG_PIN);
Serial.print("L: ");
Serial.println(lightLevel);
if (lightLevel != lastLightLevel) {
gw.send(msgLight.set(lightLevel));
lastLightLevel = lightLevel;
}
}
// Check Flame
if (currentMillis - pMillis_FS >= sCheck_FS){
pMillis_FS = currentMillis;
// 1=no fire 0=fire
int flameLevel = digitalRead(FLAME_SENSOR_DIGITAL_PIN);
Serial.print("F: ");
Serial.println(digitalRead(FLAME_SENSOR_DIGITAL_PIN));
if (flameLevel != lastFlameLevel) {
gw.send(msgFlame.set(flameLevel));
lastFlameLevel = flameLevel;
}
}
//Check water
if (currentMillis - pMillis_WS >= sCheck_WS){
pMillis_WS = currentMillis;
//under 10 ignore up to 70 = 1 drop over 300 = really wet
int waterLevel = analogRead(WATER_SENSOR_ANALOG_PIN);
Serial.print("W: ");
Serial.println(waterLevel);
if (waterLevel != lastWaterLevel) {
gw.send(msgWater.set(waterLevel));
lastWaterLevel = waterLevel;
}
}
// Check gas
if (currentMillis - pMillis_GS >= sCheck_GS){
pMillis_GS = currentMillis;
int gasLevel = analogRead(GAS_SENSOR_ANALOG_PIN);
Serial.print("G: ");
Serial.println(gasLevel);
if (gasLevel != lastGasLevel) {
gw.send(msgWater.set(gasLevel));
lastGasLevel = gasLevel;
}
}
}