/* mannema */ // Enable and select radio type attached #define MY_RADIO_NRF24 // #define MY_DEBUG #define MY_NODE_ID 11 #include #include #include #define CHILD_ID_HUM 0 // Id of the sensor child: Hum #define CHILD_ID_TEMP 1 // Id of the sensor child: Temp #define CHILD_ID_PIR 2 // Id of the sensor child: PIR MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED); // for PIR sensor, int PIRinputPIN = 2; int pirState = LOW; // we start, assuming no motion detected int pirVal = 0; // variable for reading the pin status // for red LED, int ledPINred = 3; // for DHT11, int pinDHT11 = 4; SimpleDHT11 dht11; // for green LED, int ledPINgreen = 5; // constants won't change // interval (miliseconden) const long intervalTemp = 60000; //Time used to store millis in unsigned long currentMillis = millis(); // Generally, you should use "unsigned long" for variables that hold time // The value will quickly become too large for an int to store // will store last time something was updated unsigned long previousMsTemp = 0; // the setup function runs once when you press reset or power the board void setup() { Serial.begin(115200); // initialize digital pins pinMode(PIRinputPIN, INPUT); //PIR sensor as input pinMode(ledPINred, OUTPUT); //LED as output pinMode(ledPINgreen, OUTPUT); //LED as output digitalWrite(ledPINgreen, HIGH); // turn the LED on (HIGH is the voltage level) } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("SensorNode001", "1.4"); // 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_PIR, S_MOTION); } // the loop function runs over and over again forever void loop() { currentMillis = millis(); // ----------------------------------------- // Uitlezen PIR sensor // ----------------------------------------- delay(100); // wait for a second ReadPIR(); // ----------------------------------------- // Uitlezen Temp+Hum elke intervalTemp(ms) // ----------------------------------------- if (currentMillis - previousMsTemp >= intervalTemp) { // save the last time you read Temp previousMsTemp = currentMillis; Serial.print(intervalTemp / 1000); Serial.print(" seconds has passed: "); Serial.println(previousMsTemp / 1000); // Read Temperature and Humidity ReadTempHum(); } // einde intervalTemp } // einde loop void ReadTempHum(){ // ----------------------------------------- // read Temp+Hum. DHT11 // ----------------------------------------- byte temperature = 0; byte humidity = 0; if (dht11.read(pinDHT11, &temperature, &humidity, NULL)) { Serial.print("Read DHT11 failed."); } else { Serial.print("Sample OK: "); Serial.print((int)temperature); Serial.print(" *C, "); Serial.print((int)humidity); Serial.println(" %"); // // Versturen data // send(msgTemp.set((int)temperature)); send(msgHum.set((int)humidity)); } // einde Uitlezen Temp+Hum } // einde ReadTempHum void ReadPIR(){ // ----------------------------------------- // read PIR sensor. // ----------------------------------------- pirVal = digitalRead(PIRinputPIN); if (pirVal == HIGH) { // Er is beweging if (pirState == LOW){ // Beweging heeft net plaats gevonden Serial.print("Motion detected: "); Serial.println(currentMillis / 1000); digitalWrite(ledPINred, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(ledPINgreen, LOW); // turn the LED on (HIGH is the voltage level) pirState = HIGH; send(msgPIR.set(1)); } } else { // Er is geen beweging if (pirState == HIGH){ // Status is net veranderd van beweging naar geen beweging Serial.print("Motion ended: "); Serial.println(currentMillis / 1000); digitalWrite(ledPINred, LOW); // turn the LED on (HIGH is the voltage level) digitalWrite(ledPINgreen, HIGH); // turn the LED on (HIGH is the voltage level) pirState = LOW; send(msgPIR.set(0)); } } } // einde ReadPIR