So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By sjap84
#78080 Hey Everyone,
I am busy with a diy alarm system and i am having issues with the following:

I bought these esp01 devices. https://www.aliexpress.com/item/Upgraded-version-ESP-01-ESP8266-serial-WIFI-wireless-module-wireless-transceiver-ESP01-ESP8266-01/32341788594.html

I want to create a simpel mqtt device that sends a message when a magnetic sensor is open.

Here for i am using the following code:
Code: Select all#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
ADC_MODE(ADC_VCC);

//USER CONFIGURED SECTION START//
const char* ssid = "YOUR_WIRELESS_SSID";
const char* password = "YOUR_WIRELESS_SSID";
const char* mqtt_server = "YOUR_MQTT_SERVER_ADDRESS";
const int mqtt_port = YOUR_MQTT_SERVER_PORT;
const char *mqtt_user = "YOUR_MQTT_USERNAME";
const char *mqtt_pass = "YOUR_MQTT_PASSWORD";
const char *mqtt_client_name = "PICK_UNIQUE_WINDOW_NAME"; // Client connections cant have the same connection name
const char *mqtt_topic = "PICK_UNIQUE_WINDOW_TOPIC";
IPAddress ip(192, 168, 86, 48);
IPAddress gateway(192, 168, 86, 160);
IPAddress subnet(255, 255, 255, 0);
//USER CONFIGURED SECTION END//

WiFiClient espClient;
PubSubClient client(espClient);

// Variables
bool boot = true;
char batteryVoltageMQTT[50];

//Functions

void setup_wifi()
{
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
    {
      delay(50);
    }
}

void reconnect()
{
  while (!client.connected())
  {
      int battery_Voltage = ESP.getVcc() + 600;
      String temp_str = String(battery_Voltage);
      String mqttString = temp_str + "mV Replace Battery";
      mqttString.toCharArray(batteryVoltageMQTT, mqttString.length() + 1);
      if (battery_Voltage <= 2900)
      {
        if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass, mqtt_topic, 0, 1, batteryVoltageMQTT))
        {
       
          if(boot == true)
          {
            client.publish(mqtt_topic,"open");
            boot = false;
          }
        }
        else
        {
          ESP.restart();
        }
      }
      if (battery_Voltage > 2900)
      {
        if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass, mqtt_topic, 0, 1, "closed"))
        {
          if(boot == true)
          {
            client.publish(mqtt_topic,"open");
            boot = false;
          }
        }
        else
        {
          ESP.restart();
        }
      }
  }
}

void setup()
{
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
}

void loop()
{
 
  if (boot == true)
  {
    reconnect();
  }
  else
  {
  ESP.deepSleep(0);
  }
}


My wiring is as follows:

Image

The device works but the battery drain is high.
I have used normal aa battery`s and rechargeable 2000 mah battery`s but i get 2 days tops.
Looking ad other projects i see people getting 6 months till a year of battery life.
I am sure i am missing something but i cant find it.

Can someone please have a look at it and point me in the right direction?
Thanks in advance.
User avatar
By rudy
#78083 The ESP-01 was a bad choice. You don't have access to the required pins. Most people would use a ESP-12 series module. That has all the pins available and can go into, and come out of, deep sleep.

If I was stuck using the ESP-01 I would use the alarm switch contact to bring the ESP out of a reset state. I would then use an available output pin to override the alarm switch, allow the ESP to continue to operate even if the switch went back into the normal, non-alarm, state.

The ESP does it's thing. Goes and sends out the notification. Then the override pin is released by the output port and the ESP goes back into a reset condition.

This would allow for a very low power operation, only operate when triggered. But it might be more desirable to also have deep sleep operation so that it could periodically "call home" and report that it still is alive.
User avatar
By sjap84
#78084
rudy wrote:The ESP-01 was a bad choice. You don't have access to the required pins. Most people would use a ESP-12 series module. That has all the pins available and can go into, and come out of, deep sleep.

If I was stuck using the ESP-01 I would use the alarm switch contact to bring the ESP out of a reset state. I would then use an available output pin to override the alarm switch, allow the ESP to continue to operate even if the switch went back into the normal, non-alarm, state.

The ESP does it's thing. Goes and sends out the notification. Then the override pin is released by the output port and the ESP goes back into a reset condition.

This would allow for a very low power operation, only operate when triggered. But it might be more desirable to also have deep sleep operation so that it could periodically "call home" and report that it still is alive.


Thanks for your answer.
I assumed that the reset to ground would do this.
In the script the esp is put in a definite deepsleep mode and then awaken again by pulling the reset pin to ground and reboots the esp.

I have read that to awaken the esp01 you need to have access to the non available pins but there are also a lot of examples putting in a button between reset and ground and that worked fine.

But I do need to have access to those non available pins to put the esp in deepsleep mode or are those only needed to pull it out of deepsleep?

Thanks in advance.
User avatar
By btidey
#78085 I would put a multimeter (current mode) in the Vdd feed to the circuit to help diagnose the issue.

With mag switch closed (reset low) the current should be lowish but it maybe the internal pull up on the reset line is a low value. Typically this would be about 10K which would amount to 300uA current in addition to the standby current of the ESP8266. Although high for a battery application 300uA should still last 6000 hours with that battery pack

With it open the current should go up to about 80mA for a few seconds until deep sleep kicks in and it drops back down to the same sort of quiescent current as with switch closed. If that doesn't happen then there is some flaw in the program logic preventing the deep sleep kicking in. It sounds like that might be happening in your case.

To avoid the higher quiescent current caused by the reset pull up. I use the CH_PD as the trigger input for the switch. This behaves similarly to the RST but you can put your own high value pull up on that. For example, I leave the RST alone, put a 1M pull up from CH_PD to Vdd and connect the switch to that. Quiescent current is then around 10uA.

Rudy's logic to force hold the CH_PD via another GPIO (via a medium value resistor) is what I do as well using a ESP-12F on my security sensors. Otherwise a short 'open' may not give enough time to report. After reporting the event the code releases the hold GPIO and goes into deep sleep waiting for the next event.