Advertisement
jasper_fracture

Basic HCSR04 Data to Blynk Value Widget Example

Dec 10th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. // Basic HCSR04 Data to Blynk Value Widget Example
  2. // jasper_fracture // jasperfracture.com
  3.  
  4. #include <BlynkSimpleEsp8266.h>
  5.  
  6. char auth[] = "your_blynk_token_here";
  7. char ssid[] = "your_wifi_ssid_here";
  8. char pass[] = "your_wifi_password_here";
  9.  
  10. // gpio pins for the ultrasonic sensor
  11. const int trigPin = 12;  // pin D6 on board
  12. const int echoPin = 13;  // pin D7 on board
  13.  
  14. void setup()
  15. {
  16.   // put your setup code here, to run once:
  17.   Blynk.begin(auth, ssid, pass);
  18. }
  19.  
  20. void loop()
  21. {
  22.   // put your main code here, to run repeatedly:
  23.   Blynk.run();
  24. }
  25.  
  26. // your basic HCSR04 code to return value in inches
  27. // much of this code was borrowed from the Ping example
  28. int checkFront()
  29. {  
  30.   long duration, inches;
  31.   pinMode(trigPin, OUTPUT);
  32.   digitalWrite(trigPin, LOW);
  33.   delayMicroseconds(2);
  34.   digitalWrite(trigPin, HIGH);
  35.   delayMicroseconds(10);
  36.   digitalWrite(trigPin, LOW);
  37.   pinMode(echoPin, INPUT);
  38.   duration = pulseIn(echoPin, HIGH);
  39.   inches = microsecondsToInches(duration);
  40.   return inches;
  41. }
  42.  
  43. long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; }
  44.  
  45. //  Blynk function to push data to your phone app through V1
  46. //  Be sure to set up a "Value Display M" or something similar in your app
  47. //  Give the widget a label, set the input to V1, and set the reading frequency to something like 500 ms
  48. //  The Blynk "virtualWrite" function will keep the data on your phone in synch with the hardware every .5 seconds
  49. BLYNK_READ(V1)
  50. {
  51.   Blynk.virtualWrite(V1, checkFront());
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement