Advertisement
safwan092

Project_3652_final code

Nov 17th, 2021
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.57 KB | None | 0 0
  1. /*************************************************************
  2.   Download latest Blynk library here:
  3.     https://github.com/blynkkk/blynk-library/releases/latest
  4.  
  5.   Blynk is a platform with iOS and Android apps to control
  6.   Arduino, Raspberry Pi and the likes over the Internet.
  7.   You can easily build graphic interfaces for all your
  8.   projects by simply dragging and dropping widgets.
  9.  
  10.     Downloads, docs, tutorials: http://www.blynk.cc
  11.     Sketch generator:           http://examples.blynk.cc
  12.     Blynk community:            http://community.blynk.cc
  13.     Follow us:                  http://www.fb.com/blynkapp
  14.                                 http://twitter.com/blynk_app
  15.  
  16.   Blynk library is licensed under MIT license
  17.   This example code is in public domain.
  18.  
  19.  *************************************************************
  20.   This example runs directly on ESP32 chip.
  21.  
  22.   Note: This requires ESP32 support package:
  23.     https://github.com/espressif/arduino-esp32
  24.  
  25.   Please be sure to select the right ESP32 module
  26.   in the Tools -> Board menu!
  27.  
  28.   Change WiFi ssid, pass, and Blynk auth token to run :)
  29.   Feel free to apply it to any other example. It's simple!
  30.  *************************************************************/
  31.  
  32. /* Comment this out to disable prints and save space */
  33. #define BLYNK_PRINT Serial
  34.  
  35. /* Fill-in your Template ID (only if using Blynk.Cloud) */
  36. //#define BLYNK_TEMPLATE_ID   "YourTemplateID"
  37.  
  38. #include <WiFi.h>
  39. #include <WiFiClient.h>
  40. #include <BlynkSimpleEsp32.h>
  41. #include <ESP32Servo.h>
  42.  
  43. Servo servo1;
  44.  
  45. BlynkTimer timer;
  46.  
  47. //Pins Connections:
  48. #define soil_moisture_PIN 35  //
  49. #define LOW_indicator_PIN 3   //21  //
  50. #define HIGH_indicator_PIN 19 //
  51. #define relay_1_PIN 4         // Water Pump
  52. #define relay_2_PIN 21  //18        // LED Strip
  53. #define servo_PIN 13          //
  54. #define LDR_PIN 33            //
  55. #define redLED_PIN 25         //
  56. #define blueLED_PIN 26        //
  57. // You should get Auth Token in the Blynk App.
  58. // Go to the Project Settings (nut icon).
  59. char auth[] = "tg00tBqfJZA1BCOli4ztyyhvffJQ3-N6";
  60.  
  61. // Your WiFi credentials.
  62. // Set password to "" for open networks.
  63. char ssid[] = "network";
  64. char pass[] = "123456789";
  65.  
  66. void sendSensor()
  67. {
  68.   int value1 = analogRead(soil_moisture_PIN);
  69.   value1 = map(value1, 4095, 0, 0, 100);
  70.   int value2 = digitalRead(LOW_indicator_PIN);
  71.   int value3 = digitalRead(HIGH_indicator_PIN);
  72.   int value4 = analogRead(LDR_PIN);
  73.   Serial.print(value1);
  74.   Serial.print(" | ");
  75.   Serial.print(value2);
  76.   Serial.print(" | ");
  77.   Serial.print(value3);
  78.   Serial.print(" | ");
  79.   Serial.println(value4);
  80.   int percent = 0;
  81.  
  82.  
  83.   if (value2 == 0 && value3 == 1) {
  84.     percent = 20;
  85.     digitalWrite(redLED_PIN, 1);
  86.     digitalWrite(blueLED_PIN, 0);
  87.     Serial.println("#1");
  88.   }
  89.   else if (value3 == 0 && value2 == 0) {
  90.     percent = 100;
  91.     digitalWrite(redLED_PIN, 0);
  92.     digitalWrite(blueLED_PIN, 1);
  93.     Serial.println("#3");
  94.   }
  95.   else if (value3 != 0 && value2 != 0) {
  96.     percent = 0;
  97.     digitalWrite(relay_1_PIN, 1); // turn off water pump
  98.     digitalWrite(redLED_PIN, 1);
  99.     digitalWrite(blueLED_PIN, 0);
  100.     Serial.println("#4");
  101.   }
  102.  
  103.   if (value1 < 60 && value1 != 0 && percent != 0) {
  104.     digitalWrite(relay_1_PIN, 0);
  105.     Serial.println("#5");
  106.   }
  107.   else if (value1 > 90 || percent == 0 || value1 == 0) {
  108.     digitalWrite(relay_1_PIN, 1);
  109.     Serial.println("#6");
  110.   }
  111.  
  112.   if (value4 < 700) {
  113.     digitalWrite(relay_2_PIN, 0);
  114.     Serial.println("#7");
  115.   }
  116.   else if (value4 > 700) {
  117.     digitalWrite(relay_2_PIN, 1);
  118.     Serial.println("#8");
  119.   }
  120.  
  121.   Blynk.virtualWrite(V5, value1);
  122.   Blynk.virtualWrite(V6, percent);
  123. }
  124.  
  125. void setup()
  126. {
  127.   // Debug console
  128.   Serial.begin(9600);
  129.   ESP32PWM::allocateTimer(0);
  130.   ESP32PWM::allocateTimer(1);
  131.   ESP32PWM::allocateTimer(2);
  132.   ESP32PWM::allocateTimer(3);
  133.   servo1.setPeriodHertz(50);// Standard 50hz servo
  134.   servo1.attach(servo_PIN, 500, 2400);
  135.   pinMode(soil_moisture_PIN, INPUT);
  136.   pinMode(LOW_indicator_PIN, INPUT);
  137.   pinMode(HIGH_indicator_PIN, INPUT);
  138.   pinMode(LDR_PIN, INPUT);
  139.   pinMode(relay_1_PIN, OUTPUT);
  140.   pinMode(relay_2_PIN, OUTPUT);
  141.   pinMode(redLED_PIN, OUTPUT);
  142.   pinMode(blueLED_PIN, OUTPUT);
  143.   //digitalWrite(relay_1_PIN, 0);
  144.   //digitalWrite(relay_2_PIN, 0);
  145.   //delay(1000);
  146.   digitalWrite(relay_1_PIN, 1);
  147.   digitalWrite(relay_2_PIN, 1);
  148.   digitalWrite(redLED_PIN, 0);
  149.   digitalWrite(blueLED_PIN, 0);
  150.   Blynk.begin(auth, ssid, pass);
  151.   timer.setInterval(2000L, sendSensor);
  152. }
  153.  
  154. void loop()
  155. {
  156.   Blynk.run();
  157.   timer.run();
  158. }
  159.  
  160. BLYNK_WRITE(V3)
  161. {
  162.   servo1.write(param.asInt());
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement