Unable to connect to the Blynk server

i tried to make a monitoring system using nodemcu esp8266 with 3 sensor (dht22,mq135,water level) for my project.

i’ve written this lines of code :

#include <ESP8266WiFi.h>
#include <DHT.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_TEMPLATE_ID "TMPL6z0RDKd8g"
#define BLYNK_TEMPLATE_NAME "fyp"
#define BLYNK_AUTH_TOKEN "MzNiiGHCM9XPH8JSHtx9T0fr00X6Owtq"

// Define your WiFi credentials and Blynk authentication token
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "bukan untuk kamu";
char pass[] = "brendenpaul";

// Initialize DHT sensor
#define DHTPIN D2 // Digital pin where the DHT22 is connected
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// Initialize analog pin for MQ135
const int mq135Pin = A0;

// Initialize digital pin for water level sensor
const int waterLevelPin = D3;

void setup() {
  Serial.begin(9600);

  // Connect to Wi-Fi
  Blynk.begin(auth, ssid, pass);

  // Initialize DHT sensor
  dht.begin();
}

void loop() {
  Blynk.run();

  // Read temperature and humidity from DHT22 sensor
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Read air quality from MQ135 sensor
  int airQuality = analogRead(mq135Pin);

  // Read water level sensor
  int waterLevel = digitalRead(waterLevelPin);

  // Print sensor data to Serial Monitor
  Serial.print("Temperature: ");
  Serial.println(temperature);
  Serial.print("Humidity: ");
  Serial.println(humidity);
  Serial.print("Air Quality: ");
  Serial.println(airQuality);
  Serial.print("Water Level: ");
  Serial.println(waterLevel);

  // Send data to Blynk app
  Blynk.virtualWrite(V1, temperature); // Virtual Pin V1 for temperature
  Blynk.virtualWrite(V2, humidity);    // Virtual Pin V2 for humidity
  Blynk.virtualWrite(V3, airQuality);  // Virtual Pin V3 for air quality
  Blynk.virtualWrite(V4, waterLevel);  // Virtual Pin V4 for water level

  delay(10000); // Delay for 10 seconds before reading the sensors again
}

i did connected the nodemcu to the server once but after troubleshooting the project because the input readings not correct. when i tried to reconnect the project again, it appear offline again. can anyone help?

just to add, the serial monitor doesnt show anything but blank

First thing you need to do is keep your void loop clean

billd

thank you bill, i did change my coding based on the link. but still got offline message

my new codes:

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

char auth[] = "O5u6ObuMwhfBtM59zSGF1cRxpXDFSnC-"; // Replace with your Blynk Auth Token
char ssid[] = "bukan untuk kamu"; // Replace with your Wi-Fi SSID
char pass[] = "brendenpaul";    // Replace with your Wi-Fi password

#define DHTPIN 2    // Signal pin of the DHT22 connected to GPIO 2
#define DHTTYPE DHT22 // DHT 22 (AM2302) sensor

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

const int MQ135Pin = A0;      // Analog pin for MQ135 gas sensor

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

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Read data from MQ135 gas sensor and send it to Virtual Pin 3
  int mqValue = analogRead(MQ135Pin);
  Blynk.virtualWrite(V3, mqValue);

  // Send temperature data to Virtual Pin 1 and humidity data to Virtual Pin 2
  Blynk.virtualWrite(V1, t); // Temperature to Virtual Pin 1
  Blynk.virtualWrite(V2, h); // Humidity to Virtual Pin 2
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  dht.begin();

  // Setup a timer to send data to Blynk every 1000ms (1 second)
  timer.setInterval(1000L, sendSensor);
}

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

@Brenden_Alt Please edit your posts, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

@PeteKnight what else should i do to fix my project?

Add:
#define BLYNK_PRINT Serial
near the top of your sketch, and ensure that your serial monitor is set to the baud rate you define in your Serial.begin() command.

You should then get some useful serial output that points you in the right direction.
If you need assistance with understanding what you’re seeing, or solving the issues that are highlighted in your serial monitor, then copy the serial monitor text and paste it (with triple backticks) into a post.
So not post a screenshot of a portion of your serial output.

Pete.

@PeteKnight i followed your instruction but serial monitor is still blank.
iill show you my full code to give more insight:

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

#define BLYNK_PRINT Serial // Add this line to enable Blynk debug output

char auth[] = "MzNiiGHCM9XPH8JSHtx9T0fr00X6Owtq"; // Replace with your Blynk Auth Token
char ssid[] = "bukan untuk kamu"; // Replace with your Wi-Fi SSID
char pass[] = "brendenpaul";    // Replace with your Wi-Fi password

#define DHTPIN 2    // Signal pin of the DHT22 connected to GPIO 2
#define DHTTYPE DHT22 // DHT 22 (AM2302) sensor

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

const int MQ135Pin = A0;      // Analog pin for MQ135 gas sensor

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

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Read data from MQ135 gas sensor and send it to Virtual Pin 3
  int mqValue = analogRead(MQ135Pin);
  Blynk.virtualWrite(V3, mqValue);

  // Send temperature data to Virtual Pin 1 and humidity data to Virtual Pin 2
  Blynk.virtualWrite(V1, t); // Temperature to Virtual Pin 1
  Blynk.virtualWrite(V2, h); // Humidity to Virtual Pin 2
}

void setup()
{
  Serial.begin(115200); // Set the baud rate to match your serial monitor
  Blynk.begin(auth, ssid, pass);
  dht.begin();

  // Setup a timer to send data to Blynk every 1000ms (1 second)
  timer.setInterval(1000L, sendSensor);
}

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

is there any specific reason for my project not perform as intended?
x

What baud rate is your serial monitor set to?

If this is your full code then you’re missing three lines of Blynk firmware configuration at the beginning of your sketch.

Pete.

baud rate at serial monitor is set to 115200.

and can you help, showing me the missing lines of Blynk firmware

Presumably you’ve created a template and device in the Blynk web console?
Go to web console > device > device info and you’ll see the three lines of firmware configuration.

Try removing all connections from your device except the USB cable and reboot. If your serial monitor is still blank then your device may be faulty.

Pete.

@PeteKnight when i disconnect all components and reupload the codes after adding the firmware configuration.
the serial monitor shown this:

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Soft WDT reset

Exception (4):
epc1=0x4000dd27 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffda0 end: 3fffffd0 offset: 0160
3fffff00:  00000000 0000194c 10e56041 00001137  
3fffff10:  00000000 0062d131 0000464f 00001137  
3fffff20:  0000464f 00000000 3ffeec44 00001137  
3fffff30:  0000464f 00000000 3ffeec20 40201848  
3fffff40:  3fffdad0 000001f4 00000f43 40204661  
3fffff50:  40239daa 3ffeec78 00000000 00000001  
3fffff60:  00000000 0000194c 1020c49b 001d3ff2  
3fffff70:  78cca8c0 00ffffff 98cca8c0 00001137  
3fffff80:  0000464f 00000000 3ffeec20 40201e11  
3fffff90:  40207aa0 78cca8c0 feefeffe feefeffe  
3fffffa0:  feefeffe feefeffe feefeffe 3ffeee3c  
3fffffb0:  3fffdad0 00000000 3ffeee10 40204708  
3fffffc0:  feefeffe feefeffe 3fffdab0 40100e75  
<<<stack<<<
--------------- CUT HERE FOR EXCEPTION DECODER ---------------

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3424, room 16 
tail 0
chksum 0x2e
load 0x3fff20b8, len 40, room 8 
tail 0
chksum 0x2b
csum 0x2b
v00046880
~ld
�a�n�r��n|�l�l`bbrl�nB�nl`�rl�l�

although the device is still shown offline on Blynk web console.
what are the lines on the serial monitor suppose to mean?

Your device is rebooting because of a watchdog timer issue, installing the exception decode in the IDE and pasting the result into it will give more info. Which you should share.

You should post your latest code.

Pete.

my full latest code:

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

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6z0RDKd8g"
#define BLYNK_TEMPLATE_NAME "fyp"
#define BLYNK_AUTH_TOKEN "MzNiiGHCM9XPH8JSHtx9T0fr00X6Owtq"

char auth[] = "MzNiiGHCM9XPH8JSHtx9T0fr00X6Owtq"; // Replace with your Blynk Auth Token
char ssid[] = "bukan untuk kamu"; // Replace with your Wi-Fi SSID
char pass[] = "brendenpaul";    // Replace with your Wi-Fi password

#define DHTPIN 2    // Signal pin of the DHT22 connected to GPIO 2
#define DHTTYPE DHT22 // DHT 22 (AM2302) sensor

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

const int MQ135Pin = A0;      // Analog pin for MQ135 gas sensor
const int WaterLevelPin = D3; // Digital pin for water level sensor

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

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Read data from MQ135 gas sensor and send it to Virtual Pin 3
  int mqValue = analogRead(MQ135Pin);
  Blynk.virtualWrite(V3, mqValue);

  // Read data from water level sensor and send it to Virtual Pin 4
  int waterLevel = digitalRead(WaterLevelPin);
  Blynk.virtualWrite(V4, waterLevel); // Water level data to Virtual Pin 4

  // Send temperature data to Virtual Pin 1 and humidity data to Virtual Pin 2
  Blynk.virtualWrite(V1, t); // Temperature to Virtual Pin 1
  Blynk.virtualWrite(V2, h); // Humidity to Virtual Pin 2
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  dht.begin();

  // Setup a timer to send data to Blynk every 1000ms (1 second)
  timer.setInterval(1000L, sendSensor);
}

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

You ignored the instructions in the web console to place those three lines at the very top of the sketch.

Also, this line is redundant…

if you do the sensible thing and replace this…

with this…

Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

Also, as the default baud rates for most ESP8266 boards is 74880 you’d be better using that as your serial baud rate.

Pete.

followed your instructions, still no changes.
I’m starting to think that i setup my template incorrectly.
can you guide me if im not troubling you?

It’s not a template issue.

You haven’t done this…

so its impossible to know the cause of the reboot problem.

Pete.