BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Using BLYNK_COONECTED reboots NodeMCU

Hello Everyone,

I am trying to connect all the lights in my room to Blynk. I am using a NodeMCU. I don’t have a private server and am using Blynk cloud server. I have just copied the code from the DHT example and it works fine. But when I add a BLYNK_CONNECTED() function with a Blynk.Syncall() command, the NodeMCU starts to reboot every few seconds. Also, when I try to turn on a pin through IFTTT and Webhooks, it reboots again. Please Help.

```


#define BLYNK_PRINT Serial
/*
   Pins Defined on app; D2, D4, D5, D6
*/

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>


char auth[] = "aG43gJ-SF*********nx-AGXal5IJw";


char ssid[] = "R******* 4";
char pass[] = "78*******";

#define DHTPIN 13         // What digital pin we're connected to D7

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}
BLYNK_CONNECTED()
{
Blynk.syncAll();
}

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

  Blynk.begin(auth, ssid, pass);
  

  dht.begin();

    `indent preformatted text by 4 spaces`
  timer.setInterval(1000L, sendSensor);
}

void loop()
{

  Blynk.run();
  timer.run();
}
```

try this

void setup() {
 // Debug console Serial.begin(9600); 
Blynk.begin(auth, ssid, pass); 
BLYNK_CONNECTED(); //<---
Blynk.syncAll(); //<----
dht.begin(); 
//`indent preformatted text by 4 spaces` 
timer.setInterval(3000L, sendSensor); //every 3 sec
}

Assuming you asked me to upload this code ( please forgive me if its wrong i am super dumb),



#define BLYNK_PRINT Serial
/*
   Pins Defined on app; D2, D4, D5, D6
*/

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>


char auth[] = "aG******************-AGXal5IJw";


char ssid[] = "R*****i 4";
char pass[] = "7********90";

#define DHTPIN 13         //  digital pin  connected to D7


#define DHTTYPE DHT11     

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;


void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

BLYNK_CONNECTED()
{
  Blynk.syncAll();
}

  void setup() {
 Serial.begin(9600);
Blynk.begin(auth, ssid, pass); 
BLYNK_CONNECTED(); 
Blynk.syncAll(); 
dht.begin(); 

timer.setInterval(3000L, sendSensor); 
}




void loop()
{

  Blynk.run();
  timer.run();
}

It still does the same thing.

Personally, I think your original code was correct, as the BLYNK_CONNECTED needs to be a callback function, which it isn’t with @Blynk_Coeur’s modification (more coffee required?).
I assume that this line was an unwanted addition caused when you posted the code?:

Using Blynk.SyncAll is bad practice and can cause issues. Much better to sync each required pin. That may be what’s causing one of your issues.

Do you actually have a DHT11 connected, as I don’t see the connection between this and your aim of turning lights on an off?
Trying to read a DHT11 every second is a bit ambitious, and may be causing one of your other issues.

Pete.

1 Like

Yes, I do have a dht11 connected. I did this just because I could and I also wanted to know how to use virtual pins.

Please guide me on how to do that.

Thanks in advance,
Shmoke

Hi,
For example, check below code

// Every time we connect to the cloud...
// Anything written inside BLYNK_CONNECTED() will get executed once when blynk is connected after some disconnection
BLYNK_CONNECTED() {
  // Request the latest state from the server
 Blynk.syncVirtual(V18); 
 Blynk.syncVirtual(V24);

//clear terminal windows on blynk connection
 terminal.clear(); 
 terminal_HTTP.clear(); 

 //Alternatively, you could override server state using: sending local LEDs state to blynk app virtual pins - giving priority to local states
 Blynk.virtualWrite(V12, led1State); 
 Blynk.virtualWrite(V13, led2State);
 Blynk.virtualWrite(V14, led3State);
 Blynk.virtualWrite(V15, led4State);
 Blynk.virtualWrite(V16, HIGH);
 Blynk.virtualWrite(V17, HIGH); 
 Blynk.virtualWrite(V19, HIGH); 

// updating some local variables on blynk connection
 indicator = true; 
 result=true;
 Blynk_Connected=true;
}

Regards,
Gaurav Barwalia

Please clear my doubt. Does Blynk.syncAll sync only the virtual pins?

Yes , only virtual pins on your app
How many virtual pins do you have?

@Blynk_Coeur, you’re not having a good day my friend!

Blynk.syncAll()
Requests all stored on the server latest values for all widgets. All analog/digital/virtual pin values and states will be set to the latest stored value. Every virtual pin will generate BLYNK_WRITE() event.

Pete.

1 Like

@Shmoke I’m not sure that you reall understand the purpose of synchronisation. It serves no purpose in the sketch you are using, so is a waste of time.

In your planned project (light controller) it could be quite useful, but there is a case for not syncing in that scenarios - more on that later…

Assume you have a button widget to control your lights and it’s connected to pin V1. You’ll have s9me code in your sketch that looks something like this:

BLYNK_WRITE (V1)
{
  if (param.asInt())
  {
    // code in here to activate your relay to turn the lights on...
  }
  else 
  {
    // code in here to deactivate your relay - lights off...
  }
}

This is, in effect, a callback function that is called automatically by the code whenever the Blynk server identifies a change in state of pin V1.
What Blynk.syncVirtual(V1) does is to force the Blynk server to trigger the BLYNK_WRITE(V1) callback, and synchronise the state of your lights with the last value that was stored on the server.

This can be a problem if you leave the home with the lights switched off, but not knowing that your NodeMCU is offline. When it comes back online it will synchronise with the app/server and this may put the lights on when you don’t want them on.

I’m assuming that you’ll also want to use a physical switch to control your lights, so it may be better to experiment with the “sync physical button” sketch from the sketch builder.

Pete.

I have always believed that Blynk.syncAll , only sync Vpins :alarm_clock::rofl:

First off, I want to thank all of you for being so helpful.

I am currently having my exams. So I was unable to update for the last month. I have seen all the advice given by the great teachers.

I have also updated my code so that all the pins are virtual now.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

#define DHTPIN 13
#define DHTTYPE DHT11 

#define relay1 16 
#define relay2 5
#define relay3 4 
#define relay4 0



char auth[] = "G_fjsuV_Ar***********nU0gsgF2";

char ssid[] = "M**********1";
char pass[] = "s********";

BlynkTimer timer;


BLYNK_WRITE(V7)
{
  int v = param.asInt();
  digitalWrite(relay1, v);
}

BLYNK_WRITE(V8)
{
  int v = param.asInt();
  digitalWrite(relay2, v);
}

BLYNK_WRITE(V9)
{
 int v = param.asInt();
  digitalWrite(relay3, v);
}

BLYNK_WRITE(V10)
{
  int v = param.asInt();
  digitalWrite(relay4, v);
}

BLYNK_WRITE(V11){  //Function to turn everything off or on

  int v = param.asInt();

  if(v == 1)
 {
    digitalWrite(relay1, HIGH);
    Blynk.virtualWrite(V7, HIGH);

    digitalWrite(relay2, HIGH);
    Blynk.virtualWrite(V8, HIGH);

    digitalWrite(relay3, HIGH);
    Blynk.virtualWrite(V9, HIGH);

    digitalWrite(relay4, HIGH);
    Blynk.virtualWrite(V10, HIGH);    
  }

  else
  {
    digitalWrite(relay1, LOW);
    Blynk.virtualWrite(V7, LOW);

    digitalWrite(relay2, LOW);
    Blynk.virtualWrite(V8, LOW);

    digitalWrite(relay3, LOW);
    Blynk.virtualWrite(V9, LOW);

    digitalWrite(relay4, LOW);
    Blynk.virtualWrite(V10, LOW); 

  }
}

DHT dht(DHTPIN, DHTTYPE);

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V7);
  Blynk.syncVirtual(V8);
  Blynk.syncVirtual(V9);
  Blynk.syncVirtual(V10);
 }
void setup()
{

//  Serial.begin(9600);

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

timer.setInterval(1000L, sendSensor);

}

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

But now the problem is that the NODEMcu does not respond at all. Can you please help me?

I have two-way switches in my house. So I was going to connect it to that (it is possible, right?) so that even if the internet was down, I could control the lights.

I understand it and it can be a big problem. Can you help me with mitigating this issue?

PS: I had left the project for a while. After I reuploaded the same code that I was uploading earlier, the issue of reboots was solved!!!

What EXACTLY does this mean?

Have you tried adding some Serial.print statements into each of your functions so that you can track the execution of the code?

It’s potentially possible, but it depends on your wiring and what you’re planning to do.

The simple solution is to have the devices turn the relays off when it boots, and to update the Blynk app, rather than synchronising the values from the server.

Pete.

I think ther is some problem with my hardware for every time I upload the code, something does not work but starts working properly after uploading the exact same code again. So, the problem has been solved by re-uploading the code.

But I have the problem that all the lights relays turn on even if all of them are turned off in the app.

Are you saying that the operation of the relays is opposite to the app - on when they should be off and off when they should be on?

Or, are you saying that the widget buttons attached to V7, 8, 9 & 10, plus V11 have no effect on the status of your relays?

I’d suggest that you make a greater effort to describe EXACTLY what your issues are, or forum users will very quickly tire of trying to extract the required information from you.

Pete.

I’m sorry. I will try to explain.

First of all, the problem of the buttons not changing the state of the relays was fixed by re uploading the code to the nodemcu . Also, the relays are on when voltage is low.
Next, whenever the hardware is connected to WiFi, all the relays turn on regardless of last saved state in app. This is the current issue I am having. Please help me in fixing it.

What do you mean by this?

You don’t have any pinMode statements in your code, so I’m surprised if anything is working with your relays.

Pete.

2 Likes

I’m extremely sorry for being a complete idiot.

I meant to say that the relays are active-low ( I forgot that word).

I TOTALLY forgot about that!!! Thank you so very much. Now my project is working perfectly, just how I wanted it to.

Again. Thank You So MUCH!!!

3 Likes