Using Deep Sleep Mode w/ Arduino Mega2560 and ESP2866

Hello,

I am currently making an automated plant waterer, using the Arduino Mega2560 along with the ESP2866. Letting the Arduino run would waste too much battery, so I am trying to use a low power mode. When I try to use it, my Arduino doesn’t respond to the Blynk’s commands. Does anybody know what sleep modes work with ESP2866 and Blynk, or if I am doing something wrong? My code is posted below


#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxxx";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

#include <avr/sleep.h>

int trigPin = 6;
int echoPin = 7;
int containerDepth = 50;
int solenoidPin = 2;
int wateringTime = 5;
int solenoidState = 0;

void Sleep(){

  sleep_enable(); //Enabling sleep mode
  attachInterrupt(0, activate, RISING);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_cpu();
  
}


void activate () {

  sleep_disable();

  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2000);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  int duration = pulseIn(echoPin, HIGH);
  
  // Calculating the distance
  int distance = duration * 0.034/2;
  int waterLevel = 50 - distance;
  int percentage = waterLevel/containerDepth;

  Blynk.virtualWrite(V1, waterLevel);

  if (digitalRead(solenoidPin)) {
    
    delay(5000);
    digitalWrite(solenoidPin, LOW);
  
    
  }


  if (waterLevel < 10) {
    Blynk.notify("Water level running low! Please refill soon!");
  }
 
}


void setup()
{
  // Debug console
  Serial.begin(112500);

  delay(10);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

}

void loop()
{
  Blynk.run();
  Sleep();
}


I know little about AVR sleep state, but if you are going to put into sleep only Atmega, you will gain little benefits. The major power consumer in this setup will be ESP itself (not counting peripherals )
If you ask me, I would switch to ESP standalone, where sleep seem to work just fine…

Oh, so to limit power consumption, I need to put ESP into sleep. Is it possible to put both to sleep, and still be able to look for Blynk commands?

Logically thinking: NO! Such a setup will need to work quite autonomously, and can accept commands only while connected to network. Server will accept commands from app and they will be passed to device as soon as connection will be reestablished, but it will not be realtime.

If you put ESP8266 to sleep, the WiFi radio is off. It will never receive commands from Blynk. The only way to wake it up is by external pin toggling or use a timer to tell it to wake up every so often then go back to sleep. You also have to have the reset line tied to an interrupt pin on the 8266. It essentially hard resets the unit when the timer expires.

Since the ESP in this scenario is used as a simple Serial to WiFi transceiver (in AT mode) you would not be able to program it for sleep mode anyhow.

Also good to know… sleep works differently in ESP than with AVR…

@Gunner, it seems the ESP AT API does support some kind of deep sleep by issuing .deepSleep(time) This command issues an AT+GSLP to the ESP. But how does it work?

Probably much the same way, wakes via timer or external interrupt…

https://bbs.espressif.com/viewtopic.php?t=677

But how to get it to sleep in the first place? Since Blynk is actively communicating over the serial line, I don’t know if any AT commands will function. Perhaps need to disconnect Blynk first?

Aside from the intellectual challenge of learning… i see negligible benefit from power saving a battery powered Arduino/ESP combo as they will be too power intensive when running to make it worthwhile.

If that’s the case, is there anything I can do to save power?

Well, I suggested switching to ESP standalone, just because of the deep sleep functionality needed. But the OP insist on using combo setup.

Solar recharging?