Pliz help, nodeMcu+ ACS 712 don't run

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();
}


If your serial monitor is showing funny symbols is probably due to the wrong Baud rate selected.

Sorry wasn’t clear “what doesn’t work”

it is not the monitor I checked it, it does not work in the sense of nor the node if it connects with the app nor the reading of the current…

I said that based on what you said, the serial monitor would show some hint of what can be wrong.

I have to agree with @ldb

…is not anywhere clear enough…

Is you device connecting to the Server? If so, are you getting any info in the App or just wrong values?

So… what does that mean? You are or are not getting actual text on the serial monitor, and under what conditions? Show us what you do see.

Clarity helps :wink:

yes, I understood … but I think the can be in the code, the part that I check if the current is greater than 30mah does such a action … :frowning:


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)

What board are you using?

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.

for(int a=0; a<nMuestras; a++){           

    voltajeInst=5.00/1023*analogRead(A0);
    
  if(voltajeInst>=voltajeMax){        
    voltajeMax=voltajeInst;          
  }
  if(voltajeInst<=voltajeMin){       
    voltajeMin=voltajeInst;           
  }
}


powering the ACS and relay with the source and usb interface

alterei para 1, sem sucesso,had separate tests with the same 500 and there were no problems

Edit: I changed tabem bound rate to 115200 … and nothing

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.

but this is how it is connected, (ACS712 and relay in the external source and NodeMCU in the usb) only the gnd that is in common,

edit :I changed the baud rate to 115200 before posting … I already tested it like this and nothing

I loaded your code into my board and it crashes, you will see below the Serial Monitor printout:

[3088] Connecting to blynk-cloud.com:80
[3567] Ready (ping: 234ms).

 ets Jan  8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v614f7c32
~ld

I changed int rele = 15; //pin rele and it works.

@Guilherme_Ferreira you need to use GPIO numbering in your sketch… NOT the silk screened numbers on the board.

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

I am using an UNO and USB link for my ASC712… but perhaps this code might have something worth looking at??

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

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();
     } 
  }

between these two parts of the code

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 ");
                     
  } 
 
}

Would you help me ?

I’m 100% sure you will find something similar on @Gunner’s comprehensive examples.

Is your button configured as a switch or push?

both push and switch stay the same
bloggif_5b6e4ac1d6184

Well… this has nothing to do with Blynk anymore.

The following line is responsible for the blinking button.

Edit:
Change it to

                Blynk.virtualWrite(V2, 0);