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

Constant disconnects from Blynk server with attached code (using UltraSonic Sensor)


#1

I keep getting disconnected from Blynk server every 5-10 seconds with the following code for Ultrasonic sensor (HC-SR04). Connection works fine with other code i.e. servo code. Please someone tell me if you see anything that might be causing this issue. I’m using a NodeMCU ESP8266 from Geekcreit (ESP-12E module)
Thank you in advance.

/*
********************************************
14CORE ULTRASONIC DISTANCE SENSOR CODE TEST
********************************************
*/
#define TRIGGER 4
#define ECHO    5

// NodeMCU Pin D1 > TRIGGER | Pin D2 > ECHO

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxxx";
char pass[] = "xxxxxxxxxxx";
void setup() 
{
  Serial.begin (9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUILTIN_LED, OUTPUT);
}

void loop() {
  
  long duration, distance;
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(2); 
  
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10); 
  
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;

   if (distance <= 150) {
    Blynk.virtualWrite(V0, 255);
}
  else {
    Blynk.virtualWrite(V0, 0);
  }

 if (distance <= 100) {
    Blynk.virtualWrite(V1, 255);
}
  else {
    Blynk.virtualWrite(V1, 0);
  }

   if (distance <= 80) {
    Blynk.virtualWrite(V2, 255);
}
  else {
    Blynk.virtualWrite(V2, 0);
  }

   if (distance <= 40) {
    Blynk.virtualWrite(V3, 255);
}
  else {
    Blynk.virtualWrite(V3, 0);
  }

   if (distance <= 20) {
    Blynk.virtualWrite(V4, 255);
}
  else {
    Blynk.virtualWrite(V4, 0);
  }

  
  
  Serial.print(distance);
  Serial.println("Centimeter:");
  Blynk.virtualWrite(V5, distance);
  delay(200);
  Blynk.run();
}

#2

Hello and Welcome to the Blynk Forum.

I edited your post to format the code for viewability, as per Welcome Topic.

As for your disconnections, you have ‘Too Much / Too Fast’ happening in your void loop(). You need to eliminate blocking delays and segregate functions into timed loops with BlynkTimer.
http://docs.blynk.cc/#blynk-firmware-blynktimer

Don’t forget to check out the Documents, Help Center and Sketch Builder… their links are at the top right of this page. All the info you need to get started with Blynk is in there.

Also make use of this forums Search function… try searching for “Ultrasonic” as there have been a few topics about them that you can get ideas from. And you will see some issues getting them to work properly as the delayMicroseconds() tends to run into conflicts with Blynk’s internal timing… at least it seems that way to me. I have found it better to scan in short bursts.

Try adding in a timer that runs most of what you have in the void loop() but in a separate timed loop, say every 250ms - 500ms or so, instead of the current hundreds of times a second that it is currently trying.


#3

in short, remove all delays from loop and use SimpleTimer


#4

RTFM :wink:


#5

:joy:


#6

:grin: