Browse Source

add PIR

main
Hendrik Langer 7 years ago
parent
commit
aa2cf99d6d
  1. 4
      src/hardware.h
  2. 37
      src/main.cpp

4
src/hardware.h

@ -5,7 +5,7 @@
// LED_BUILTIN = 2 // LED_BUILTIN = 2
// KEY_BUILTIN = 0 // KEY_BUILTIN = 0
static constexpr uint8_t buttonPin = 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 rotaryPinA = 35;
static constexpr uint8_t rotaryPinB = 34; static constexpr uint8_t rotaryPinB = 34;
static constexpr uint8_t rotaryPinButton = 2; 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; static constexpr uint8_t PROGMEM NUM_LEDS = 10;
#define LED_TYPE WS2812B #define LED_TYPE WS2812B
#define COLOR_ORDER GRB #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 uint8_t ext_wakeup_pin_2 = 0;
static constexpr uint32_t secondsToSleep = 15*60; static constexpr uint32_t secondsToSleep = 15*60;

37
src/main.cpp

@ -66,17 +66,21 @@ Led led;
menuType menuChange = eNone; menuType menuChange = eNone;
uint32_t lastButtonPress = 0; uint32_t lastButtonPress = 0;
uint32_t lastActive = 0;
uint32_t lastUpdate = 0; uint32_t lastUpdate = 0;
uint32_t lastTransmit = 0; uint32_t lastTransmit = 0;
bool sensorON = false;
//Variables for the mqtt packages and topics //Variables for the mqtt packages and topics
String commandTopic; String commandTopic;
String weatherTopic; String weatherTopic;
String statusTopic; String statusTopic;
String sensorTopic;
void setup() { void setup() {
// gpio configuration // gpio configuration
pinMode(buttonPin, INPUT_PULLUP); pinMode(buttonPin, INPUT_PULLUP);
pinMode(sensorPin, INPUT_PULLDOWN);
pinMode(18, OUTPUT); pinMode(18, OUTPUT);
digitalWrite(18, HIGH); // disable LoRa_CS digitalWrite(18, HIGH); // disable LoRa_CS
@ -91,6 +95,7 @@ void setup() {
commandTopic = "esp32-node/cmd/" + iot.hostname + "/play"; commandTopic = "esp32-node/cmd/" + iot.hostname + "/play";
weatherTopic = "esp32-node/stat/" + iot.hostname + "/weather"; weatherTopic = "esp32-node/stat/" + iot.hostname + "/weather";
statusTopic = "esp32-node/stat/" + iot.hostname + "/status"; statusTopic = "esp32-node/stat/" + iot.hostname + "/status";
sensorTopic = "esp32-node/stat/" + iot.hostname + "/sensor";
/* /*
//Use the web object to add elements to the interface //Use the web object to add elements to the interface
@ -178,6 +183,8 @@ void setup() {
//iot.mqtt.onPublish(NULL); //iot.mqtt.onPublish(NULL);
iot.mqtt.onMessage(onMqttMessage); iot.mqtt.onMessage(onMqttMessage);
iot.mqtt.setWill(sensorTopic.c_str(), 0, false, "OFFLINE");
while (iot.wifi.status() != WL_CONNECTED) { while (iot.wifi.status() != WL_CONNECTED) {
delay(500); delay(500);
Serial.print("."); Serial.print(".");
@ -194,6 +201,8 @@ void setup() {
// update 'now' variable with current time // update 'now' variable with current time
time(&now); 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); if (timeinfo.tm_hour < 7 || timeinfo.tm_hour > 23) u8g2.setContrast(1);
else u8g2.setContrast(127); else u8g2.setContrast(127);
@ -204,6 +213,16 @@ void setup() {
lastButtonPress = millis(); lastButtonPress = millis();
rotary.registerCallback(rotation); rotary.registerCallback(rotation);
rotary.begin(rotaryPinA, rotaryPinB, rotaryPinButton); 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_ext1_wakeup(ext_wakeup_rotarybtn_mask, ESP_EXT1_WAKEUP_ANY_HIGH);
////esp_sleep_enable_ext0_wakeup((gpio_num_t)sensorPin, 0); ////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; // 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); //// 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(); if (direction == 1) screen->next();
else if (direction == -1) screen->previous(); else if (direction == -1) screen->previous();
Serial.println("rotation call returned"); lastActive = millis();
} }
@ -385,12 +405,12 @@ void loop()
bool stayAwake = false; bool stayAwake = false;
if (alarmArmed && seconds >= -2*5*60) stayAwake = true; 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); Serial.printf("alarm in %f seconds (%d)\n", seconds, alarmArmed);
if (seconds >= 0 && alarmArmed) { if (seconds >= 0 && alarmArmed) {
alarmArmed = false; alarmArmed = false;
lastButtonPress = millis(); lastActive = millis();
led.wakeUpLight(0); led.wakeUpLight(0);
Serial.println("WAKEUP TIME!!!!!"); Serial.println("WAKEUP TIME!!!!!");
// mp3.setVolume(10); // mp3.setVolume(10);
@ -414,6 +434,15 @@ void loop()
Serial.print("Free Heap: "); Serial.print("Free Heap: ");
Serial.println(ESP.getFreeHeap()); 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) { if(millis() - lastTransmit >= 60*1000) {

Loading…
Cancel
Save