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();
}
it connects and then disconnects and does not connect anymore, I can not activate the relay by the virtual button nor measure the current, being that as I said before. if I test the separate code (just read the virtual button and just le the current sensor, run)
Are you powering Relay, ACS712 and the ESP8266 from the same power supply?
Edit:
Right, your are sampling 500 samples on a for loop, my bet is that this is holding your communication with Blynk. Change it to 1 or something and see if it works.
If you connect it to your PC while using that external power supply you’ll damage something.
Try powering ACS712 and Relay with an external power supply and only the NodeMCU with the USB. Also change your Baud rate to 115200 and the Serial Monitor needs to show something.
Edit:
try changing all this variables to float:
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
Also I’m not familiar with NodeMCU but I believe int rele = 7; //pin rele is wrong.
it’s true I’m sorry for this silly mistake I had forgotten, but making the change to pin 13 (esp which is the D7) the button in the app keeps turning on and off, I can not record because I have only one cell phone … but it does not stay in something defined … as the sensor is dialing 0 mah, it keeps switching on and off
great posts will be very useful for future projects … but I did not find something similar to what I think may be causing this conflict in the buttons … I believe that it is at the moment that it does the reading that identifies for example that the current reading is zero, or the light is off, it keeps trying to update and conflicts with the reading of the virtual pin