BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

I need help in a college job

Hello, i’m doing a college job and I need show a value of current sensor in my smart phone. For this i’m using Blynk to make app. My problem is that I can not show the data read by the sensor in Blynk, can anyone help me?

I’m using Arduino MEGA 2560 - Bluetooth Communication.

My code:

#define BLYNK_PRINT Serial

#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>
#include "EmonLib.h" //sensor de corrente
#include <Ultrasonic.h> // sensor ultrassonico
#include <EEPROM.h>


char buf; // ultrassonico
unsigned short flag = 0; //flag para armazenar valor anterior do sensor ultrasonico


#define pino_trigger 4 // ultrassonico
#define pino_echo 5 // ultrassonico

Ultrasonic ultrasonic(pino_trigger, pino_echo);
EnergyMonitor emon1; // corrente

//Tensao da rede eletrica
int rede = 127.0;
 
//Pino do sensor SCT
int pino_sct = A0;

//variaveis para contar tempo
double timems=0, time_ant=0, time_atual=0, tempo = 0;

//variaveis para calculo de kwh
double kwtotal=0;
double Irms = 0;

char auth[] = "cbd0eb8743e74c6c993552b0b9df1ef3";

SoftwareSerial SerialBLE(10, 11); // RX, TX

BlynkTimer timer;

void corrente()
{
  Blynk.virtualWrite(V0, Irms);
}
void potenciaInsta()
{
  Blynk.virtualWrite(V1, Irms*rede/1000);
}

void potenciaTotal()
{
  Blynk.virtualWrite(V2, kwtotal);
}

void gastoRede()
{
  Blynk.virtualWrite(V3,kwtotal*0.78063);
}
void setup() {
 //Define o pino 7 como saida
  pinMode(7, OUTPUT);//saida do relé
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  SerialBLE.begin(9600);
  Blynk.begin(SerialBLE,auth);
  Serial.println("Lendo dados do sensor");  
    
  //Pino, calibracao - Cur Const= Ratio/BurdenR. 1800/62 = 29. 
  emon1.current(pino_sct, 16); // calibração corrente com o multimetro
  
  timer.setInterval(1000L, corrente);
  timer.setInterval(1000L, potenciaInsta);
  timer.setInterval(1000L, potenciaTotal);
  timer.setInterval(1000L, gastoRede);
}

void loop() {

  Blynk.run();
  timer.run();
  
  long microsec = ultrasonic.timing();
  float cmMsec, inMsec;
 
  time_atual = millis() - time_ant;
  time_ant = time_ant + time_atual;
  
  Irms = emon1.calcIrms(1480); //calcula Irms

  kwtotal = kwtotal + (double)Irms*rede*(time_atual/3600E6); //total de kw consumido
  
  //sensor ultrasonico
  cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  //inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
  //Serial.print("Distancia em cm: ");
  //Serial.print(cmMsec);
  //Serial.print(" - Distancia em polegadas: ");
  //Serial.println(inMsec);
  
  //Mostra o valor da corrente
  Serial.print("Corrente: ");
  Serial.print(Irms);
  
  //Calcula e mostra o valor da potencia
  Serial.print(" Potencia: ");
  Serial.println(Irms*rede);

  //mostra o valor de kw instantaneo
  Serial.print(" Kw instantaneo: ");
  Serial.println(Irms*rede/1000,10); // 3600E3

  //mostra o total de kwh consumido
  Serial.print(" Kwh total consumido: ");
  Serial.println(kwtotal,10);

  //mostra o valor em reais
  Serial.print("Valor da conta atual: R$");
  Serial.println(kwtotal*0.78063191,10); // valor do Kwh da conta de energia

  //mostra o valor de kwh total estipulado para um mês
  Serial.print(" Kwh total estipulado para 30 dias: ");
  Serial.println((Irms*rede*720)/1000);     //valor de kwh atual vezes 720horas de 30dias
  Serial.println(time_atual);
  
  if(cmMsec <20) //seta flag dizendo que foi menor que 20cm
  {
       flag = 1;   
  }

  if(cmMsec >30 && flag==1) //se for maior que 30 e sensor foi acionado anteriormente
  {
    digitalWrite(13, !digitalRead(13)); //inverte estado do led
    flag = 0;  //limpa flag  
  }

  delay(10000);
}

Do colleges stop teaching how to read directions?

I formatted your code as required and stated in both the Welcome Topic and in the templaye test you needed to erase when posting your topic.

Blynk - FTFC

And speaking of directions… your void loop() is not Blynk friendly…

Look at the Avoiding the Void part in particular…

And this is BAD! with Blynk, particularly in the loop.

1 Like

Ok, i remove the delay and i’m read the Avoid Void, but i’m not print in void loop(). You say my void loop() is no Blynk friendly, how I do this?

By reading the suggested document, and the many others in the Help Center :wink:

Use timed functions and keep the loop clear of everything but the basics… as stated in the document link above…

void loop() {
  Blynk.run();
  timer.run();
}

Thank you man, this link help me so much xD.

I finish my project thanks.

1 Like