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

NodeMCU keeps going offline

Before creating the topic

  1. Search forum for similar topics
    I tried the BlynkTimer did not worked and also i tried without any timesrs or delays, still same issue
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. NodeMCU v3 - wifi
    • Smartphone OS (iOS or Android) + version: Latest android version
    • Blynk server or local server - Local server
    • Blynk Library version 0.6.1
    • Add your sketch code.

#define BLYNK_PRINT Serial

#include "DHT.h"                              // DHT Sensor
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "";
char ssid[] = "";
char pass[] = "";

#define SensorPin A0

IPAddress local_IP(192, 168, 88, 8);
IPAddress gateway(192, 168, 88, 1);

IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8);
IPAddress secondaryDNS(8, 8, 4, 4);

DHT dht(5, DHT21);
BlynkTimer timer;

void notifySoil(float prt, String type)
{
  if (prt < 52.00 && type == "soil") {
    Blynk.notify("Alert Soil Hum low: " + String(prt) + "%");
  };
  if (prt > 27.00 && type == "temp") {
    Blynk.notify("Alert Temp high: " + String(prt) + "C");
  };
   if (prt < 21.00 && type == "temp") {
    Blynk.notify("Alert Temp low: " + String(prt) + "C");
  };
}

void climateRoutine() {
    int dryValue = 1023;
    int wetValue = 0;
    int friendlyDryValue = 0;
    int friendlyWetValue = 100;
    float percentage = 0.0;
    byte h1 = dht.readHumidity();        
    byte t1 = dht.readTemperature();
    float sensorValue = analogRead(SensorPin);
    percentage = map(sensorValue, dryValue, wetValue, friendlyDryValue, friendlyWetValue);
    Blynk.virtualWrite(V0, t1);
    Blynk.virtualWrite(V1, h1);
    Blynk.virtualWrite(V2, percentage);
    notifySoil(percentage, "soil");
    notifySoil(t1, "temp");
    Serial.println(t1);
    }

void setup() {
  Serial.begin(9600);
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
     Serial.println("STA Failed to configure");
  };
  Blynk.begin(auth, ssid, pass, IPAddress(192,168,88,7), 8080);
  dht.begin();
  timer.setInterval(40000L, climateRoutine);
}


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

Initially i was using the public server, and then i moved to a local one, I thought maybe my devboard would make to many requests or something adn I wanted to see the logs.

Did not found anything relevant in the logs.

Then i found some forum posts about not using delay in the code, so i tried using BlynkTimer, but same issues persisted.

Then i eliminated all trimmers or delays and still the same issue.

Can’t find anything else on the forum or google…

I am new to c and maybe i am doing something wroang in the code, idk…

Your code is structured so that every cycle of the void loop you try to do multiple Blynk.writes and also potentially send multiple Blynk notifications.
This doesn’t work!

Using a timer to call your climateRoutine function, maybe once every 10 seconds is the solution, plus you should use flags to indicate that a notification has been sent when your alert criteria have been met so that you don’t then keep re-sending the same notification. You would then clear the flag when the criteria is no longer met, to enable future alerts to be sent.

You should read this:

and if your use of a timer doesn’t work then post your revised code along with an explanation of what is and isn’t working.

Pete.

Thanks for the answer,
I already cleaned the code.
Also, thanks for the tag pointers, for the notifications.

Now that i have defined #define BLYNK_PRINT Serial, I am seeing the following in the console:

[526787] Connecting to 192.168.88.7
[531788] Connecting to 192.168.88.7
[536789] Connecting to 192.168.88.7
[541790] Connecting to 192.168.88.7
[546791] Connecting to 192.168.88.7
[546796] Ready (ping: 2ms).
255
[575867] Heartbeat timeout
[577870] Connecting to 192.168.88.7
[582871] Connecting to 192.168.88.7
[587872] Connecting to 192.168.88.7
[592873] Connecting to 192.168.88.7
[597874] Connecting to 192.168.88.7
[602875] Connecting to 192.168.88.7
255
[607876] Connecting to 192.168.88.7

I don’t think this is normal, because it still behaves really strange, the value do not get updated in the adnroid app, the servers goes down, but when i click on it says it is up, and so on.

Same thing with cloud:

   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.0 on NodeMCU

[4485] Connecting to blynk-cloud.com:80
[4654] Ready (ping: 37ms).
255
[53759] Heartbeat timeout
[55778] Connecting to blynk-cloud.com:80
[60779] Connecting to blynk-cloud.com:80
[65780] Connecting to blynk-cloud.com:80
[70781] Connecting to blynk-cloud.com:80
[75782] Connecting to blynk-cloud.com:80
[80783] Connecting to blynk-cloud.com:80
255


Is this the t1 value?

Pete.

Yes.

I found my issue, apparently if I put the included files in this order:

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

I do not have an issue anymore.

Who knew, the order was so important :slight_smile: