ESP8266 + HR SC04 Issue

Hi.

I have successfully run ESP8266 Feather Huzzah + HR SC04 by pining it in the loop functin. Getting correct vales for the ultra sonic distance

However when i move the code to blynk sketch. Occationaly the sketch will not even connect to Blynk or it connects and returns just zero. Am i missing something?

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

#include <NewPing.h>
#define TRIGGER_PIN  2
#define ECHO_PIN     0
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

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

WidgetLCD lcd(V0);
SimpleTimer timer;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "my wifi name", "my wifi pass");
  delay(100);
  
  timer.setInterval(1000, ultra);
}

void ultra()
{
  unsigned int uS = sonar.ping_cm();
  Serial.println(uS);
  lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(0,0,"Distance (cm):");
  lcd.print(0,1,(uS));
}

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

Seems fine. Could you please post the code that works without Blynk?

@Blynk182 notice you are using pin 0 and 2. Not the best pins to use, try others.

1 Like
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping every second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  2  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     0  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(1000);                     // pings every second
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

I was able to fix the intermitten connection issue by always hard reseting the device by disconnecting the power and then connecting it.

But there is def some issue getting sonar.ping_cm() to work on any part of the sketch.

I was able to run a simple counter variable and get that to display on the lcd blynk module file.

Thanks for your help Dimitriy

Hey That did the trick.

Thanks for the tip.

Solved :slight_smile:

2 Likes