Hello.
I’m using an Arduino Uno R3 board with a ESP8266 shield. My Smartphone is a iPhone 7 with Blynk 2.26.1. I’m using Blynk Cloud Server.
//CONSTANTES E VARIÁVEIS
#define BLYNK_PRINT Serial
#define CURRENT_CAL 2 //REVER VALOR DE CALIBRAÇÃO COM MULTIMETRO - TENHO QUE CHEGAR EM 0.0716 COM LAMPADA LIGADA, 0 COM LAMPADA DESLIGADA
#define EspSerial Serial
#define ESP8266_BAUD 9600
const int pinoSensor = A2;
const int sensorPIR = 4;
int horaInicio;
int horaFinal;
int sensorLigado;
int botao;
const int rele = 8;
int contador; //variável de auxílio para o desligamento automatico da lampada
//BIBLIOTECAS
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <EmonLib.h>
#include <WidgetRTC.h>
//Settando configurações do Blynk para permitir a conexão do shield à rede WiFi abaixo
char auth[] = "xxxE";
char ssid[] = "Jucilaine";
char pass[] = "88218378";
ESP8266 wifi(&EspSerial); //Funcionamento do shield ESP8266
EnergyMonitor emon1; //Criação de instância para acompanhar o consumo de energia
WidgetRTC rtc; //Criação de instância para acompanhar o horário local
BLYNK_CONNECTED(){ //Função para atualizar os valores do servidor com os mostrados no app.
Blynk.syncAll();
}
BLYNK_WRITE(V3) {
sensorLigado = param.asInt();
}
BLYNK_WRITE(V4) {
botao = param.asInt();
Serial.println("Blynk_Write(V4) correu");
Serial.println("Botão de ligar e desligar settado para: " + String(botao));
if (botao == 0){
digitalWrite(rele, HIGH);
} else {
digitalWrite(rele, LOW);
}
}
BLYNK_WRITE(V5) {
horaInicio = param.asInt();
}
BLYNK_WRITE(V6){
horaFinal = param.asInt();
}
void sensorPresenca() {
Serial.println();
Serial.print("Sensor de presenca: ");
Serial.println(digitalRead(sensorPIR));
if (digitalRead(sensorPIR) == 0){ //5 leituras por segundo
contador++;
} else {
contador = 0;
}
Serial.println();
Serial.print("SensorLigado: ");
Serial.println(sensorLigado);
Serial.print("Contador: ");
Serial.println(contador);
int horaAtual = hour()*3600 + minute()*60;
if ((horaAtual > horaInicio) && (horaAtual < horaFinal) && (sensorLigado == 1)) {
if(contador > 10) {
}
}
}
void enviarCorrente(){
emon1.calcVI(20,100); //peguei (20,100) de um blog masterwalker
double corrente = emon1.Irms;
Serial.println();
Serial.print("Corrente: ");
Serial.print(corrente);
Serial.println("A");
Blynk.virtualWrite(V1, corrente);
}
void setup() {
rtc.begin(); //sincronizando o horário
pinMode(sensorPIR, INPUT);
pinMode(rele, OUTPUT);
Serial.begin(9600);
EspSerial.begin(ESP8266_BAUD);
delay(10);
emon1.current(pinoSensor, CURRENT_CAL);
Blynk.begin(auth, wifi, ssid, pass);
}
void loop() {
enviarCorrente();
sensorPresenca();
Blynk.run();
}
So I’ve taken off some pieces of code that doesn’t relate to the V4 virtual pin, but my problem is: Sometimes the blynk_write(v4) function works properly, but sometimes I press the button on my mobile app and it doesn’t change the variable state. It’s connected to a relay module to control a lamp.