BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

Problem with ultrasonic+esp8266 when use blynk

When I using normal code(offline on monitor) it work fine.

But when I combine with BlynkEdgent it give me weird value 1 upto 15 and down to 1 again loop like this.

Ps.sorry for my eng skill
Here the code

#define BLYNK_TEMPLATE_ID "*****PLW0*****"
#define BLYNK_DEVICE_NAME "Test Counter"
//#define BLYNK_AUTH_TOKEN "_____"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
#define APP_DEBUG
#include "BlynkEdgent.h"
BlynkTimer timer;

//=================================================
const int TrigPin = D1;      
int EchoPin = D2;             

long duration;                
long cm ;                     

//======================================================
void setup()
{
  Serial.begin(9600);
        
  pinMode(TrigPin, OUTPUT);  
  pinMode(EchoPin, INPUT);    
 
  BlynkEdgent.begin();
  timer.setInterval(300L, sendUltrasonic);
  delay(2000);
}

//====================================================
void loop() {
  BlynkEdgent.run();
  timer.run();
}

//====================================================
void sendUltrasonic() {
  digitalWrite(TrigPin, LOW);        
  delayMicroseconds(2);                 
  digitalWrite(TrigPin, HIGH);          
  delayMicroseconds(10);                 
  digitalWrite(TrigPin, LOW);      
  
  duration = pulseIn(EchoPin, HIGH);   
  cm = duration*0.034/2; 

  Blynk.virtualWrite(V7,cm);
  Serial.print(cm);                    
  Serial.print("cm");                  
  Serial.println();                    
}

And this the offline code that work fine

const int TrigPin = D1;      
int EchoPin = D2;           

long duration;                
long cm ;                   
void setup()
{
  Serial.begin(9600);
  
  pinMode(TrigPin, OUTPUT);   
  pinMode(EchoPin, INPUT);    
}
void loop()
{ 
  digitalWrite(TrigPin, LOW);           
  delayMicroseconds(2);                
  digitalWrite(TrigPin, HIGH);        
  delayMicroseconds(10);                
  digitalWrite(TrigPin, LOW);          
  duration = pulseIn(EchoPin, HIGH);   
  cm = duration*0.034/2;  

  Serial.print(cm);                  
  Serial.print("cm");                  
  Serial.println();                    
}

You’ve removed the lines from the Edgent example that say…

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
//#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI

so the Custom board configuration in Settings.h is being used.

This utilises GPIO4 (Pin D2) for the LED, so you have a pin conflict…

You’d be better adding back in the #define USE_NODE_MCU_BOARD line so that the onboard LED is utilised, along with the FLASH button on the board to clear the provisioning.

More info here…

Pete.

1 Like

THANKS Pete!!
I adding back in the #define USE_NODE_MCU_BOARD line and it work!!
I find the way to fix this so long.You made my day <3

1 Like