Using light switches when network unavailable

Hi,

I’ve searched forums but only found old suggestions from 2016.

Im using an uno with ethernet connection with a light switch that I can physically trigger the relay with a switch or via blynk.

But if I don’t have network I can’t use my light switches and get stuck in the void setup.

Any suggestions to get around this?

Thanks.

Offline functionality has been discussed many times, and the principals are the same for Blynk Legacy and IoT.

Blynk.begin is a blocking function, so you have to manually manage your internet connection then use Blynk.config and optionally Blynk.connect() or Blynk.connect(timeout).
You also have to ensure that Blynk.run is not executed if Blynk.connected() == false as this will cause a connection attempt for either the default timeout period or the period specified in Blynk.connect(timeout).

Pete.

Thanks for the reply. I found this that you helped with:

I Have done the following:

void setup() 
{

  Serial.begin(9600);
  //Ethernet.begin(mac, ip);
 // Blynk.begin(auth, "blynk.cloud", 8080);

    Blynk.config(auth, "blynk.cloud", 8080);  // Initialise the Blynk connection settings
    Blynk.connect();   

  
  pinMode(3, OUTPUT); //relay 1
  pinMode(5, OUTPUT); //relay 2
  pinMode(6, INPUT_PULLUP ); //shed1 light switch
  pinMode(9, INPUT_PULLUP ); //shed2 light switch


    
   timer.setTimeout(3600000L, [] () {} ); // dummy/sacrificial Function timers with ID 0 cannot be turned on/off
   timer.setInterval(500, Timer); 



}

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

  timer.run();
}

I can now get into the timer, but Blynk doesnt connect

Probably not a great example because that’s a sketch aimed at using deep sleep, but it does contain all the necessary stuff - albeit for a WiFi board not an Ethernet board.

Not surprising, because you’ve skipped this step…

You’ll almost certainly need an Ethernet.begin() command to initialise your Ethernet connection, and this will need the necessary arguments such as MAV Address, IP Address, DNS, Gateway, Subnet Mask etc.

You’ll also need to put your CS pin in the correct state to ensure that it’s your Ethernet port rather than your SD card that’s being used.

Pete.

byte mac[] = {0xDA, 0xFD, 0xDE, 0xFD, 0xFE, 0xFD};
IPAddress ip ( 10, 20, 10, 75);
IPAddress myDns(1, 1, 1, 1);
IPAddress gateway(10, 20, 10, 1);
IPAddress subnet(255, 255, 248, 0);


void setup() 
{

  Serial.begin(9600);
  Ethernet.begin(mac, ip, myDns, gateway, subnet);

   Blynk.config(auth, "blynk.cloud", 8080);  // Initialise the Blynk connection settings
   Blynk.connect();   

  
  pinMode(3, OUTPUT); //relay 1
  pinMode(5, OUTPUT); //relay 2
  pinMode(6, INPUT_PULLUP ); //shed1 light switch
  pinMode(9, INPUT_PULLUP ); //shed2 light switch


    
   timer.setTimeout(3600000L, [] () {} ); // dummy/sacrificial Function timers with ID 0 cannot be turned on/off
   timer.setInterval(500, Timer);



}

void loop() 
{

  Blynk.run();
  timer.run();
}

Now done this, still no connection. I dont have an SD card plugged in, I’ve never done anything with the CS port, what should I be looking at? I found this on a tutorial for the ethernet controller, but it didnt help Ethernet.init(10);

I have tried pinging the IP and that fails

Thanks

It would help if you posted your full sketch.

This isn’t a Blynk specific thing, it’s standard practice for Ethernet shields. It involves setting GPIO4 and 10 to the correct statuses to activate the Ethernet port and de-select the SD card.

Pete.

#define BLYNK_TEMPLATE_ID "x"
#define BLYNK_DEVICE_NAME "Shed Light Relays"
#define BLYNK_AUTH_TOKEN "x"

#define BLYNK_PRINT Serial

#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>


#include <SPI.h>
#include <WidgetRTC.h> //blynk time server

WidgetRTC rtc;

char auth [] = BLYNK_AUTH_TOKEN;

int extlight;
int lightcheck;
int shed1check;
int shed2check;



BlynkTimer timer;


BLYNK_CONNECTED() 
{
  // Synchronize time on connection
  rtc.begin();
  
  Blynk.syncVirtual(V0); //both lights
  Blynk.syncVirtual(V1); //shed1 lights
  Blynk.syncVirtual(V2); //shed2 lights

}

byte mac[] = {0xAX, 0x61, 0x0X, 0xAE, 0xXX, 0xXX}; //specific to the new ethernet shield
IPAddress ip ( 10, 20, 10, 18);
IPAddress myDns(1, 1, 1, 1);
IPAddress gateway(10, 20, 10, 1);
IPAddress subnet(255, 255, 248, 0);

////////////////////////////////////////

void setup() 
{

  Serial.begin(9600);
  Ethernet.init(10);
  Ethernet.begin(mac, ip, myDns, gateway, subnet);


 // Blynk.begin(auth, "blynk.cloud", 8080);

   Blynk.config(auth, "blynk.cloud", 8080);  // Initialise the Blynk connection settings
   Blynk.connect();   

  
  pinMode(3, OUTPUT); //relay 1
  pinMode(5, OUTPUT); //relay 2
  pinMode(6, INPUT_PULLUP ); //shed1 light switch
  pinMode(9, INPUT_PULLUP ); //shed2 light switch


    
   timer.setTimeout(3600000L, [] () {} ); // dummy/sacrificial Function timers with ID 0 cannot be turned on/off
   timer.setInterval(500, Timer); // Miliseconds to seconds, every 1/2 second run function which counts time and potentially turns the light off



}

////////////////////////////////////////

void loop() 
{

  Blynk.run();
  timer.run();
}

////////////////////////////////////////
BLYNK_WRITE(0)//Lights on via Blynk

{  if ( param.asInt() == 1 )
    {
           digitalWrite(3, HIGH);
           digitalWrite(5, HIGH);
           extlight = 0;
           lightcheck = 1;
    }      
    else
    {      
          digitalWrite(3, LOW);
          digitalWrite(5, LOW);
          extlight = 0;
          lightcheck = 0;     
    } 
}


////////////////////////////////////////

void Timer()

{

    Serial.println("Entered Timer"); //just here to test with no ethernet connection
  
    //Checking to turn off light switches
     if (lightcheck == 1)
     {
        extlight ++;
     
        if(extlight >= 600) //if 5 minutes has passed turn the light off. (600 because timer runs every 1/2 second)
        {
            digitalWrite(3, LOW);
            digitalWrite(5, LOW);
            Blynk.virtualWrite(0, LOW); //V0
            extlight = 0;
            lightcheck =0;
            
        }
        
     }

     if (digitalRead(6) == LOW) //Shed1 Switch, goes low when switch is pressed //ON
     {
     digitalWrite(3, HIGH); //Shed1 Relay
      shed1check = 1;
      Blynk.virtualWrite(V1, HIGH);
     }

     if (digitalRead(6) == HIGH && shed1check == 1) //Shed1 Switch //OFF
     {
     digitalWrite(3, LOW); //Shed1 Relay
     shed1check = 0;
     Blynk.virtualWrite(V1, LOW);

     }

///////////////////
     
     if (digitalRead(9) == LOW) //Shed2 Switch, goes low when switch is pressed //ON
     {
     digitalWrite(5, HIGH); // Shed2 Relay
      shed2check = 1;
      Blynk.virtualWrite(V2, HIGH);
     }

     if (digitalRead(9) == HIGH && shed2check == 1) //Shed2 Switch //OFF
     {
     digitalWrite(5, LOW); //Shed2 Relay 
     shed2check = 0;
     Blynk.virtualWrite(V2, LOW);
     }

}

Blynk gets to:
22:53:58.045 → [5356751] Connecting to blynk.cloud:8080