Hello,
I was looking for a good working non-battery powered 3 in 1 sketch but couln't find one, that worked the way i wanted it to work. My wishlist:
- Instant update's on temp and light changes
- Only send motion stop if motion stopped for at least 10 seconds
- Only send motion start if motion stopped
- No delays
- Make it easy to add other sensors / remove excisting ones *
- Enable / disable serial debug by define *
I have made this sketch, hope it can be usefull to someone else. Have tried to document the code as good as possible. I'm planning to add defines in the future to be able to add / remove a temp sensor without altering the whole sketch. So just define light / temp / hum / dhtxx etc, and the sketch will work. The same goes for serial debugging,
What i used: Dallas DS18b20 , PIR, LDR
// Include libaries
#include <SPI.h>
#include <MySensor.h>
#include <DallasTemperature.h> // For Dallas temp sensor
#include <OneWire.h> // For Dallas temp sensor
// Define mysensors data
#define CHILD_ID_LIGHT 0 // LDR child ID
#define CHILD_ID_TEMP 1 // Temp child ID
#define CHILD_ID_MOTION 2 // MOTION child ID
#define LIGHT_SENSOR_ANALOG_PIN A2 // LDR PIN
#define MOTION_INPUT_SENSOR 2 // Motion input pin
#define ONE_WIRE_BUS 3 // DALLAS pin
#define MAX_ATTACHED_DS18B20 16 // DALLAS max nr. of sensors
// Initialize mysensor
MySensor gw;
// Initialize dallas
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Store variables
long previousMillis = 0; // last time the sensors are updated
long sensorInterval = 5000; // interval at which we will take a measurement ( 5 seconds)
int lastLightLevel; // Holds last light level
float lastTemperature[MAX_ATTACHED_DS18B20]; // Holds DALLAS temperatures
int numSensors=0; // Holds number of sensors
boolean metric = true; // Celcius or fahrenheid
int calibrationTime = 30; // the time we give the sensor to calibrate (10-60 secs according to the datasheet)
long unsigned int lowIn; // the time when the sensor outputs a low impulse
long unsigned int pause = 10000;//the amount of milliseconds the sensor has to be low before we assume all motion has stopped
boolean lockLow = true;
boolean takeLowTime;
// Initialize childs
MyMessage msgLight(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
MyMessage msgTemp(CHILD_ID_TEMP,V_TEMP);
MyMessage msgMot(CHILD_ID_MOTION, V_TRIPPED);
void setup() {
sensors.begin();
gw.begin();
// Send sketch info to controller
gw.sendSketchInfo("Light sensor without delay", "1.0");
// Register child sensors
gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
gw.present(CHILD_ID_MOTION, S_MOTION);
// Set pinmode for motion sensor
pinMode(MOTION_INPUT_SENSOR, INPUT);
// Pull down motion pin
digitalWrite(MOTION_INPUT_SENSOR, LOW);
numSensors = sensors.getDeviceCount();
// Present all sensors to controller
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
gw.present(i, S_TEMP);
}
delay(calibrationTime); // Give pir time to calibrate
}
void loop()
{
// Check if sensors have changed
sensorCheck();
if(digitalRead(MOTION_INPUT_SENSOR) == HIGH){
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
gw.send(msgMot.set("1")); // Send tripped value to gw
}
takeLowTime = true;
}
if(digitalRead(MOTION_INPUT_SENSOR) == LOW){
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause, we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
gw.send(msgMot.set("0")); // Send tripped value to gw
}
}
}
void sensorCheck(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > sensorInterval) {
// Save the current millis
previousMillis = currentMillis;
// take action here:
// Begin light measurement
int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
Serial.print("Lightlevel: ");
Serial.println(analogRead(LIGHT_SENSOR_ANALOG_PIN));
if (lightLevel != lastLightLevel) {
gw.send(msgLight.set(lightLevel));
lastLightLevel = lightLevel;
}
// end light measurement
// Begin dallas temperature
// Fetch temperatures from Dallas sensors
sensors.requestTemperatures();
// Read temperatures and send them to controller
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
// Fetch and round temperature to one decimal
float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
Serial.print("Temperature ");
Serial.print(i);
Serial.print(" ");
Serial.println(temperature);
// Only send data if temperature has changed and no error
if (lastTemperature[i] != temperature && temperature != -127.00) {
// Send in the new temperature
gw.send(msgTemp.setSensor(i).set(temperature,1));
lastTemperature[i]=temperature;
}
}
// End dallas temperarures
}
}