I need help it is not connecting with the internet but if i use blynk.begin() it will halt the code if there is no wifi connection I am using Arduino Mega2560 and Esp-01s

Read this before creating new topic:

  1. Search forum for similar topics
  2. Check https://docs.blynk.io
  3. Add details :
    • Hardware model + communication type. For example: ESP32
    • Smartphone OS (iOS or Android) + OS version
    • Blynk server region
    • Blynk Library version
    • Post your FORMATTED sketch code. Remove your AUTH TOKEN from code. Don’t post screenshots
    • Post your serial monitor output when experiencing some issues

:warning: If you don’t format your code, your topic can be deleted by moderators.

Here is a correct example of code formatting:

#include <Wire.h>
#include <U8g2lib.h>
#include <Adafruit_HDC1000.h>
#include <Adafruit_Sensor.h>


#define BLYNK_TEMPLATE_ID           "TMPL6nbINoFLa"
#define BLYNK_TEMPLATE_NAME         "Quickstart Template"
#define BLYNK_AUTH_TOKEN            "VLzoDUg8OgnDUuK6se_YmTFxkOiVh3mF"
#include <ESP8266_Lib.h>
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <BlynkSimpleShieldEsp8266.h>
char auth[] = "VLzoDUg8OgnDUuK6se_YmTFxkOiVh3mF";
char server[] = "blynk-cloud.com";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "PLDTHOMEFIBR2b770";
char pass[] = "17sRjamielg1701_";
int port = 8080;
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

Adafruit_HDC1000 hdc1080;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
BlynkTimer timer;

float temperature;
float humidity;
const int AirValue = 895;   // Replace with your values
const int WaterValue = 370; // Replace with your values
int soilMoistureValue = 0;
int soilmoisturepercentage = 0;
const int soilmoisturePin = A0;

bool isEspConnected = false;

void connectToWiFi()
{
  // Configure Blynk with Auth Token
  Blynk.config(wifi, auth, "blynk-cloud.com", 80);

  // Set up WiFi connection
  Blynk.connectWiFi(ssid, pass);

  // Check connection status
  if (Blynk.connected())
  {
    isEspConnected = true;
    Serial.println("Connected to Blynk server.");
  }
  else
  {
    isEspConnected = false;
    Serial.println("Connection to Blynk server failed.");
  }
}

void checkEspConnection()
{
  // Check if the ESP-01S is connected to the internet
  if (Blynk.connected())
  {
    isEspConnected = true;
    Serial.println("ESP connected to Blynk server.");
  }
  else
  {
    isEspConnected = false;
    Serial.println("ESP not connected to Blynk server.");
  }
}

void readHDC1080()
{
  temperature = hdc1080.readTemperature();
  humidity = hdc1080.readHumidity();
  soilMoistureValue = analogRead(soilmoisturePin);
  soilmoisturepercentage = map(soilMoistureValue, AirValue, WaterValue, 0, 100);

  if (isEspConnected)
  {
    Blynk.virtualWrite(V1, temperature);
    Blynk.virtualWrite(V2, humidity);
    Blynk.virtualWrite(V3, soilmoisturepercentage);
  }
}

void displayData()
{
  u8g2.clearBuffer();
  u8g2.setBitmapMode(1);
  u8g2.setFontMode(1);
  u8g2.setFont(u8g2_font_6x10_tr);
  u8g2.setCursor(3, 28);
  u8g2.print("T: ");
  u8g2.print(temperature);
  u8g2.print("°C");

  u8g2.setCursor(69, 28);
  u8g2.print("H: ");
  u8g2.print(humidity);
  u8g2.print("%");

  u8g2.setCursor(3, 50);
  u8g2.print("S: ");
  u8g2.print(soilmoisturepercentage);
  u8g2.print("%");

  u8g2.sendBuffer();
  delay(500);
}

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  pinMode(soilmoisturePin, INPUT);

  // Initialize the HDC1080 sensor
  hdc1080.begin();

  // Initialize the OLED display
  u8g2.begin();

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  // Connect to WiFi and Blynk
  connectToWiFi();

  // You can also specify the server:
  // Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, "blynk.cloud", 80);
  timer.setInterval(1000L, readHDC1080);
}

void loop()
{
  // Process incoming commands and maintain Blynk connection
  Blynk.run();

  if (isEspConnected)
  {
    readHDC1080();
    displayData();
    timer.run();
  }
  else
  {
    readHDC1080();
    displayData();
  }
}

Blynk.cloud.com is the URL for the legacy servers, and is now decommissioned.

The correct URL for Blynk IOT is Blynk.cloud

Blynk.begin is a blocking command. If you need a non-blocking connection system then search the forum for posts about Blynk.config and Blynk.connect

Pete.

oh okay that is why it is not connecting to the server? What command can i use instead of blynk.begin?

Update: it is connecting to the internet now but it doesnt connect to the blynk server and app.

can you help me fix it i am using arduino mega and ESP-01s and i cant use <ESP8266Wifi.h>. is there any way so that i can use blynk.begin and still run my code even if there is no wifi connection?

No, Blynk.begin is a blocking function.

You should probably switch to a board that supports IoT connectivity.

Pete.

Your last two posts have been deleted because you didn’t use triple backticks before and after your code.

Pete.

okay sir here is my code please i really need help :frowning:

#include <Wire.h>
#include <U8g2lib.h>
#include <Adafruit_HDC1000.h>
#include <Adafruit_Sensor.h>


#define BLYNK_TEMPLATE_ID           "TMPL6nbINoFLa"
#define BLYNK_TEMPLATE_NAME         "Quickstart Template"
#define BLYNK_AUTH_TOKEN            "VLzoDUg8OgnDUuK6se_YmTFxkOiVh3mF"
#include <ESP8266_Lib.h>
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <BlynkSimpleShieldEsp8266.h>
char auth[] = "VLzoDUg8OgnDUuK6se_YmTFxkOiVh3mF";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "PLDTHOMEFIBR2b770";
char pass[] = "17sRjamielg1701_";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

Adafruit_HDC1000 hdc1080;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
BlynkTimer timer;

float temperature;
float humidity;
const int AirValue = 895;   // Replace with your values
const int WaterValue = 370; // Replace with your values
int soilMoistureValue = 0;
int soilmoisturepercentage = 0;
const int soilmoisturePin = A0;

bool isEspConnected = false;

void connectToWiFi()
{
  // Configure Blynk with Auth Token
  Blynk.config(wifi, auth, "Blynk.Apps", 443);

  // Set up WiFi connection
  Blynk.connectWiFi(ssid, pass);

  // Check connection status
  if (Blynk.connected())
  {
    isEspConnected = true;
    Serial.println("Connected to Blynk server.");
  }
  else
  {
    isEspConnected = false;
    Serial.println("Connection to Blynk server failed.");
  }
}

void checkEspConnection()
{
  // Check if the ESP-01S is connected to the internet
  if (Blynk.connected())
  {
    isEspConnected = true;
    Serial.println("ESP connected to Blynk server.");
  }
  else
  {
    isEspConnected = false;
    Serial.println("ESP not connected to Blynk server.");
  }
}

void readHDC1080()
{
  temperature = hdc1080.readTemperature();
  humidity = hdc1080.readHumidity();
  soilMoistureValue = analogRead(soilmoisturePin);
  soilmoisturepercentage = map(soilMoistureValue, AirValue, WaterValue, 0, 100);

  if (isEspConnected)
  {
    Blynk.virtualWrite(V1, temperature);
    Blynk.virtualWrite(V2, humidity);
    Blynk.virtualWrite(V3, soilmoisturepercentage);
  }
}

void displayData()
{
  u8g2.clearBuffer();
  u8g2.setBitmapMode(1);
  u8g2.setFontMode(1);
  u8g2.setFont(u8g2_font_6x10_tr);
  u8g2.setCursor(3, 28);
  u8g2.print("T: ");
  u8g2.print(temperature);
  u8g2.print("°C");

  u8g2.setCursor(69, 28);
  u8g2.print("H: ");
  u8g2.print(humidity);
  u8g2.print("%");

  u8g2.setCursor(3, 50);
  u8g2.print("S: ");
  u8g2.print(soilmoisturepercentage);
  u8g2.print("%");

  u8g2.sendBuffer();
  delay(500);
}

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  pinMode(soilmoisturePin, INPUT);

  // Initialize the HDC1080 sensor
  hdc1080.begin();

  // Initialize the OLED display
  u8g2.begin();

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  // Connect to WiFi and Blynk
  connectToWiFi();

  // You can also specify the server:
  // Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, "blynk.cloud", 80);
  timer.setInterval(1000L, readHDC1080);
}

void loop()
{
  // Process incoming commands and maintain Blynk connection
  

  if (isEspConnected)
  {
    
    readHDC1080();
    displayData();
    timer.run();
    Blynk.run();
  }
  else
  {
    readHDC1080();
    displayData();
  }
}


image

This is the wrong syntax for Blynk.config.

I think you’ll find that Blynk.connectWiFi is also a blocking command.

Pete.

what is the right syntax for Blynk.config?

Blynk.config(wifi, auth, “Blynk.Apps”, 443);

what is the right port and server if i want to connect to my mobile app and pc dashboard?

Pete.