sorry my bad English is not my native language, and the code is long. I’m using a node CMU and an ACS 712 20 A sensor, in order to be able to trigger a lamp either by the Blynk application (<3) or by the physical button , and call the app and shut down the fisico.porem I am not able to make it work … “both” the codes were tested separately and worked. but when “together” does not work, trying to update the status of the virtual button and led in the app. if the serial monitor opens, strange letters appear or nothing appears.
#include <SoftwareSerial.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "***";
char ssid[] = "***";
char pass[] = "***";
BlynkTimer timer;
void checkcurrent();
int ledState = LOW;
int btnState = HIGH;
int rele = 7; //pin rele
// ============================ Check Current ===================================================
unsigned long previousMillis = 0; //Variable que indica el tiempo que ha pasado en ms
const long interval = 1000; //Variable asignado para el intervalo de tiempo, es 1s
char comando; //Variable que guarda el valor recibido por el puerto serie
double voltajeInst; //Variable para la corriente instantanea
double voltajeMax; //Variable para la corriente máxima
double voltajeMin; //Variable para la corriente mínima
int potencia; //Variable para guardar la potencia
float Irms; //Variable para la corriente rms
int nMuestras=500;
// =============================================================================================
WidgetLED led1(V0);
BLYNK_CONNECTED() {
Blynk.syncVirtual(V2);
}
BLYNK_WRITE(V2) {
ledState = param.asInt();
digitalWrite(rele, ledState);
if (param.asInt()){
Blynk.virtualWrite(V3,"ligado");
led1.on();
} else {
Blynk.virtualWrite(V3,"desligado");
led1.off();
}
}
// ==================================== Check current ===========================================
void checkcurrent()
{
unsigned long currentMillis = millis();
voltajeMax=0.00;
voltajeMin=5.00;
for(int a=0; a<nMuestras; a++){
voltajeInst=5.00/1023*analogRead(A0);
if(voltajeInst>=voltajeMax){
voltajeMax=voltajeInst;
}
if(voltajeInst<=voltajeMin){
voltajeMin=voltajeInst;
}
}
float Vpp=voltajeMax-voltajeMin;
Irms=(5.3568 *Vpp)-0.15;
if(Irms<=0){
Irms=0;
}
potencia=Irms*220;
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial.print("Corrente: ");
Serial.println(Irms*1000); // currente in mAh
}
// ====================== Action after check=========================================================
if (Irms*1000 > 30) { // if the current ig higher a 30mah...
ledState = ledState;
digitalWrite(rele, ledState);
// Update Button Widget
Blynk.virtualWrite(V2, ledState);
led1.on();Blynk.virtualWrite(V3,"ligado");//Blynk.notify("ligado ");
}else{
ledState = !ledState;
digitalWrite(rele, ledState);
// Update Button Widget
Blynk.virtualWrite(V2, ledState);
led1.off();Blynk.virtualWrite(V3,"desligado"); //Blynk.notify("Desligado ");
}
}
// ====================== SETUP =========================================================
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(rele, OUTPUT);
//pinMode(push, INPUT_PULLUP);
digitalWrite(rele, ledState);
timer.setInterval(1000L, checkcurrent);
}
// ====================== LOOP =========================================================
void loop()
{
Blynk.run();
timer.run();
}