HC-05 and Blynk don't work

I have read all the discussions on the HC-05.
but as i can undestand the HC-05 with blynk doesn’t work.
i mave made this sketch i and i try to config my system with the app blynk, but the bluetooth spend some time in “connecting” and next give me an error.
I have the last version 1.0.1 for library
the last versiono of Blynk (Legacy)
and one of the last samsung smartphone,
Can you help me?
If there is an other method an order to connecr my sistem “Arduino Nano” with App Blynk without the use internet, can you explain its to me? Thk a lot

#define BLYNK_USE_DIRECT_CONNECT

#include <avr/sleep.h>

#include <avr/power.h>

#include <SoftwareSerial.h> // Questo permette di leggere sul seriale

SoftwareSerial DebugSerial(2, 3); // RX, TX  // Pin per trasmissione dati

#define BLYNK_PRINT DebugSerial

#include <BlynkSimpleSerialBLE.h>

char auth[] = "92VFpWptDnjibDuxg1Vd9HGetmWkAhD_"; //codice applicazione



void setup()
{

  DebugSerial.begin(9600); //Inizializzo la comunicazione modulo Bluetooth

  DebugSerial.println("Waiting for connections...");

  Serial.begin(9600); // attivazione del seriale per risultati di arduino

  Blynk.begin(Serial, auth); //Attivazione della trasmissione al server Blink
  
  pinMode (A0, INPUT); //Impostazione del pin in Input Analogico per lettura valori

  pinMode (A1, INPUT); //Impostazione del pin in Input Analogico per lettura valori

  pinMode(2,INPUT_PULLUP);    //Pin per fare interrupt   

}


void loop()
{

  Blynk.run();

  int Conta;

  float R1 = 52000; //Restistenza a monte.

  float R2 = 8000;  //resistenza a valle.

  float calc_res = (R1 + R2) / R2;  // Partitore di tensione
  

//Segnali in ingresso

  int ValPinBatt = analogRead(A0);  // Leggo valore in ingresso im porta analogica

  int ValPinCorr = analogRead(A1);  // Leggo valore in ingresso im porta analogica

// Trasformazione in Volt dei segnali digitali -- Per la Batteria

  float Vpin = ValPinBatt*4.8/982;  // Per settare il valore "982" consiglio di farlo dal seriale vedendo cosa leggere quando la batteria è al max 
  
  //4.8V = 4,2V max carica che scende subito dopo la carica
  //ma io max carica fino a 4,34V = 3,8V per batteria --> 982 Analogici
  //per batteria scarica 4.11V = 36V
  
  float Vbatt = Vpin * calc_res;  //VALORE PER APPLICAZINE

  int ValBattPerc = Vbatt*100/38; // Vbatt:36=ValBattPerc:100 VALORE PER APPLICAZIONE

  if (ValBattPerc > 100){     // Evitare valori superiori a 100 per le batterie

    ValBattPerc = 100;
    
    }

// Trasformazione in Volt dei segnali digitali -- Per la Corrente

  float CorrBatt = ValPinCorr*2.36/520-2.36;

// Codice passaggio in modalità Sleep

//  while (CorrBatt > 0.5){     // Evitare valori superiori a 100 per le batterie

//    delay(1000);

//    Conta = Conta + 1;
//      if (Conta > 10){     // Dopo 10 secondi della condizione precedente va in Sleep 
         
//        Serial.println("Vado a dormire");

//        Conta = 0;
         
//        StartSleep();  //Funzione da richiamare per mandarlo i Sleep
    
//      }

//  }



  Blynk.virtualWrite(V0, Vbatt); //Byte di scrittura applicativo VOLT

  Blynk.virtualWrite(V1, ValBattPerc); //Byte di scrittura applicativo % BATTERIA

  Blynk.virtualWrite(V2, CorrBatt); //Byte di scrittura applicativo CORRENTE (A)

  delay(1000);

  Serial.println(ValPinBatt);
  
  Serial.print("Tensione batteria:");
  
  Serial.println(Vbatt);                

  Serial.print("Percentuale batteria:");
  
  Serial.println(ValBattPerc); 

  Serial.println(ValPinCorr);

  Serial.print("Corrente erogata:");
  
  Serial.println(CorrBatt); 

}

// Funzione per dormire

void StartSleep(){ 

  attachInterrupt(digitalPinToInterrupt(2), pin2Interrupt, RISING); //Dico ad arduino come risvegliarsi
  delay(100);

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);  //Modalità più profonda possibile di riposo

  Serial.println("Sto dormendo");

  sleep_enable();
  sleep_mode();

  //La partenza inizia da qui

  sleep_disable();

  Serial.println("Sono sveglio");
}

//  Funzione per risveglio

void pin2Interrupt(){

  detachInterrupt(digitalPinToInterrupt(2));
  
}

I’d use version 0.6.1 if I were you.

Do you have an FTDI adapter attached to pins 2 and 3 so that you can view your debug serial messages? If so, what are you getting?

Why are you using GPO2 for your software serial port and also as an interrupt pin?

Your void loop is a total mess, and won’t work with Blynk.
Your should read this…

Do you know for sure that your HC-05 is set to work at 9600 baud?

Have you tried the simple example sketch in the sketch builder…

Can you be more specific than this?..

Pete.

First overall thk for your answer.
Ok i can try with 0.6.1. now i give you the Sketch without error sorry.
I know that my HC-05 use 38400 Baud.
the error form app is:
“Can’t connect. Something went wrong when connetting to your BT device. Please try again later.”

[Unformatted code removed by moderator]

Please format your code correctly, as you did in your first post, with triple backticks at the beginning and end.
triple backticks look like this:
```

I asked you quite a lot of questions, but you haven’t answered most of them.
You should also clarify why you’ve posted this second sketch. Did you post the wrong one initially?

Pete.

Yes i post the wrong one initially.
Now i post my sketch.

  1. In the serial i see only “Waiting for connections…”.
  2. Ok i can try with 0.6.1. now i give you the Sketch without error sorry.
  3. I use the interrupt in pin 2 in order to wake up my arduino nano from sleep mode.
  4. I asked AT+UART? to HC-05 and he respond 9600,0,0.
  5. the error form app Blynk is:
    “Can’t connect. Something went wrong when connetting to your BT device. Please try again later.”

And i give you my last sketch that i want use:

#define BLYNK_USE_DIRECT_CONNECT

#include <avr/sleep.h>

#include <avr/power.h>

#include <SoftwareSerial.h>

SoftwareSerial DebugSerial(0, 1);

#define BLYNK_PRINT DebugSerial

#include <BlynkSimpleSerialBLE.h>

char auth[] = "92VFpWptDnjibDuxg1Vd9HGetmWkAhD_";



void setup()
{

  DebugSerial.begin(9600);

  DebugSerial.println("Waiting for connections...");

  Serial.begin(9600);

  Blynk.begin(Serial, auth); 
  
  pinMode (A0, INPUT); 

  pinMode (A1, INPUT); 

  pinMode(2,INPUT_PULLUP);  

}


void loop()
{

  Blynk.run();

  int Conta;

  float R1 = 52000; //Restistenza a monte.

  float R2 = 8000;  //resistenza a valle.

  float calc_res = (R1 + R2) / R2;  
  

//Segnali in ingresso

  int ValPinBatt = analogRead(A0);  // Leggo valore in ingresso im porta analogica

  int ValPinCorr = analogRead(A1);  // Leggo valore in ingresso im porta analogica

// Trasformazione in Volt dei segnali digitali -- Per la Batteria

  float Vpin = ValPinBatt*4.8/982;  // Per settare il valore "982" consiglio di farlo dal seriale vedendo cosa leggere quando la batteria è al max 
  
  //4.8V = 4,2V max carica che scende subito dopo la carica
  //ma io max carica fino a 4,34V = 3,8V per batteria --> 982 Analogici
  //per batteria scarica 4.11V = 36V
  
  float Vbatt = Vpin * calc_res;  //VALORE PER APPLICAZINE

  int ValBattPerc = Vbatt*100/38; // Vbatt:36=ValBattPerc:100 VALORE PER APPLICAZIONE

  if (ValBattPerc > 100){     // Evitare valori superiori a 100 per le batterie

    ValBattPerc = 100;
    
    }

// Trasformazione in Volt dei segnali digitali -- Per la Corrente

  float CorrBatt = ValPinCorr*2.36/520-2.36;

//Codice passaggio in modalità Sleep

  while (CorrBatt > 0.5){     // Evitare valori superiori a 100 per le batterie

   delay(1000);

      Conta = Conta + 1;
      if (Conta > 10){     // Dopo 10 secondi della condizione precedente va in Sleep 
         
       Serial.println("Vado a dormire");

        Conta = 0;
         
        StartSleep();  //Funzione da richiamare per mandarlo i Sleep
    
     }

  }



  Blynk.virtualWrite(V0, Vbatt); //Byte di scrittura applicativo VOLT

  Blynk.virtualWrite(V1, ValBattPerc); //Byte di scrittura applicativo % BATTERIA

  Blynk.virtualWrite(V2, CorrBatt); //Byte di scrittura applicativo CORRENTE (A)

  delay(1000);

  Serial.println(ValPinBatt);
  
  Serial.print("Tensione batteria:");
  
  Serial.println(Vbatt);                

  Serial.print("Percentuale batteria:");
  
  Serial.println(ValBattPerc); 

  Serial.println(ValPinCorr);

  Serial.print("Corrente erogata:");
  
  Serial.println(CorrBatt); 

}

// Funzione per dormire

void StartSleep(){ 

  attachInterrupt(digitalPinToInterrupt(2), pin2Interrupt, RISING); //Dico ad arduino come risvegliarsi
  delay(100);

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);  //Modalità più profonda possibile di riposo

  Serial.println("Sto dormendo");

  sleep_enable();
  sleep_mode();

  //La partenza inizia da qui

  sleep_disable();

  Serial.println("Sono sveglio");
}

//  Funzione per risveglio

void pin2Interrupt(){

  detachInterrupt(digitalPinToInterrupt(2));
  
}

Okay, the important questions, which you haven’t answered, are…

and…

I guess that the answer to the FTDI question is “No” because in this version of the sketch you are trying to connect the HC-05 and the FTDI to the same pins…

Pins 0 & 1 are the single hardware UART for your board, and you are trying to use the same pins for your serial debug messages. You can’t do this, and if you you can’t send debug messages to your serial hardware UART like this either…

The HC-05 needs exclusive access to a serial port, whether that is a physical one or one emulated using SoftwareSerial.

If you don’t have an FTDI adapter and your HC-05 uses 9600 baud then I’d attach the HC-05 to the SoftwareSerial port and use the onboard FTDI to enable you to see debug messages via the hardware UART.

I’d strongly recommend that you use the example sketch, which takes this approach and doesn’t have the cluttered void loop and deep sleep code to get in the way of your testing.

Pete.

No i haven’t an FTDI (i must buy it?).

in order to connect my HC-05 i must remove this part of sketch? “SoftwareSerial DebugSerial(0, 1);”

as can i do this? Can you correct the wrong part of sketcj, in order i can understand.

Thk of your diponibility

No, I’m not going to edit your sketch for you. I’m going to repeat my advice to use the Blynk sketch builder example instead.

If you want to do any serious IoT work with your Nano then you should buy an FTDI adapter.

Pete.

Sorry but i don’t know what is is FTDI.
Can you explain me role of this devide?

Goggle is your friend!

Pete.

hi Pete,
with your help my system work.
By your opinion, what baud rate is more suitable in order to send data to Blynk?
Now i have 9600 Baud un HC-05.

If you’re using a SoftwareSerial port to connect the HC-05 to your Nano then you’re limited to 9600 baud because the Nano doesn’t have the processing power to emulate a serial port at a higher baud rate.

I doubt if you’d notice much difference if you used a hardware serial port and a higher baud rate, but I’ve never used Bluetooth with Blynk so I can’t really say.

Pete.

Ok many many thk,
an other question.
if i leave go my Blynk app it works very well for some time but next it stop to refresh the data read from arduino and i must close and open an other time the blynk app and then it restart to work.
Do you know a method avoid this?

I think that’s how the BT connection works, one of the many reasons why I never use BT.

Pete.

I thought.
I wanted put my arduino il Sleep mode after some time, as i can do with blynk if i can’t use delay()?
Do you have never use sleep mode and Blynk app?

I’ve ever used sleep mode with an Arduino, mostly because I don’t use Arduinos.
Sleep mode with ESP8266’s or ESP32’s, using WiFi connectivity, requires a very different approach to the program structure.
I can’t see this working well with an Arduino and BT connection.

Pete.

Tomorrow i will post the sketch works.
If can i ask, there is a method in order to have a value cumulative on blynk.
Because i wanted monitor the kWh of a battery.

It’s not something that is built in to Blynk, but straightforward in C++. How you do that will depend on whether you wnat the values to be persistent when the device reboots.
If that’s something you want then you’ll probably be better using a different board type so that you have NVR that can be used more effectively than the EEPROM in an Arduino.

Pete.

Hi,
I what save data in a micro sd. I want create a file with some column as “Voltage” “Current” “Power” and “Energy”. But the Energy must be’ cumulative data. do you have a project I can take inspiration from? Thk

This isn’t really a Blynk related question.
You’ll need to read the last value from the card, add the new reading to that, and write the result back to the card.
Depending on how frequently you do this, you may cause premature wear to the card.
Google SD card read/write functions for whatever hardware you are using and you should find some examples.

Pete.