I am building a ESP-07 based IOT device, that detects the status of 4 doors. The device must connect to WiFi and send an http request, when 1 of 4 doors is opened. I want to place a reed switch, for example on each door. The battery consumption should be minimal, so I thought of using deepsleep () or something similar to keep the device asleep most of the time, or keep it completely off (like most Dash buttons does).

I have studied several options, but almost all use a single GPIO to wake up the device. For example, this option wakes the device up if D3 goes high, and sleep if D3 is low.

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define WAKE_PIN D3

const char* ssid = "SSID";
const char* pass = "PASSWORD";

extern "C" {
    #include "gpio.h"
}

extern "C" {
    #include "user_interface.h"
}

void setup(){
    WiFi.begin(ssid, pass);
    uint8_t i = 0;
    while (WiFi.status() != WL_CONNECTED && i++ < 10) delay(250);
    Serial.begin(115200);
    Serial.println("Setup");
    pinMode(WAKE_PIN, INPUT_PULLUP);
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
    gpio_init(); // Initilise GPIO pins
}

void loop(){
    Serial.println("Do something...");
    if(!digitalRead(WAKE_PIN)){
        sleep();
    }
    delay(1000);
}

void sleep(){
    Serial.println("Going to sleep...");
    delay(10);
    wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); // set sleep type
    gpio_pin_wakeup_enable(GPIO_ID_PIN(WAKE_PIN), GPIO_PIN_INTR_HILEVEL); 
    wifi_fpm_open(); // Enables force sleep
    wifi_fpm_do_sleep(0xFFFFFFF); // Sleep for longest possible time
    delay(100);
    Serial.println("Woke up!");
}

Here, GPIO_ID_PIN(2) corresponds to GPIO2 on ESP8266-01 , GPIO_PIN_INTR_LOLEVEL for a logic low, can also do other interrupts.

My question is:

Can I use more than one GPIO, that is, one of 4 GPIOs to wake up the device?

I have the problem of knowing which of the four doors has been opened!

The other option I have thought is to use a latch circuit that turns on the (previously turned off) device using a single GPIO. Something like this:

Latch circuit

What I meant is that ESP-07 can latch its own power on (and off) using an MOSFET pair connected in parallel with the physical (momentary contact) power switch. When the power switch is pressed (or a door is opened), one of the first things the ESP8266 needs to do is latch the power on. When the current iteration of this programm is done, it simply turns it's own power off again.

#define POWER_SW   2   // GPIO connected to POWER_LATCH

void LatchOn() {
    pinMode(POWER_SW, OUTPUT); 
    digitalWrite(POWER_SW, HIGH);
}


void LatchOff() {
    digitalWrite(POWER_SW, LOW);
}

What I want to know is how I can use more than one signal from each of the 4 doors, or, use 4 GPIOs to activate the latch circuit. I tried with one single door connected to the GPIO that wakes the chip up. I want to extend the functionality of my program so that any of the 4 doors turns the chip on.

Thanks in advance!

P.S.
I have opened a thread about this topic in Arduino Forum as well.

  • 1
    do you have the option of using a logic IC? – ratchet freak Jan 17 at 15:59
  • Sounds good! Any ideas? – Jose Alfredo Sanchez Diaz Jan 17 at 16:06
  • 1
    Idea: Wire the four door-GPIOs together with an OR gate, connect that to your wakeup pin, but still connect all door-GPIOs to 4 different GPIO pins on your ESP8266. When a wakeup is triggered (because ANY of the door GPIOs were triggered), read the state of the 4 door-GPIOs, act on it, then go back to sleep. You can use simple diode-ORs or CMOS (N)OR logic gates for this. – Maximilian Gerhardt Jan 17 at 17:51
  • 1
    Also asked at stackoverflow.com/q/48279829 – per1234 Jan 17 at 19:55
  • I will try with the OR gates! Thanks! – Jose Alfredo Sanchez Diaz Jan 17 at 20:24

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.