How ton run program without connected to wifi or internet, use blynk only to monitor when it required

Please help.

How ton run program without wifi, use blynk only to monitor when it required.

I have searches the same topic but they use different library or device so it’s not work in my project.
They use ESP8266Wifi.h and etherned. I use esp8266_lib.h as the library.

I use arduino mega and esp8266 as a shield.
I want to run my program without it connected to wifi, but I want the device send data to blynk when it required and anytime I want. It temporary data not a historycal data so it doesn’t need to save data to blynk cloud server.

Here is my code

#define A 22
#define B 23
#define C 24
#define D 25
int counter = 0;
int diam;
int akhir;
float volt;
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "54737a98fcd843968facd53968b53691";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "jonihermawanto";
char pass[] = "123456789";

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

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

BlynkTimer timer;
void saya(){
  int sensorValue = analogRead(A14);
  float volt = sensorValue*(5.00/1023.00);
  Serial.print("voltase :  ");
  Serial.println (volt);
    Blynk.virtualWrite(V4, volt);
void lampu(){
  int sensorValue = analogRead(A14);
  float volt = sensorValue*(5.00/1023.00);
  if (volt>=4.01){
    digitalWrite(C, HIGH);}
    else if (volt <=3.82){
      else {digitalWrite(C,LOW); digitalWrite(D,LOW);}}

void sensor(){
  if (diam!=akhir){
      counter ++;
        counter --;}
      Serial.print("Posisi :  ");
       Blynk.virtualWrite(V5, counter);}

void setup (){
  pinMode(A, INPUT);
  pinMode(B, INPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  Serial.begin (9600);
  timer.setInterval(1000, saya);
  timer.setInterval(100 , sensor);
  timer.setInterval(10, lampu);

  // Set ESP8266 baud rate

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);

void loop (){
  if (Blynk.connected()){;};

Just an idea (and untested), but maybe you could use another timer that would then call the blynk connection stuff, then add a button in blynk to disconnect.

in setup add

timer.setInterval(120000, connect); //run every 2 minutes

and remove (?)

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

and then add to code


void connect();

  bool result = Blynk.connected();
   if (!result)
  Blynk.begin(auth, wifi, ssid, pass);

BLYNK_WRITE(V0) //button widget on V0, set to switch
  int value = param.asInt(); // Get value as integer

  if (value == 1)
// Send integer
Blynk.virtualWrite(V0, 0); //turn switch off
WiFi.disconnect(); //???


You could probably do something similar using a physical button (to connect and disconnect) as well.

yes , physical button is needed, else you can’t reconnect to blynk server

The re-connection would happen automatically with the use of the timed routine.

But the button option could be implemented additionally, or instead of, to force a re-connection on demand.

is that he wants to do

Sounds like he wants both.

I was just throwing an example out there to give an idea how it might be done.

The adding a button would be simple. Just add another timed routine and check button/pin state. if pressed, start the connection process. If a switch was used, one state could be to connect, the other to disconnect.

1 Like

In reference to both your topics about these libraries… This is part of your issue…

The ESP8266 in your case is ONLY acting as a Serial to WiFi transceiver (AKA as Shield), thus it uses different libraries and has different functions then a full standalone ESP8266.

I also use a Mega with ESP as shield… and am able to run without Blynk connection via this code…

This is for an auto reconnection routine…

#include <WiFi.h>
#include <ESP8266_Lib.h>  // ESP-01 Link
#include <BlynkSimpleShieldEsp8266.h>  // ESP-01 Link
void setup() {
  // stuff...
  wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
  Blynk.config(wifi, auth, server, port);
  if (Blynk.connectWiFi(ssid, pass)) {
  // stuff...
void loop() {;
  if (Blynk.connected()) {  // If connected run as normal;
  } else if (ReCnctFlag == 0) {  // If NOT connected and not already tring to reconnect, set timer to try to reconnect in 30 seconds
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 30 seconds...");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
      ReCnctFlag = 0;  // Reset reconnection Flag
      ReCnctCount++;  // Increment reconnection Counter
      Serial.print("Attempting reconnection #");
      wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
      Blynk.config(wifi, auth, server, port);
      Blynk.connect();  // Try to reconnect to the server
      if (Blynk.connectWiFi(ssid, pass)) {
    });  // END Timer Function

This is NOT drop in code… you will need to examine it and extrapolate what you need for your own projects…

1 Like