Hi all,
I've read a lot of posts about battery-powered motion/temp/humidity sensors.
I'm new to electronics and programming, but I've already had sucess in setting everything up.
Below is my wiring.
![0_1490045167603_pir-dht22-battery-sketch.PNG](Uploading 100%)
I haven't modified the arduino pro mini 3.3V version for a battery optimizied use.
The VCC is directly connected to the h-pad (wire soldered to h-pad) of the HC-SR501.
The Temp/Humidity sensors is a DHT22.
The node should do the following:
If the arduino woke up from interrupt, then send only the tripped status of the motion sensor.
if the arduino woke up from the sleep timer (1 min for testing), then send humidity, battery and temperature, but only if the values changed more than the treshold.
Everything works well, the node sends humidity, battery and temperature when the sleep timer ended and does this only if it changed more than my defined threshold.
The node also detects correctly that it woke up from interrupt and sends only the motion sensor value.
The problem is that it looks like the radio triggers the motion sensors or something like that.
Everytime when the motion sensor detects motion, it sends 2-3 tripped messages in a row, if I understood correctly, this means the arduino wakes up from the interrupt, then sends the message and goes back to sleep and then immediately wakes up and sends the message again, right?
Also when the arduino wakes up from the sleep timer, it sends the temp/hum/battery values and sometimes, with random behaviour also the motion tripped value.
Where is the problem? Wiring? Coding? I tried dozens of codes and always the same behaviour
Below my sketch:
#include <SD.h>
// Enable debug prints
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//define Node ID before include mysensors.h
#define MY_NODE_ID 200
#include <SPI.h>
#include <MySensors.h>
#include <readVcc.h>
#include <DHT.h>
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define CHILD_ID_MOT 2
//define PINS
int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
#define DHT_DATA_PIN 5
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!)
#define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define MIN_V 1900 // empty voltage (0%)
#define MAX_V 3000 // full voltage (100%)
unsigned long SLEEP_TIME = 60000UL; // Sleep time between reads (in milliseconds)
int oldBatteryPcnt = 0;
//Reporting Thresholds
#define HUMI_TRANSMIT_THRESHOLD 3.0 // THRESHOLD tells how much the value should have changed since last time it was transmitted.
#define TEMP_TRANSMIT_THRESHOLD 0.5
#define BATTERY_TRANSMIT_THRESHOLD 2.0
//Parameters
DHT dht;
float lastTemp = 0 ;
float lastHum = 0 ;
int count = 0;
boolean lastTripped = false ;
boolean metric = true;
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
void setup()
{
// use the 1.1 V internal reference
#if defined(__AVR_ATmega2560__)
analogReference(INTERNAL1V1);
#else
analogReference(INTERNAL);
#endif
dht.setup(DHT_DATA_PIN);
// Send the Sketch Version Information to the Gateway
sendSketchInfo("Humidity/Motion", "1.0");
pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID_HUM, S_HUM);
present(CHILD_ID_TEMP, S_TEMP);
present(CHILD_ID_MOT, S_MOTION);
metric = getControllerConfig().isMetric;
}
void loop()
{
//-1 woke up from sleep timer, 1 woke up from pin interrupt
static int8_t wakeup;
//////////////////////////////////////////////////////////////////////////
// Battery
/////////////////////////////////////////////////////////////////////////
int sensorValue = analogRead(BATTERY_SENSE_PIN);
#ifdef DEBUG
Serial.println(sensorValue);
#endif
// 1M, 470K divider across battery and using internal ADC ref of 1.1V
// Sense point is bypassed with 0.1 uF cap to reduce noise at that point
// ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
// 3440/1023 = Volts per bit = 3.363075
float batteryV = sensorValue * 3.363075;
//int batteryPcnt = ((batteryV - MIN_V) / (MAX_V - MIN_V))*100;
int batteryPcnt = min(map(readVcc(), MIN_V, MAX_V, 0, 100), 100);
#ifdef DEBUG
Serial.print("Battery Voltage: ");
Serial.print(batteryV);
Serial.println(" V");
Serial.print("Battery percent: ");
Serial.print(batteryPcnt);
Serial.println(" %");
#endif
float diffBattery = abs(oldBatteryPcnt - batteryPcnt);
if(wakeup == 1){
}
else if (diffBattery > BATTERY_TRANSMIT_THRESHOLD) {
// Power up radio after sleep
sendBatteryLevel(batteryPcnt);
oldBatteryPcnt = batteryPcnt;
}
/////////////////////////////////////////////////////////////
//Motion
////////////////////////////////////////////////////////////
boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
if(wakeup == -1){
}
else if (tripped != lastTripped ) {
Serial.println(tripped);
send(msgMot.set(tripped?"1":"0")); // Send tripped value to gw
}
////////////////////////////////////////////////////////////////////
//Temp and Humidity
///////////////////////////////////////////////////////////////////
delay(dht.getMinimumSamplingPeriod());
float temperature = dht.getTemperature();
float diffTemp = abs(lastTemp - temperature);
float humidity = dht.getHumidity();
float diffHum = abs(lastHum - humidity);
if (isnan(temperature)) {
Serial.println("Failed reading temperature from DHT");
}else if (wakeup == 1){
}
else if(diffTemp > TEMP_TRANSMIT_THRESHOLD) {
lastTemp = temperature;
if (!metric) {
temperature = dht.toFahrenheit(temperature);
}
send(msgTemp.set(temperature, 1));
Serial.print("T: ");
Serial.println(temperature);
}
if (isnan(humidity)) {
Serial.println("Failed reading humidity from DHT");
} else if (wakeup == 1){
}
else if(diffHum > HUMI_TRANSMIT_THRESHOLD) {
lastHum = humidity;
send(msgHum.set(humidity, 1));
Serial.print("H: ");
Serial.println(humidity);
}
//////////////////////////////////////////////////////////////////////
// Sleep until interrupt comes in on motion sensor. Send update every two minute.
wakeup=sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), RISING, SLEEP_TIME);
}