Can't connect Arduino Uno to Blynk via Bluetooth (and issues with ultrasonic sensor)

I have the HC-06 bluetooth hardware device set up with my Arduino UNO.

The RX on the Arduino is connected to the TX on the HC-06, and vice versa. They share a common 5V and GND.

I’m trying to connect to Blynk. I’ve got the Blynk widget set up on my Android phone (Motorola Nexus 6), and it detects the HC-06 and connects to it (stable LED on HC-06).

Here’s the code I uploaded to the Arduino:

#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial


// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <BlynkSimpleSerialBLE.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MyAuthTokenThatIDoNotWantToShare";

void setup()
{
  // Blynk will work through Serial
  // 9600 is for HC-06. For HC-05 default speed is 38400
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Serial.println("Setting up");
  Blynk.begin(Serial, auth);

  // It never gets here
  Serial.println("Connected");
}

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

Most of it is taken directly from the Blynk github.

Here’s what I see on the Serial Monitor:

Setting up
[0] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.7 on Arduino Uno

[95] Connecting...
[6094] Connecting...
[12094] Connecting...
[18094] Connecting...
[24094] Connecting...
[30094] Connecting...

I’ve been at this for hours now, with no luck. What am I doing wrong?

Because you are using the same serial port for both BLYNK_PRINT and the Bluetooth, they will conflict. Use SoftwareSerial for the Bluetooth as shown here (from the Sketch Builder - Link at the top right of this page)

https://examples.blynk.cc/?board=Arduino%20Uno&shield=HC05%20or%20HC06&example=GettingStarted%2FBlynkBlink

2 Likes

I’m using ultrasonic sensors with my Arduino, which absolutely require delays in order to work properly. They need to wait for the sound to echo back.

// Get distance from the front of the car to the object in front
int getFrontDistance(){
  long duration, distance;

  Serial.println("Getting front distance");

  digitalWrite(TRIGGERFRONT, LOW);

  delayMicroseconds(2);

  digitalWrite(TRIGGERFRONT, HIGH);

  delayMicroseconds(10);

  digitalWrite(TRIGGERFRONT, LOW);
  duration = pulseIn(ECHOFRONT, HIGH);
    
  distance = (duration / 2) / 29.1;

  Blynk.virtualWrite(V4, distance);

  return distance;
}

With this code, I’m having a lot of problems with Blynk (disconnects, lots of lag etc). When I remove this code, everything works perfectly.

I know I shouldn’t use delays, but this code absolutely needs to have them. Is there a workaround for this? I tried having a while loop that kept checking if the required time had passed, but that pretty much resulted in the same thing because the loop is essentially a delay.

I have merged your last topic into your first as they are related.

It is not so much the delay (a few microseconds here or there shouldn’t matter), as the type of delay… I have found using delayMicroseconds() command combined with Blynk library causes strange issues that I haven’t been able to really track down. The only way I can get a ping sensor to work in a Blynk sketch is in short bursts of a few seconds at a time… and even then it seems to affect other timing processes.

Hmm, that sounds frustrating… I’ll try different workarounds and post here if I have any success.

Because timing is somewhat critical with Blynk, ESP and other IoT connected type devices, and delayMicroseconds() seems to really interfere with SimpleTimer functions and Blynk functions (in my testing), I would seriously consider any long term use of a device like an ultrasonic sensor, that relies on constant microsecond control, be done on a dedicated device that does not have Blynk installed, but that then sends the processed distance data, via I2C or something, to another Blynk controlled device.

1 Like

A post was merged into an existing topic: Interfacing a particle photon with Blynk via a HC 05 Bluetooth module