Blynk_write() wont trigger

The Blynk_write function never triggers in my program.

The structure of the program is:

void loop() {
current_millis = millis();
read_dht(); //Read DHT11
notifications(); //Check if DHT values are out of boundaries and notify if temp is below or above a set point
Blynk.run(); // Does this call the Blynk_write() function and trigger the Blynk.virtualWrite functions?
sleep_routine(); // Put NODE MCU into deep sleep
}

I’ve tried everything but cannot trigger Blynk_write. The code in it looks like this

BLYNK_WRITE(V0)
{
Serial.println(“Blynk_Write(V1) was run”); //I never get the message…
hibernation_time_min = param.asInt(); //Read last known slider pos from database
Serial.println("Hibernation time set to: " + String(hibernation_time_min)); //Give feedback to user
terminal.println("Hibernation time set to: " + String(hibernation_time_min)); //Give feedback to user

}

If I access the web API for V0 i can see the last known slider position there, but Blynk_Write just wont download it.
It won’t even give me the debug messages in the serial monitor. It is like it never runs.
The idea with the slider is to be able to set a deep sleep time that the NODE MCU downloads upon waking up and adjusts the time accordingly for the next deep sleep.

Help appreciated. I’m pulling my already thin hair :wink:

You need to use a blynk appropriate structure,

There’s a few to look at on here.

you must use simpletimer

get it looking like this (sorry, i am on my phone so no complier and below is ROUGH idea!):

#include simpleTimer;
current_millis = millis();
simplTimer timer;
int hibernation_time_min;

BLYNK_WRITE(V0)
{
hibernation_time_min = param.asInt(); /Read last known slider pos from database
Serial.println("Blynk_Write(V1) was run"); //I never get the message.. 
Serial.println("Hibernation time set to: " + String(hibernation_time_min)); //Give feedback to user
terminal.println("Hibernation time set to: " + String(hibernation_time_min)); //Give feedback to user
}

void setup()
{
setbaud etc
timer.SetInterval(5000, sleep_routine); //run function every X milliseconds (change the 5000)
timer.SetInterval(5000, read_dht); //run function every X milliseconds
timer.setInterval(5000, notifications); //run function every X milliseconds
}

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

Thanks for the feedback.
I opened up the simpletimer library and it is based of the same structure as with millis()
so I have hard time believing this would be the issue since they are just two applications of the same
function.
I tried moving the Blynk_write() function to the top before the setup() but it throws a
compilation error since it asks the compiler to compile the terminal.println call before the class is called in blynk.begin() (atleast I think it’s called there, I haven’t opened up the library to check it out).

I tried excluding the possibility that it has to do with the asynchronous nature of blynk with a
while loop that waits for a flag to be set in the blynk_write function, but that didn’t work.

Any more suggestions?

Just thought about one thing.
Does the NODE-MCU have to be powered up for sliders in the app to work?

My idea of how it works is that the hardware doesn’t have to be online to still be able to get the data.
Slider position ->uploads to blynk cloud -> NODE-MCU downloads the data from blynk-cloud.
In this case I should be able to pull off what I’m trying to. That is, change parameters in the app
while the MCU is in deep sleep which then downloads the data when it comes online.

Am I wrong in my assumption? Or is the issue related to Blynk_write?

Sync is your solution as per the comprehensive docs.

I have read the docs and I guess most of the related posts in the forum but cannot find what the issue is.

I tried sync in the setup
Blynk.syncVirtual(V0);

I tried Blynk.syncAll(); in the setup.

I have tried adding a delay after. But nothing seems to trigger blynk_write().

Do you a while statement directly after your Blynk connection?

Without it many of your setup() calls will be ignored.

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

#define BLYNK_PRINT Serial 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleDHT.h>
SimpleDHT11 dht11;

//PIN ASSIGNMENTS
int DHT_data_pin = D6;
int DHT_power_pin = D7;

//WIFI
const char* ssid =      "ssid";   
const char* password =  "pass";       

//BLYNK
char auth[] = "my blynk auth code";

//Hibernation settings
int hibernation_time_min = 1;  //Minutes

int humidity;
int temperature;

int low_temp_warning_threshold = 15;
int high_temp_warning_threshold = 40;
String room_name = "My Room";


//Initialize Blynk Modules
WidgetTerminal terminal(V3);

void setup() 
{
  pinMode(DHT_power_pin, OUTPUT);        
  digitalWrite(DHT_power_pin, LOW);      
  Serial.begin(115200);
  Blynk.begin(auth,ssid,password);                         
  while (Blynk.connect() == false) 
   {
   Serial.print(".");
   delay(1000);
   }
Blynk.syncVirtual(V0);
  
  
  
  //Do some printouts
  terminal.println("\nBlynk v" BLYNK_VERSION ": Device started");
  terminal.println("SSID: " + String(ssid));
  Serial.println("\n I'm awake. Transmitting Package");
  terminal.println(room_name);
}


void loop() 
{
 read_dht();
 notifications();
 Blynk.run();
 sleep_routine();
}


void read_dht() 
{ LEFT OUT THIS CODE SINCE IT WORKS AND 
ONLY READS THE DHT AND SENDS TO BLYNK } 

void sleep_routine() 
{
  unsigned long hibernation_time_usec = hibernation_time_min * 60000000;     
  Serial.println("Transmit complete");
  Serial.println("\nHibernating for " + String(hibernation_time_min) + " minutes");
  terminal.println("Transmit complete. \nHibernating for " +     String(hibernation_time_min) + " minutes"); 
  terminal.flush();
  system_deep_sleep(hibernation_time_usec);   // microseconds. 10 000 000 = 10 seconds 
  delay(200); //Allow time to implement the sleep routine
}

 void notifications() 
   { this part works as well. Left it out }


BLYNK_WRITE(V0)
{
  Serial.println("Blynk_Write was run");
  hibernation_time_min = param.asInt();
  Serial.println("Hibernation time set to: " + String(hibernation_time_min));
  terminal.println("Hibernation time set to: " + String(hibernation_time_min));
}

some strange formating occured in the copy and paste.

I should add that the NODE-MCU is awake for about 4-5 seconds before going back to deep sleep.
It only runs all code once, thus no timers should be needed.

Edit the post so we can read it.

It should be better know.
Removed the read_dht and notifications functions. They got all messed up in the copy and paste.
Those parts works, so the code should be easier to read without them.

Not strictly true but I have used millis() in this simulation as I don’t have a DHT.
You might need to tweak the livetime of 5800ms and there are ways to speed up the process.

[5001] Connecting to blynk-cloud.com:8442
[5189] Ready (ping: 2ms).

Blynk v0.4.0: Device started
SSID: MTN WIFI 19996

I'm awake. Transmitting Package
My Room
Blynk_Write was run
Hibernation time set to: 3
=================================
Sample DHT11...
Sample OK: Read DHT11 OK.
22 *C, 63 %
Temperature warning: 22C
Transmit complete

Hibernating for 3 minutes

Take a look through this, modify and then post your final code for other Blynkers with DHT sensors using deepSleep.

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

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//include <SimpleDHT.h>
//SimpleDHT11 dht11;

//PIN ASSIGNMENTS
//int DHT_data_pin = D6;
//int DHT_power_pin = D7;
unsigned int livetime = 5800;
const char* ssid = "xxxxxx"; 
const char* password = "xxxxxxx";
char auth[] = "xxxxxx";
char server[] = "blynk-cloud.com";

//Hibernation settings
int hibernation_time_min = 1; //Minutes

int humidity;
int temperature;

int low_temp_warning_threshold = 15;
int high_temp_warning_threshold = 40;
String room_name = "My Room";

//Initialize Blynk Modules
WidgetTerminal terminal(V3);

void setup() {
  //pinMode(DHT_power_pin, OUTPUT); 
  //digitalWrite(DHT_power_pin, LOW); 
  Serial.begin(115200);
  
  //Blynk.begin(auth,ssid,password,server);
   
  Blynk.config(auth,server);
  while (Blynk.connect() == false) {
    //Serial.print(".");
    //delay(500);
  }
  Blynk.syncVirtual(V0);
  
  //Do some printouts
  terminal.println("\nBlynk v" BLYNK_VERSION ": Device started");
  terminal.println("SSID: " + String(ssid));
  terminal.println(room_name);
  terminal.flush();
  Serial.println("\nBlynk v" BLYNK_VERSION ": Device started");
  Serial.println("SSID: " + String(ssid));
  Serial.println("\nI'm awake. Transmitting Package");
  Serial.println(room_name);
}

void loop() {
    //read_dht(); // done in sleep_routine()
    //notifications(); // done in sleep_routine()
    Blynk.run();
    sleep_routine();
}

void read_dht() {
  //digitalWrite(DHT_power_pin, HIGH); //Turn on power supply prior to reading DHT
  //delay(300);
  Serial.println("=================================");
  Serial.println("Sample DHT11...");
  
  // read without samples.
  byte _temperature = 0;
  byte _humidity = 0;
  //if (dht11.read(DHT_data_pin, &temperature, &humidity, NULL)) { //READ ON DIGITAL 6
    //Serial.print("Read DHT11 failed.");
    //terminal.println("Read DHT11 failed.");
    //return;
  //}
  
  Serial.print("Sample OK: ");
  Serial.println("Read DHT11 OK.");
  terminal.println("Read DHT11 OK.");
  terminal.flush();
  //temperature = int(_temperature); 
  //humidity = int(_humidity);
  temperature = 22; 
  humidity = 63;
  Serial.print((int)temperature); Serial.print(" *C, "); 
  Serial.print((int)humidity); Serial.println(" %");
  Blynk.virtualWrite(V16, temperature);
  Blynk.virtualWrite(V17, humidity);
  //digitalWrite(DHT_power_pin, LOW); //Turn off power supply to DHT to save battery
  delay(20);
}

void sleep_routine() {
  if(millis()  > livetime){
    read_dht();
    notifications();
    unsigned long hibernation_time_usec = hibernation_time_min * 60000000; 
    Serial.println("Transmit complete");
    Serial.println("\nHibernating for " + String(hibernation_time_min) + " minutes");
    terminal.println("Transmit complete. \nHibernating for " + String(hibernation_time_min) + " minutes"); 
    terminal.flush();
    ESP.reset();
    //system_deep_sleep(hibernation_time_usec); // microseconds. 10 000 000 = 10 seconds 
    delay(200); //Allow time to implement the sleep routine
  }
}

void notifications() {  // notify not available in simulation mode as loop < 15s
  if (temperature < low_temp_warning_threshold) {
    //Blynk.notify("Temperature warning: " + String(temperature) +"C");
  }
  if (temperature > high_temp_warning_threshold) {
    //Blynk.notify("Temperature warning: " + String(temperature) + "C");
  }
  Serial.println("Temperature warning: " + String(temperature) +"C");
}

BLYNK_WRITE(V0)  // this is not really needed as sync will do the job
{
  Serial.println("Blynk_Write was run");
  hibernation_time_min = param.asInt();
  Serial.println("Hibernation time set to: " + String(hibernation_time_min));
  terminal.println("Hibernation time set to: " + String(hibernation_time_min));
  terminal.flush();
}

@Costas
You are a genius! Thank you so much!
Blynk.run() needs to be allowed to run for a while before it downloads the data.
My code was designed to run every line of code only once, and this was not enough for the syncing to occur.
I only added the timer in my code to make the magic work.

Added livetime variable at top of code:
unsigned long livetime = 10000;

and at the top of sleep_routine I added:

void sleep_routine() {
while (livetime > millis()) { //Allow Blynk.run() to keep running for a while before going into sleep.
Blynk.run();
}
(The rest of the code in the sleep routine comes after the while loop) .

The purpose of this code is be batteryeffecient enough to run the NODE-MCU on Battery. That´s why it turns on the power to the DHT11 only before reading temperature and moisture and then turns it off again.

The full code is:

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

#define BLYNK_PRINT Serial 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleDHT.h>
SimpleDHT11 dht11;

//PIN ASSIGNMENTS
int DHT_data_pin = D6;
int DHT_power_pin = D7;

//WIFI
const char* ssid =      "Your SSID";    
const char* password =  "Your password";       
//BLYNK
char auth[] = "Your Blynk authorization code";

//Live time to allow blynk.run to run multiple times
unsigned long livetime = 10000;

//Hibernation settings
int hibernation_time_min = 1;  //Minutes

int humidity;
int temperature;

int low_temp_warning_threshold = 15;
int high_temp_warning_threshold = 40;
String room_name = "Your room name";



//Initialize Blynk Modules
WidgetTerminal terminal(V3);

void setup() {
  pinMode(DHT_power_pin, OUTPUT);        //Power supply for DHT11
  digitalWrite(DHT_power_pin, LOW);      //Initialize power supply low
  Serial.begin(115200);
  Blynk.begin(auth,ssid,password);                         //Connect to specified SSID and password
  while (Blynk.connect() == false) {
   Serial.print(".");
   delay(500);
  }
  Blynk.syncVirtual(V0);
  
  
  
  //Do some printouts
  terminal.println("\nBlynk v" BLYNK_VERSION ": Device started");
  terminal.println("SSID: " + String(ssid));
  Serial.println("\n I'm awake. Transmitting Package");
  terminal.println(room_name);
}


void loop() {
 read_dht();
 notifications();
 Blynk.run();
 sleep_routine();
}


void read_dht() {
  digitalWrite(DHT_power_pin, HIGH);     //Turn on power supply prior to reading DHT
  delay(300);
  Serial.println("=================================");
  Serial.println("Sample DHT11...");
  
  // read without samples.
  byte _temperature = 0;
  byte _humidity = 0;
  if (dht11.read(DHT_data_pin, &_temperature, &_humidity, NULL)) {        //READ ON DIGITAL 6
    Serial.print("Read DHT11 failed.");
    terminal.println("Read DHT11 failed.");
    return;
  }
  
  Serial.print("Sample OK: ");
  terminal.println("Read DHT11 OK.");
  temperature = int(_temperature); 
  humidity = int(_humidity);
  Serial.print((int)temperature); Serial.print(" *C, "); 
  Serial.print((int)humidity); Serial.println(" %");
  Blynk.virtualWrite(V16, temperature);
  Blynk.virtualWrite(V17, humidity);
  digitalWrite(DHT_power_pin, LOW);      //Turn of power supply to DHT to save battery
  delay(20);
  
}

void sleep_routine() {
  while (livetime > millis()) {
  Blynk.run();
  }
  unsigned long hibernation_time_usec = hibernation_time_min * 60000000;     //Convert time from min to usec.
  Serial.println("Transmit complete");
  Serial.println("\nHibernating for " + String(hibernation_time_min) + " minutes");
  terminal.println("Transmit complete. \nHibernating for " + String(hibernation_time_min) + " minutes"); 
  terminal.flush();
  system_deep_sleep(hibernation_time_usec);  
  delay(200); //Allow time to implement the sleep routine
  
}

void notifications() {
  if (temperature < low_temp_warning_threshold) {
    Blynk.notify("Temperature warning: " + String(temperature) +"C");
  }
  if (temperature > high_temp_warning_threshold) {
    Blynk.notify("Temperature warning: " + String(temperature) + "C");
  }
}

BLYNK_WRITE(V0)
{
  Serial.println("Blynk_Write(V1) was run");
  hibernation_time_min = param.asInt();
  Serial.println("Hibernation time set to: " + String(hibernation_time_min));
  terminal.println("Hibernation time set to: " + String(hibernation_time_min));
}