From aa2cf99d6d2f54f10d081981eae1ef65e0853cfb Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Thu, 15 Feb 2018 18:34:06 +0100 Subject: [PATCH] add PIR --- src/hardware.h | 4 ++-- src/main.cpp | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/hardware.h b/src/hardware.h index b38cd81..c726d34 100644 --- a/src/hardware.h +++ b/src/hardware.h @@ -5,7 +5,7 @@ // LED_BUILTIN = 2 // KEY_BUILTIN = 0 static constexpr uint8_t buttonPin = 0; -static constexpr uint8_t sensorPin = 0; +static constexpr uint8_t sensorPin = 36; static constexpr uint8_t rotaryPinA = 35; static constexpr uint8_t rotaryPinB = 34; static constexpr uint8_t rotaryPinButton = 2; @@ -13,7 +13,7 @@ static constexpr uint8_t PROGMEM LED_PIN = 21; static constexpr uint8_t PROGMEM NUM_LEDS = 10; #define LED_TYPE WS2812B #define COLOR_ORDER GRB -static constexpr uint8_t ext_wakeup_pin_1 = 0; +static constexpr uint8_t ext_wakeup_pin_1 = sensorPin; static constexpr uint8_t ext_wakeup_pin_2 = 0; static constexpr uint32_t secondsToSleep = 15*60; diff --git a/src/main.cpp b/src/main.cpp index 99ef8de..f7863e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -66,17 +66,21 @@ Led led; menuType menuChange = eNone; uint32_t lastButtonPress = 0; +uint32_t lastActive = 0; uint32_t lastUpdate = 0; uint32_t lastTransmit = 0; +bool sensorON = false; //Variables for the mqtt packages and topics String commandTopic; String weatherTopic; String statusTopic; +String sensorTopic; void setup() { // gpio configuration pinMode(buttonPin, INPUT_PULLUP); + pinMode(sensorPin, INPUT_PULLDOWN); pinMode(18, OUTPUT); digitalWrite(18, HIGH); // disable LoRa_CS @@ -91,6 +95,7 @@ void setup() { commandTopic = "esp32-node/cmd/" + iot.hostname + "/play"; weatherTopic = "esp32-node/stat/" + iot.hostname + "/weather"; statusTopic = "esp32-node/stat/" + iot.hostname + "/status"; + sensorTopic = "esp32-node/stat/" + iot.hostname + "/sensor"; /* //Use the web object to add elements to the interface @@ -178,6 +183,8 @@ void setup() { //iot.mqtt.onPublish(NULL); iot.mqtt.onMessage(onMqttMessage); + iot.mqtt.setWill(sensorTopic.c_str(), 0, false, "OFFLINE"); + while (iot.wifi.status() != WL_CONNECTED) { delay(500); Serial.print("."); @@ -194,6 +201,8 @@ void setup() { // update 'now' variable with current time time(&now); } + localtime_r(&now, &timeinfo); + strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo); if (timeinfo.tm_hour < 7 || timeinfo.tm_hour > 23) u8g2.setContrast(1); else u8g2.setContrast(127); @@ -204,6 +213,16 @@ void setup() { lastButtonPress = millis(); rotary.registerCallback(rotation); rotary.begin(rotaryPinA, rotaryPinB, rotaryPinButton); + + if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT1) { + uint64_t wakeup_pinmask = esp_sleep_get_ext1_wakeup_status(); + if (wakeup_pinmask & (1ULL << ext_wakeup_pin_1)) { + uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, timeStr); + sensorON = true; + } + } + + lastActive = millis(); } @@ -291,7 +310,8 @@ void suspend(uint32_t secondsToSleep) { esp_sleep_enable_ext1_wakeup(ext_wakeup_rotarybtn_mask, ESP_EXT1_WAKEUP_ANY_HIGH); ////esp_sleep_enable_ext0_wakeup((gpio_num_t)sensorPin, 0); -// const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1; + const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1; + esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ANY_HIGH); // const uint64_t ext_wakeup_pin_2_mask = 1ULL << ext_wakeup_pin_2; //// esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask | ext_wakeup_pin_2_mask, ESP_EXT1_WAKEUP_ANY_HIGH); // @@ -367,7 +387,7 @@ void rotation(int i, int direction, int buttonPressed) { } if (direction == 1) screen->next(); else if (direction == -1) screen->previous(); - Serial.println("rotation call returned"); + lastActive = millis(); } @@ -385,12 +405,12 @@ void loop() bool stayAwake = false; if (alarmArmed && seconds >= -2*5*60) stayAwake = true; - if (!mp3.playing && millis() - lastButtonPress >= 60*1000 && !stayAwake) suspend(); + if (!mp3.playing && millis() - lastActive >= 60*1000 && !stayAwake) suspend(); Serial.printf("alarm in %f seconds (%d)\n", seconds, alarmArmed); if (seconds >= 0 && alarmArmed) { alarmArmed = false; - lastButtonPress = millis(); + lastActive = millis(); led.wakeUpLight(0); Serial.println("WAKEUP TIME!!!!!"); // mp3.setVolume(10); @@ -414,6 +434,15 @@ void loop() Serial.print("Free Heap: "); Serial.println(ESP.getFreeHeap()); + if (digitalRead(sensorPin) == HIGH) { + lastActive = millis(); + } + + if (sensorON && digitalRead(sensorPin)) { + uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, "OFF"); + sensorON = false; + } + } if(millis() - lastTransmit >= 60*1000) {