BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Luminaire Management

Hi, let me tell you what I was working on. it’s about automating the lights in my room. Get it all working. The only drawback is that a few hours pass and the system is disconnected with the cell phone’s APP. Here I leave the programming and connections. I hope you can help me. Thanks a lot

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

#include <SoftwareSerial.h>


char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxx";                           //WiFi que quiero conectar al Esp8266
char pass[] = "xxxxxx";                           //Contraseña del WiFi

int btnPin = D2;          
int RelayPin = D8;  
int LM = D3;
int apagado;       

BlynkTimer timer;
void checkPhysicalButton();
int btnPinState = LOW;           // ON
int RelayPinState = HIGH;

#define TURN_ON 0                 // TURN_ON and TURN_OFF are defined to account for Active High relays
#define TURN_OFF 1                // Used to switch relay states

void setup()
{
  Serial.begin (9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(RelayPin, OUTPUT);            //  initialize your pin as output.
  pinMode(LM, OUTPUT);
  pinMode(btnPin, INPUT);        //  initialize your pin as input with enable internal pull-up resistor "input_pullup"
  digitalWrite(RelayPin, TURN_OFF);     // remain off till command is receive

  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);
  timer.setInterval(10L, prendido);

  apagado=0;
}

// Every time we connect to the cloud...
BLYNK_CONNECTED()
{
  // Request the latest state from the server
  Blynk.syncVirtual(V1);
}

// When App button is pushed - switch the state
// Map this Virtual Pin to your Mobile Blynk apps widget.
BLYNK_WRITE(V1)
{                           
  RelayPinState = param.asInt();
  digitalWrite(RelayPin, RelayPinState);
}

void checkPhysicalButton()
{
  digitalWrite(RelayPin, RelayPinState);

  if (digitalRead(btnPin) != btnPinState)          
  {
    digitalWrite(btnPin,btnPinState);
    btnPinState = !btnPinState; 

    if(RelayPinState == LOW)
    {
      digitalWrite(RelayPin,HIGH);
      RelayPinState = HIGH;
      Blynk.virtualWrite(V1, RelayPinState);
    }
    else
    {
      digitalWrite(RelayPin,LOW);
      RelayPinState = LOW;
      Blynk.virtualWrite(V1, RelayPinState);
    }

  }
  
}

BLYNK_WRITE(V2)                 //lectura del botón virtual V2
{ 
  int pinValue = param.asInt();
  if (pinValue == 1) {
    digitalWrite(LM,HIGH);
  }
  else {
    digitalWrite(LM,LOW);
  }
}

BLYNK_WRITE(V3)                 //lectura del botón virtual V2
{ 
 int pinValue = param.asInt();
  if (pinValue == 1) {
    apagado=1;
  }
  else {
    apagado=0;
  } 
}

void prendido()
{
  if(apagado == 0)
  {
    digitalWrite(D5,LOW);
    digitalWrite(D6,LOW);
    digitalWrite(D7,LOW);
  }
}

void loop()
{
  Blynk.run();                               // Run Blynk
  timer.run();                               // Run SimpleTimer
}

Elements:
-Wemos D1
-Rele plate
-Led strip (12v)
-PC source (BCP450-OS)
-Resistance 1k
-Key switch
-TIP122 transistors
-Lamp 12v DC
-Lamp 220v AC

What does your serial monitor show when a disconnection from the Blynk server occurs?

Your code is very odd…

Why are you including the SoftwareSerial library?

Why are you calling the prendido function 100 times every second? Calling it with a timer is totally unnecessary. It only needs to be called when the value of the widget attached to V3 changes, so why not call it directly from the BLYNK_WRITE(V3) callback?

By using Blynk.begin your physical buttons won’t work if a connection to your WiFi or to the Blynk server can’t be established. You should manage your own WiFi connection then use Blynk.config and Blynk.begin.

Pete.

Hi Pete, excuse me, I’m new to this Blynk thing. The SoftwareSerial library is there when I tested the programming on the serial monitor, I will remove it.
Thanks for the help with the BLYNK_WRITE (V3). I will do it as you told me.
I did not understand about the Blynk.begin.
From already thank you very much !