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){
      digitalWrite(D,HIGH);}
      else {digitalWrite(C,LOW); digitalWrite(D,LOW);}}

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


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

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

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

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

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


BLYNK_CONNECTED() {
Blynk.syncAll();
}

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
delay(100);
Blynk.disconnect();
delay(100);
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

1 Like

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)) {
    Blynk.connect();
  }
  // stuff...
}
void loop() {
  timer.run();
  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  } 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 #");
      Serial.println(ReCnctCount);
      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)) {
        Blynk.connect();
      }
    });  // 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

Hi everyone,

I tried to use a physical push button, and programmed it like this:
> if (digitalRead(switch))
> * {*
> * delay(200);*
> * Blynk.begin(auth, ssid, pass);*
> * }*

and successfully the circuit worked as a separate (like in offline mode) and once I pressed the switch it connects to WiFi then to Blynk.

BUT >>>>>

by checking serial monitor I found the board is restarting every 1:2 seconds with this error:

[5001] Connecting to 0.0.0.0
/home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/queue.c:1442 (xQueueGenericReceive)- assert failed!
abort() was called at PC 0x400893c5 on core 1

Backtrace: 0x4008cfc4:0x3ffb1d20 0x4008d1*****

Rebooting…
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8

If you have a comment please share.

Thanks all.
Mohamed

It seems to be a hardware issue, not a blynk issue.
I assume you are using an ESP32

I spent most of Friday trying to figure-out why some code for the ESP32 worked fine until I added-in an ArduinoOTA.begin() command, at which point I started getting constant reboots with an assert failed command.

It turns-out that I had some files missing from the Arduino/hardware folder, which only came to light when I went into Boards Manager and stared getting warning messages. Once I re-installed the ESP32 core all was good.

The strange thing was that I was able to compile and run a much more complex piece of code without any problem, which also used OTA, so I assumed my IDE install was good.

It feels like I’m a newbie all over again now I’ve started using ESP32s :dizzy_face:

Pete.

1 Like

I am using ArduinoOTA.begin() too,
and I use ESP32 core V 1.04
All is running well for me :thinking:

All running well for me now, after re-installing the ESP32 core :slightly_smiling_face:

Pete.

1 Like