From 8652690b760868ab9fb5686d33bbe80bdfa53f32 Mon Sep 17 00:00:00 2001 From: iotcat Date: Tue, 20 Aug 2019 11:42:35 +0800 Subject: [PATCH] pir --- src/din/din.ino | 16 ++++++++++++-- src/din/pir.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/din/pir.h diff --git a/src/din/din.ino b/src/din/din.ino index b3930be..feefb41 100644 --- a/src/din/din.ino +++ b/src/din/din.ino @@ -2,7 +2,7 @@ * @Author: IoTcat (https://iotcat.me) * @Date: 2019-08-20 09:57:58 * @Last Modified by: - * @Last Modified time: 2019-08-20 10:04:04 + * @Last Modified time: 2019-08-20 11:11:31 */ #include @@ -11,12 +11,13 @@ #include "led.h" #include "buz.h" #include "swi.h" +#include "pir.h" const char* ssid = "yimian-iot"; const char* password = "1234567890."; const char* mqtt_server = "192.168.3.4"; // change this to the mqtt server -const char* topicIn = "hass/ctl/liv/#"; // change this to the outgoing messages +const char* topicIn = "hass/ctl/din/#"; // change this to the outgoing messages const String clientId = "liv"; @@ -25,6 +26,7 @@ PubSubClient client(espClient); Buz buz(D13); Swi swi(D8, D9); +Pir p(D10); void setup() { Serial.begin(115200); @@ -48,6 +50,7 @@ void setup() { buz.ini(); swi.ini(); + p.ini(); } @@ -111,5 +114,14 @@ void loop() { buz.loop(); swi.loop(); + p.loop(); + + if(p.isPeopleIn()){ + Serial.println("ppppppin"); + } + + if(p.isPeopleOut()){ + Serial.println("pppppppout"); + } } diff --git a/src/din/pir.h b/src/din/pir.h new file mode 100644 index 0000000..3da68e2 --- /dev/null +++ b/src/din/pir.h @@ -0,0 +1,57 @@ +/* + * @Author: IoTcat (https://iotcat.me) + * @Date: 2019-08-20 11:15:20 + * @Last Modified by: + * @Last Modified time: 2019-08-20 11:28:17 + */ + +#ifndef __PIR_H__ +#define __PIR_H__ + +class Pir{ +public: + Pir(const int& pin){ + this->_pin = pin; + }; + ~Pir(){}; + + void ini() { + pinMode(this->_pin, INPUT); + this->_fState = this->getState(); + }; + void begin() { + this->ini(); + }; + + void loop(){ + + }; + + const bool isPeopleIn(){ + if(this->_fState == false && this->getState() == true){ + this->_fState = true; + return true; + } + return false; + } + + const bool isPeopleOut(){ + if(this->_fState == true && this->getState() == false){ + this->_fState = false; + return true; + } + return false; + }; + + const bool getState(){ + return digitalRead(this->_pin); + }; + +private: + unsigned short _pin; + bool _fState; + +}; + + +#endif //__PIR_H__