Update widget button status through Hardware

Hello community!
My project is working “perfectly”, as the one I wanted …
I believe that this code is not perfect, but it fulfills my needs …
I wanted to add one more condition to it:
if (standby3 == true) {
if (Lampada == true) {
Lampada = false;
}
} …
So far so good …
But how do I change the state of the app button (if true) to false?
In other words, it is a lamp, and I would like it to be turned off in a certain condition, and that the state of the widget button should also be turned/write off?
Follow the code below …
Thank you!
Congratulations for all the work developed!
OBS: The code is written and commented in Portuguese …:wink:
But the essential is there, it is universal! … :slight_smile:

Arduino Mega
Ethernet Board

//Local Server em Raspberry Pi



// v0 - estado maquina                        - OUT
// v1 - Hora                                  - OUT
// v2 - Data                                  - OUT
// v3 - tempo de ficheiro em minutos (m)      - OUT
// v4 - Led Refresh                           - OUT
// v5 - Humidade do ar                        - OUT
// v6 - Temperatura ambiente                  - OUT
// v7 - LED Desligado                         - OUT
// v8 - LED Aguardar Fich                     - OUT
// v9 - LED Exec Fich.                        - OUT
// v10 - LED Standby                          - OUT
// v11 - LED Chiller/Refrigeração             - OUT
// v12 - controle temperatura                 - IN
// v13 - controle Tempo ficheiro              - IN 
// v14 - tempo aguardar ficheiro minutos (m3) - OUT
// v15 - Led temperatura excesso              - OUT
// v16 - hora de ficheiro (h)                 - OUT
// v17 - tempo aguardar ficheirosegundos (s3) - OUT
// v18 - Controlo de tempo standBy /desligar  - IN
// v19 - Tempo de ficheiro segundos (s)       - OUT
// v20 - tempo standby segundos (s2)          - OUT
// v21 - tempo stanby minutos (m2)            - OUT
// v22 - Temperatura Sonda água               - OUT
// v23 - Botão Push Reset                     - IN
// v24 - LED Temperatura normal               - OUT
// v25 - controlo tempo Aguardar Ficheiro     - IN
// v26 - Ligar Flurescente                    - IN
// v27 - Ligar Led                            - IN                   
// v28 - Botão de emergencia                  - IN
// v29 - Tempo de actividade                  - IN
// v30 -
// v31 -
//



                                                       //declaracao das Bibliotecas



#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <DHT.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <OneWire.h>
#include <DallasTemperature.h>



                                                      //declaracao das variaveis para as contagens

                                                    
unsigned long over;
unsigned long over2;
unsigned long over3;
unsigned long over4; 
                                  

unsigned long tempodeactividade, inicio4, fim4; 
unsigned long inicio, fim, decorrido;
unsigned long inicio2, fim2, decorrido2;
unsigned long inicio3, fim3, decorrido3;

float h, m, s, ms;
float h2, m2, s2, ms2;
float h3, m3, s3, ms3;
float h4, m4, s4, ms4;

 
int travaodossegundos = 0;
int contagemtempofinal = 0;

int diasdecontagem = 0;
int offaguardarficheirotemperaturaalta = 0;
int offaguardarficheiro = 0;
int temperatura = 0;
int start=0;
int pinocontrolo=0;
int pinoficheiro=0;
int executarficheiro=0;
int introducao=0;
int stanby_1 = 0;
int standby_2 = 0;
int standby_3 = 0;
int chiller=0;
int controlotemperatura=0;
int controlotempo;
int controlotempo2;
int controloaguardarficheiro;
int Lampada;
int led;
int emergencia;
int refresh;




                                                        //declaracao de pinos
                                                        


#define resett 26
#define botaoemergencia 28                                         
#define leds 36
#define flurescente 22
#define desligar 24
#define controlo 7
#define ficheiro 12
#define pinochiller 9
#define W5100_CS  10 //pino placa de rede
#define SDCARD_CS 4 // pino sd card
#define DHTPIN 6  //pino do sensor  
#define DHTTYPE DHT22   //tipo de sensor
#define ONE_WIRE_BUS 8 // pino da sonda de agua







                                                    //declaracao da Comunicacao com local server


char auth[] = "xxxxxxx";
char server[] = "192.168.1.70";
int port = 8080;



                                         //declaracao da atualizaçao dos widgets da app com arduino, sempre que seja reconectado
                                         

BLYNK_CONNECTED(){
  Blynk.syncAll();
  inicio4 = millis();
}

    


                                            //declaracao da Escrita dos leds na app nas portas virtuais

WidgetLED leddesligado(V7);
WidgetLED ledaguardarficheiro(V8);
WidgetLED leddexecutarficheiro(V9);
WidgetLED ledstanby(V10);
WidgetLED ledtemperatura(V24);
WidgetLED ledtemperaturaexcesso(V15);
WidgetLED ledchiller(V11);
WidgetLED ledrefresh(V4);

                                          //declaracao da Escrita dos parametros da app para arduino


BLYNK_WRITE(V12){
  controlotemperatura = param.asInt();
}
BLYNK_WRITE(V13){
  controlotempo = param.asInt();
}
BLYNK_WRITE(V18){
  controlotempo2 = param.asInt();
}
BLYNK_WRITE(V23){
  refresh = param.asInt();
}
BLYNK_WRITE(V25){
  controloaguardarficheiro = param.asInt();
}
BLYNK_WRITE(V26){
  Lampada = param.asInt();
}
BLYNK_WRITE(V27){
  led = param.asInt();
}
BLYNK_WRITE(V28){
  emergencia = param.asInt();
}

                                          //setup sensores, relogio e timer

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
WidgetRTC rtc;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);


void setup(){
  
     Serial.begin(9600);

     pinMode(SDCARD_CS, OUTPUT);
     digitalWrite(SDCARD_CS, HIGH); // select the SD card
     pinMode(controlo, INPUT);
     pinMode(ficheiro, INPUT);
     pinMode(pinochiller, INPUT);
     pinMode(desligar, OUTPUT);
     pinMode(flurescente, OUTPUT);
     pinMode(leds, OUTPUT);
     pinMode(botaoemergencia, OUTPUT);
     pinMode(resett, OUTPUT);
     delay(10);

     Blynk.begin(auth, server, port);// entrada home server raspberry pi blynk
     
     sensors.begin();
     dht.begin();
     rtc.begin();
     
   
  
     timer.setInterval(250000L, sendSensor);
     timer.setInterval(60000L, clockDisplay);
     timer.setInterval(35000L, termometro);
     }



void loop(){

     if(!Blynk.connected()){//reconecta sempre que ficar offline
      Blynk.connect();
     }
  
     Blynk.run();
     timer.run();
     geral();
     }
                                                           
void geral(){

     fim4 = millis();
     
     contagemdeatividade();
 
     notificacao();
     
     
     executarficheiro = pinocontrolo == HIGH &&  pinoficheiro == HIGH; 
     introducao = pinocontrolo == HIGH && pinoficheiro == LOW && contagemtempofinal <= controlotempo;
     stanby_1 =  pinocontrolo == HIGH && pinoficheiro == LOW && contagemtempofinal > controlotempo;
     standby_2 = pinocontrolo == HIGH && pinoficheiro == LOW && contagemtempofinal >= controlotempo && m2 >= controlotempo2 && temperatura > controlotemperatura;
     standby_3 = pinocontrolo == HIGH && pinoficheiro == LOW && contagemtempofinal >= controlotempo && temperatura <= controlotemperatura && m2 >= controlotempo2;
     start = pinocontrolo == LOW && pinoficheiro == LOW;
     offaguardarficheiro = m3 >= controloaguardarficheiro && temperatura <= controlotemperatura;
     offaguardarficheirotemperaturaalta = m3 >= controloaguardarficheiro && temperatura > controlotemperatura;

     pinocontrolo = digitalRead(controlo);
     pinoficheiro = digitalRead(ficheiro);
     chiller = digitalRead(pinochiller);
     temperatura = sensors.getTempCByIndex(0);


                                                          //condicao para refresh


     if (refresh == 1){
      digitalWrite(resett,LOW);
     }else{
      digitalWrite(resett,HIGH);
     }



                                                        //condicao para ligar Emergencia 


      if (emergencia == 1){
         digitalWrite(botaoemergencia,HIGH);
      }else {
         digitalWrite(botaoemergencia,LOW);
      }


                                                       //condicao para ligar flurescente e LED 

 
     if (Lampada == 1){
         digitalWrite(flurescente,HIGH);
      }else {
         digitalWrite(flurescente,LOW);
      }
      
      if (led == 1){
         digitalWrite(leds,HIGH);
      }else {
         digitalWrite(leds,LOW);
      }

     
 
         

     
                                           //condicao para desligar automatico em aguardar ficheiro

                                           
                                                                                 
                                                                                               
      if (introducao == true && offaguardarficheiro == true){
      standby_3 = true;
      digitalWrite(desligar,HIGH);
      return;
      
      }

       if (introducao == true && offaguardarficheirotemperaturaalta == true){
         Blynk.virtualWrite(V0, "Ag.Temperatura");
      
      }
      
                                           //condicao de bloqueio do standBy 1 em 59 seg



   if ( s == 59){
      travaodossegundos = 59;
       }
    if (contagemtempofinal < 115){
        contagemtempofinal = s + travaodossegundos;
}

   
                                                // ligar led de refrigeracao na app


                                                
   if (chiller == HIGH){
       ledchiller.on();
      }
   if (chiller == LOW){
      ledchiller.off();
      }




                                                 // ligar led de temperatura na app
                                                 
                                                


   if ( temperatura <= controlotemperatura){
      ledtemperaturaexcesso.off();
      ledtemperatura.on();
      }
   
   if ( temperatura > controlotemperatura){
      ledtemperaturaexcesso.on();
      ledtemperatura.off();
      }


                                                                  // Inicio

       
                                                                  

   if ( start == true){
       m3 = 0;
       s=0;
       fim3 = millis();
       travaodossegundos = 0;
       contagemtempofinal = 0;
       Serial.println("Desligado");
       Blynk.virtualWrite(V0, "Desligado");
       leddesligado.on();
       ledaguardarficheiro.off();
       leddexecutarficheiro.off();
       ledstanby.off();
       ledrefresh.off();
       }


                                                               // Aguardar Ficheiro

    if (introducao == true){
   
        inicio3 = millis();
        fim = millis();
        contagem3();
        digitalWrite(desligar,LOW);
        Serial.println("Ag. Ficheiro");
        Blynk.virtualWrite(V0, "Ag.Ficheiro");
        ledrefresh.on();
        leddesligado.off();
        ledaguardarficheiro.on();
        leddexecutarficheiro.off();
        ledstanby.off();
        }


                                                              // Executar Ficheiro



                                                              
      if (executarficheiro == true){
          inicio = millis();
          fim2 = millis();
          fim3 = millis();
          contagem1();
          Serial.println("Executar Ficheiro");
          Blynk.virtualWrite(V0, "Executar Ficheiro");
          ledrefresh.on();
          leddexecutarficheiro.on();
          leddesligado.off();
          ledaguardarficheiro.off();
          ledstanby.off();
          }


                                                                  // Tempo de espera para desligar automatico                                                           


     if (stanby_1 == true){
         fim = millis();
         inicio2 = millis();
         contagem2();
         Serial.println("Stand by");
         Blynk.virtualWrite(V0, "Stand by");
         Serial.println(s2);
         ledrefresh.on();
         ledstanby.on();
         leddexecutarficheiro.off();
         leddesligado.off();
         ledaguardarficheiro.off(); 
         }  


                                                              // Tempo de espera e aguardar temperatura para desligar automatico



     //standby 2

     if (standby_2 == true){
         Serial.println("Aguardar Temperatura");
         Blynk.virtualWrite(V0, "Ag.Temperatura");
         ledtemperaturaexcesso.on();
         ledtemperatura.off();
         ledrefresh.on();
         ledstanby.on();
         leddexecutarficheiro.off();
         leddesligado.off();
         ledaguardarficheiro.off();
         }



                                                                         // Desligar automatico




//Por sua vez volta ao inicio.
                                                                         

     if (standby_3 == true){
     
        fim2 = millis();
        Serial.println("Desligado");
        Blynk.virtualWrite(V0, "Desligado");
        digitalWrite(desligar,HIGH);
         digitalWrite(flurescente,LOW);
        
              }
       }
        
                                                              //Sensor de Humidade e de temperatura do ar

                                                              

void sendSensor(){
     float h = dht.readHumidity();
     float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

     if (isnan(h) || isnan(t)) {
     Serial.println("Failed to read from DHT sensor!");
     return;
     }
    // You can send any value at any time.
    // Please don't send more that 10 values per second.
     Blynk.virtualWrite(V5, h);
     Blynk.virtualWrite(V6, t);
     }


                                                                //Contagem de ficheiro

                                                                

void contagem1(){ 
  
     decorrido = inicio - fim; 
     h = decorrido / 3600000; 
     over = decorrido % 3600000; 
     m = over / 60000; 
     over = over % 60000; 
     s = over / 1000; 
     ms = over % 1000;  
     Serial.print(h, 0); 
     Serial.print("h "); 
     Serial.print(m, 0); 
     Serial.print("m "); 
     Serial.print(s, 0); 
     Serial.print("s "); 
     Serial.println(); 
     Blynk.virtualWrite(V19,s);
     Blynk.virtualWrite(V3,m);
     Blynk.virtualWrite(V16,h);
     } 



                                                      //Contagem de tempo de stanby
                                                      

void contagem2(){
     
     decorrido2 = inicio2 - fim2; 
     h2 = decorrido2 / 3600000; 
     over2 = decorrido2 % 3600000; 
     m2 = over2 / 60000; 
     over2 = over2 % 60000; 
     s2 = over2 / 1000; 
     ms2 = over2 % 1000;  
     Serial.print(h2, 0); 
     Serial.print("h2 "); 
     Serial.print(m2, 0); 
     Serial.print("m2 "); 
     Serial.print(s2, 0); 
     Serial.print("s2 "); 
     Serial.println(); 
     Blynk.virtualWrite(V20,s2);
     Blynk.virtualWrite(V21,m2);
    }


     
                                                      //Contagem de tempo de aguardar ficheiro
                                                      

void contagem3(){
     
     decorrido3 = inicio3 - fim3; 
     h3 = decorrido3 / 3600000; 
     over3 = decorrido3 % 3600000; 
     m3 = over3 / 60000; 
     over3 = over3 % 60000; 
     s3 = over3 / 1000; 
     ms3 = over3 % 1000;  
     Serial.print(h3, 0); 
     Serial.print("h3 "); 
     Serial.print(m3, 0); 
     Serial.print("m3 "); 
     Serial.print(s3, 0); 
     Serial.print("s3 "); 
     Serial.println(); 
     Blynk.virtualWrite(V17,s3);
     Blynk.virtualWrite(V14,m3);
    }


                                                        //Contagem de tempo desde que esta online



    void contagemdeatividade(){
      
     tempodeactividade = fim4 - inicio4;
     h4 = tempodeactividade / 3600000; 
     over4 = tempodeactividade % 3600000; 
     m4 = over4 / 60000; 
     over4 = over4 % 60000; 
     s4 = over4 / 1000; 
     ms4 = over4 % 1000;
     if ( h4 == 23 && m4 == 59 && s4 == 59){
      diasdecontagem++;
     }
     Serial.print(h4, 0); 
     Serial.print("h4 "); 
     Serial.print(m4, 0); 
     Serial.print("m4 "); 
     Serial.print(s4, 0); 
     Serial.print("s4 "); 
     Serial.println(); 
     Blynk.virtualWrite(V29,s4);
     Blynk.virtualWrite(V30,m4);
     Blynk.virtualWrite(V31,h4);
      Blynk.virtualWrite(V32,diasdecontagem);
    }


                                                          //Sonda da Agua

void termometro(){
  
     sensors.requestTemperatures(); 
     Serial.print("Celsius temperature: ");
     Serial.print(sensors.getTempCByIndex(0)); 
      Blynk.virtualWrite(V22, sensors.getTempCByIndex(0));
    
     }

                                                                 //Relogio Blynk
                                                                 
void clockDisplay(){
  
     String currentTime = String(hour()) + ":" + minute();
     String currentDate = String(day()) + "/" + month() + "/" + year();
     Serial.print("Current time: ");
     Serial.print(currentTime);
     Serial.print(" ");
     Serial.print(currentDate);
     Serial.println();

                                                            // escrita da hora e data no blynk
     Blynk.virtualWrite(V1, currentTime);
     Blynk.virtualWrite(V2, currentDate);
     }



                                                    //Notificaçoes



void notificacao(){
  
     if (standby_3 == true){
         Blynk.notify("Desligado.");
         }
    
    //Notifica 1 vez quando entra em stanby
    
    
     if (stanby_1 == true && s2 < 3 && m2 < 1 && temperatura <= controlotemperatura){
         Blynk.notify("StandBy, Temperatura Normal.");
         }
    
     if (stanby_1 == true && s2 < 3 && m2 < 1 && temperatura > controlotemperatura){
        Blynk.notify("StandBy, Temperatura Excesso.");
        }
        
    
    //Notifica 1 vez quando aguarda temperatura
    
    
   if (standby_2 == true && m2 >= controlotempo2 && s2 < 3 && m2 == 1 && temperatura > controlotemperatura){
       Blynk.notify("StandBy Concluido, Aguardar temperatura.");
       }
       }

Using a Blynk.virtualWrite(vPin,value) command.

However, your void geral() function is being called from your void loop() and you’re taking temperature readings, and already doing Blynk.virtualWrites of these values to the server, which will cause server flooding issues if you’re not careful.

You should structure code better, making more use of timers.

Pete.

Thanks for the answer!

if (standby3 == true) {
if (Lampada == true) {
Lampada = false;
Blynk.virtualWrite(Lampada,false)
}
} …?
As for the void geral (), yes I know, but for the purpose that I use, being something autonomous, I don’t know how to make my program work, if not this way …
Sorry for my ignorance, if any! :slight_smile:

Because C++ treats false as zero and true as one then this will work, but personally I prefer a more explicit approach of writing 0 or 1 to the virtual pin, but that’s just my preference.

Without understanding more about the desired functionality of your project it’s difficult to advise, but you almost certainly don’t need to be trying to take temperature readings tens, if not hundreds, of times per second by calling this code segment from the void loop. Much better to reduce this to every 5 or 10 seconds using a timer to take the readings and push the results to Blynk.

Pete.

It is a project for monitoring a CNC Laser Co2. Will I have to make a timer.setInterval for all virtual pins?
Thank you.

Why would you have a timer for a virtual pin?

Pete.

So how can I do what you’re telling me? Sorry, I’m new to this, much of the code was made by attempted error. Thank you.

That tells me nothing about the functionality that you’re looking for from your project, so it’s impossible to say what is the best way forward for you.

Pete.

Ok, very well… This is a program in which the only power that the Arduino has over the machine, is to turn it on or off … So, the whole program is based on fulfilling certain conditions, so that the machine automatically turns off … These conditions will make a journey until you find the final path to automatically disconnect … Machine turns on, if it takes X time to enter a file, it turns off. Enter a file, if it is less than X time, go back, if it is bigger, it is standby. If it is longer than X time, turn off, but the temperature must be within the desired values, otherwise you will wait for the temperature to be within the requirements … All being true, the machine automatically shuts down … I have some virtual pins using led widget, 1 just for text …

Your topic title is about a Blynk button widget, yet your description of the Blynk aspect of the project is…

That doesn’t tell me anything about the functionality of the app or the ways in which the app will be used to interact with, control, and display data from the hardware. It tells me nothing about the update frequency of the app data, or the responsiveness that you’re looking for from the app.

At the moment, only you know this information, which means that only you can make the necessary decisions about the beat way to structure your code and how best to time your control, monitoring and reporting functions.

It seems from your

response that you find sharing this information tedious and somewhat unnecessary, so I’ll take a step back from the discussion and let other community members step in with their thoughts.

Pete.

Yes, it’s true, I asked about a widget, and yes it worked, thanks. As for the rest, you said that the code needed something more, and I asked how to do it, nothing more. As for my reaction, “OK very well”, I was being friendly, not arrogant! :slightly_smiling_face::wink: Good night.