Hallo,
ich schreibe gerade an einer Kleinen Box, die einen Bewegungsmelder hat und bei Bewegung oder einem festen Interval Werte mit der DHT library ausliest und versenden soll.
Nun mein Problem. im Intervall klappt das ganz gut. Bsp alle 60 sec.
Wenn der PIR-Sensor losgeht und den PIN 2 antriggert klappt es auch mit dem Interrupt. Ich kriege bloß keine Werte von dem Sensor. Ich weiß echt nicht mehr weiter...
Mein Code :
/**************************************************
** SensorBox **
**************************************************/
#include <RCSwitch.h>
#include <DHT.h>
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
/**************************************************
** SETTINGS **
**************************************************/
/*
Sleep time: 1min = 60000 ... 5min = 300000 ... 10Min = 600000
*/
long unsigned int sleep = 60000;
/***************
** ID **
***************/
int Id = 01; //The ID of your Sensorbox. Between 1 - 99
/****** PIR ****/
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
/****** DHT ****/
//define the Sensor
#define DHTTYPE DHT11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
/****** DEBUG ****/
boolean Debug = true;
/****** PINS ****/
int PirPin = 2; //the digital pin connected to the PIR sensor's output. MUST BE AN INTERRUPT PIN
int SendPin = 8; //Pin of 433 Mhz sender
int LightPin = A4; //Light Sensor
int TempPin = A5; // Temperature and hum DHT22
/***** Vars for Readings ***/
volatile int PirState = 0;
float temp = 0;
float hum = 0;
float light = 0;
/***** Initial Classes ***/
RCSwitch mySwitch = RCSwitch();
DHT dht(TempPin, DHTTYPE);
/**************************************************
** SETUP **
**************************************************/
void setup(){
if(Debug){
Serial.begin(9600);
}
mySwitch.enableTransmit(8);
pinMode(PirPin, INPUT);
digitalWrite(PirPin, LOW);
//give the PIRsensor some time to calibrate
if(Debug){
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
}else{
delay(calibrationTime*1000);
}
delay(50);
attachInterrupt(0, IntAct, RISING);
}
/**************************************************
** Main **
**************************************************/
void loop(){
fetchData();
delay (sleep);
}
void PirAction(){
/************* PIR ********************/
if(digitalRead(PirPin) == HIGH){ //PIR activity
PirState = 1;
if(Debug){
Serial.println("PIR = 1");
}
} else {
PirState = 0;
}
}
void readTH(){
/************* Temp and Hum ************/
hum = dht.readHumidity();
temp = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(hum) || isnan(temp) ) {
Serial.println("Failed to read from DHT sensor!");
Serial.println(dht.read());
//return;
}
if(Debug){
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" *C \n");
}
}
void readLux(){
/************** Light Sensor *****/
light = map(analogRead(LightPin),0,1023,100,0);
if(Debug){
Serial.print("Lux: ");
Serial.print(light);
Serial.print(" % \n");
}
}
void fetchData(){
PirAction();
readTH();
readLux();
}
void IntAct(){
PirState = 1;
readTH();
readLux();
}
void sendError (){
}
void sendData (){
//
}
Der Code wird in einigen Funktionen noch entwickelt.
Die Ausgabe sieht im Interval so aus:
Humidity: 35.00 % Temperature: 21.00 *C Lux: 86.00 %
Im Interrupt:
Failed to read from DHT sensor!
1
Humidity: nan % Temperature: 0.00 *C
Lux: 74.00 %
Need Help