First of all, do you realise that when the device is in deep sleep. it is no longer connected to WiFi, and therefore not connected to the Blynk server?
This means that if you press any button widgets while the device is asleep, it will have no idea that this button press has taken place.
As a result, it’s impossible to wake a device from deep sleep with a Blynk widget button press.
Also, during deep sleep the device will appear offline in the Blynk app and web console (aft6er the heartbeat period has passed).
Secondly, if I were going to create a deep sleep sketch, I wouldn’t start with a Blynk Edgent example.
I
In that case, the code you’ve shared isn’t the code you’re actually using (I hate it when people do that) because there’s nothing in your void loop.
Wait, Why do you think that? That’s my code. Everything works but not switches and deepsleep with them.
Ah you are right, deepsleep with this code not works but without switches thing everything was ok
Here:
// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "xxx"
#define BLYNK_TEMPLATE_NAME "Battery Monitor"
#define USE_ESP32_DEV_MODULE
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define TerminalPin V0
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
#include <WiFi.h>
#include <WiFiClient.h>
#include "BlynkEdgent.h"
#include <DHT.h>
#define TIME_TO_SLEEP (60 * 1000000)
char ssid[] = "xx"; // type your wifi name
char pass[] = "xx"; // type your wifi password
#define DHTTYPE DHT22 // DHT 22
#define DHTPIN 15 //DHT22 Pin D4(GPIO 2)
DHT dht(DHTPIN, DHTTYPE);
float voltage;
int bat_percentage;
int analogInPin = 35; // Analog input pin
int sensorValue;
float calibration = 0.00; // Check Battery voltage using multimeter & add/subtract the value
WidgetTerminal terminal(TerminalPin);
void deepSleep() {
Serial.println("Going to sleep now");
terminal.println("Going to sleep now");
delay(2000);
Serial.flush();
esp_deep_sleep_start();
}
void setup()
{
Serial.begin(115200);
delay(100);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP);
BlynkEdgent.begin();
Serial.println("Connected");
terminal.println("Connected");
terminal.println("Sensors ON");
dht.begin();
delay(1000);
for (int i = 1; i < 5; i++) {
BlynkEdgent.run();
delay(4000);
float t = dht.readTemperature();
float h = dht.readHumidity();
sensorValue = analogRead(analogInPin);
voltage = (((sensorValue) / 4096.0) * 7.445 + calibration);
bat_percentage = map(voltage, 3.3, 4.2, 0, 100); //2.8V as Battery Cut off Voltage & 4.2V as Maximum Voltage
if (bat_percentage >= 100)
{
bat_percentage = 100;
}
if (bat_percentage <= 0)
{
bat_percentage = 1;
}
//send data to blynk
Blynk.virtualWrite(V1, t); //for Temperature
Blynk.virtualWrite(V2, h); //for Humidity
Blynk.virtualWrite(V3, voltage); // for battery voltage
Blynk.virtualWrite(V4, bat_percentage); // for battery percentage
//Print data on serial monitor
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %");
Serial.print("Analog Value = ");
Serial.println(sensorValue);
Serial.print("Output Voltage = ");
Serial.println(voltage);
Serial.print("Battery Percentage = ");
Serial.println(bat_percentage);
Serial.println();
Serial.println("****************************");
Serial.println();
terminal.print("Voltage = ");
terminal.print(voltage);
terminal.println(" V");
delay(500);
terminal.print("Battery Percentage = ");
terminal.print(bat_percentage);
terminal.println(" %");
Serial.println(i);
}
deepSleep();
}
void loop() {
}
I read on some forum that if I want deep sleep and only a few loops, I should do everything in void.setup
So I did this
for (int i = 1; i < 3; i++) {
CODE
Serial.println(i);
}
Yes I know that but…
For example
Deepsleep 29min 30sec then Active 30sec (I know that after some time there will be a problem with time synchronization but I am not demanding) .
2:00-2:29:30 deepsleep
2:29:30-2:30 active
2:30-2:59:30 deepsleep again
And when I press the on/off button in the app at 2:10 the esp will download data from the server after waking up and will not fall asleep at 2:30 again, right? Isn’t that how it works? Just my guess
It depends exactly what you are trying to achieve.
If you want to be able to use Blynk.Edgent and provision your device then this code won’t work, as to do that you need Blynk.Edgent.run() in your void loop. Executing it once in void setup isn’t enough to provision a blank device.
If you want a device that will wake-up, take a reading, send it to Blynk then go to sleep then having the majority of your code somewhere other than your void loop is a good idea. If you want to take readings from widgets them that location might be in BLYNK_CONNECTED(), but it depends on the functionality you need.
The first question is why you’re planning on using deep sleep. Is your device battery powered?
Dumping lots of Edgent code into the topic isn’t helpful. Start with the basics - explain your objectives and why you’re planning on using deep sleep, and take it from there.
As I said, I want the device to check the on/off switch status after connecting to the server and either go to sleep or stay active
. Aaaah now I THINK I understand, the device must constantly perform some kind of loop to retrieve data from the server, otherwise it will stop and not read the data, right?
Yes, the device is powered by 18650 cells. At first, it was supposed to monitor the battery charge level from the solar panel, but then I thought I would add two thermometers and maybe add some additional lighting control etc etc. I keep adding something .
That’s why I would also like to be able to suspend deepsleep for some time. I know that it is impossible when the device is asleep, but I thought it would be possible to suspend deepsleep after waking up Then I will turn on deepsleep and click restart(virtual push butttom) . But now I understand that I don’t have to click restart if the device will perform a loop, right?
So maybe I will just do it this way: I will leave void.setup with sensors as it is but without deepsleep at the end of the setup loop.
Also
BLYNK_WRITE(V6)
{ deepSleepEnabled = param.asInt(); // Get the switch state (0 or 1) }
void deepSleep() {
if(deepSleepEnabled == 1){
delay(200);
Serial.println("Going to sleep now");
terminal.println(">Going to sleep now");
delay(2000);
Serial.flush(); esp_deep_sleep_start();
}
else
{
Serial.println("nope");
}
}
void setup()
Code
I think you’re not taking any notice of me when I say that Edgent isn’t the way to go.
You need to use BLYNK_CONNECTED() and Blynk.syncVirtual(vPin) to trigger the BLYNK_WRITE(vPin) callbacks, which will give you the status of your widgets.
“If you want to be able to use Blynk.Edgent and provision your device then this code won’t work, as to do that you need Blynk.Edgent.run() in your void loop. Executing it once in void setup isn’t enough to provision a blank device.”
I thought Blynk.Edgent.run() only once in setup void is not enough
I’m going to stop replying to your posts until you post a full sketch that doesn’t use Blynk Edgent.
You’ll need to manage your own WiFi connection, and use Blynk.config() and Blynk.connect() rather than Blynk.begin().
This is because Blynk.begin is a blocking function and failure to connect to either WiFi or the Blynk server will result in your device never reaching the deep sleep code, and therefore flattening your battery.
Relax small steps, I wanted to do this segment first. I know that when I turn off the router the device will drain the cells.
Now I have to read about how to avoid this and it will probably take me a few days or weeks because I do it in my free time.
Could you give me a little hint of what I should look for? There is something like this whole edgent thing? I should delete the other libraries like settings.h etc and start over right?
Im sorry for being annoying and so many questions,
thank you for your time and advices. I’ll read up on it and come back after some time. Thank you