Advertisement
pleasedontcode

"ESP32 Blynk" rev_01

Jun 30th, 2024
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "ESP32 Blynk"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-30 18:06:04
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* If water level < 0, print empty; If water level > */
  21.     /* 16, print full; else print water level */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Ultrasonic.h>  //https://github.com/ErickSimoes/Ultrasonic
  26. #include <WiFi.h>        // Built-in library for WiFi on ESP32
  27. #include <BlynkSimpleEsp32.h>  // Blynk library for ESP32
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t Level_HC_SR04_Echo_PIN_D13 = 13;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t Level_HC_SR04_Trigger_PIN_D4 = 4;
  39.  
  40. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  41. /***** used to store raw data *****/
  42. bool Level_HC_SR04_Trigger_PIN_D4_rawData = 0;
  43.  
  44. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  45. /***** used to store data after characteristic curve transformation *****/
  46. float Level_HC_SR04_Trigger_PIN_D4_phyData = 0.0;
  47.  
  48. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  49. Ultrasonic ultrasonic(Level_HC_SR04_Trigger_PIN_D4, Level_HC_SR04_Echo_PIN_D13);  // Initialize Ultrasonic object with trigger and echo pins
  50.  
  51. char auth[] = "YourAuthToken";  // Blynk authentication token
  52. char ssid[] = "YourNetworkName";  // WiFi SSID
  53. char pass[] = "YourPassword";  // WiFi Password
  54.  
  55. BlynkTimer timer;  // Blynk Timer object
  56.  
  57. void myTimerEvent() {
  58.   Blynk.virtualWrite(V5, millis() / 1000);  // Send uptime to Blynk app
  59. }
  60.  
  61. void setup(void)
  62. {
  63.     // put your setup code here, to run once:
  64.     Serial.begin(9600);  // Initialize serial communication for debugging
  65.     pinMode(Level_HC_SR04_Echo_PIN_D13, INPUT);
  66.     pinMode(Level_HC_SR04_Trigger_PIN_D4, OUTPUT);
  67.  
  68.     // Initialize Blynk
  69.     Blynk.begin(auth, ssid, pass);
  70.  
  71.     // Call myTimerEvent every second
  72.     timer.setInterval(1000L, myTimerEvent);
  73. }
  74.  
  75. void loop(void)
  76. {
  77.     // put your main code here, to run repeatedly:
  78.     Blynk.run();  // Run Blynk
  79.     timer.run();  // Run Blynk timer
  80.     updateOutputs();  // Refresh output data
  81.     delay(1000);  // Wait for 1 second before next measurement
  82. }
  83.  
  84. void updateOutputs()
  85. {
  86.     // Trigger the ultrasonic sensor and read the distance
  87.     Level_HC_SR04_Trigger_PIN_D4_phyData = ultrasonic.read();
  88.  
  89.     // Print the distance to the Serial Monitor
  90.     Serial.print("Distance in CM: ");
  91.     Serial.println(Level_HC_SR04_Trigger_PIN_D4_phyData);
  92.  
  93.     // Update the raw data output
  94.     Level_HC_SR04_Trigger_PIN_D4_rawData = (Level_HC_SR04_Trigger_PIN_D4_phyData < 10);  // Example condition
  95.  
  96.     // Write the raw data to the trigger pin
  97.     digitalWrite(Level_HC_SR04_Trigger_PIN_D4, Level_HC_SR04_Trigger_PIN_D4_rawData);
  98.  
  99.     // System requirement: Print water level status
  100.     if (Level_HC_SR04_Trigger_PIN_D4_phyData < 0) {
  101.         Serial.println("Water level: Empty");
  102.     } else if (Level_HC_SR04_Trigger_PIN_D4_phyData > 16) {
  103.         Serial.println("Water level: Full");
  104.     } else {
  105.         Serial.print("Water level: ");
  106.         Serial.println(Level_HC_SR04_Trigger_PIN_D4_phyData);
  107.     }
  108. }
  109.  
  110. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement