Advertisement
iriad11

water_quality_monitoring

Apr 15th, 2021
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Header declearation
  2. #include <LiquidCrystal_I2C.h> // Library for I2C lcd
  3. #include <SPI.h> // Library for serial peripherals
  4. #include <WiFi.h> // Library for wifi functions
  5. #include <BlynkSimpleEsp32.h> // Library for Blynk on ESP32
  6. #include "DFRobot_ESP_PH.h" // Library for reading ph
  7. #include <EEPROM.h> // Library to access eeprom
  8.  
  9. // Pin Assignment and declearation
  10. // Pin connection list
  11. // pin VP(39) of esp32 is connected to out(data) pin of turbidity sensor
  12. // pin D35(35) of esp32 is connected to Po(Ph) pin of Ph sensor
  13. // pin D22(SCL) of esp32 is connected to SCL pin of I2C display
  14. // pin D21(SDA) of esp32 is connected to SDA pin of I2C display
  15.  
  16. #define BLYNK_PRINT Serial // Define serial communication for Blynk
  17. #define ESPADC 4095.0   //the esp Analog Digital Convertion value
  18. #define ESPVOLTAGE 3300 //the esp voltage supply value
  19. #define analogTurbPin 39 // Turbidity module pin out is connected to analog pin 39
  20. #define analogPhPin 35 // PH module pin P0 connected to analog pin 35
  21.  
  22.  
  23. const char* ssid = "no internet";//Write your wifi network's name here
  24. const char* password = "monenaito";//Write your password here
  25. char auth[] = "0VxrOQfrKy0SrGr3P2C71zgO6h-HQwwu";  // Auth Token from Blynk Project
  26.  
  27. LiquidCrystal_I2C lcd(0x27, 16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
  28. BlynkTimer timer; // timer function of Blynk to call
  29. DFRobot_ESP_PH ph; // ph object of DFROBOT_ESP_PH class
  30.  
  31. long turbidityTotal;
  32. float turbidity, phValue, temperature = 30;
  33. int x;
  34. //const float C = 21.34; // Constant of straight line (Y = mx + C)
  35. //const float m = -5.70; // Slope of straight line (Y = mx + C)
  36.  
  37. // Function to write sensor data I2C LCD display
  38. void displaySensorDataToLCD(float turbidity, float ph)
  39. {
  40.   lcd.setCursor(0, 0);
  41.   lcd.print("Turbidity |  Ph");
  42.   lcd.setCursor(3, 1);
  43.   lcd.print(turbidity,2);
  44.   lcd.setCursor(10, 1);
  45.   lcd.print("|");
  46.   lcd.setCursor(12, 1);
  47.   lcd.print(ph,2);
  48. }
  49.  
  50. void readSendSensorData()
  51. {
  52.   turbidityTotal = 0;
  53.   float turbidityAvg = 0;
  54.   turbidity=0;
  55.   //taking 10 sample and adding with 10 milli second delay
  56.   for(x=0; x<10 ; x++)
  57.   {
  58.     turbidityTotal += analogRead(analogTurbPin);
  59.     delay(10);
  60.   }
  61.   turbidityAvg = turbidityTotal/10;
  62.   turbidity = abs(4095-turbidityAvg)/800;
  63.   if(turbidity>5.0)
  64.   {
  65.     turbidity = 5.0;
  66.   }
  67.  
  68.   float phVoltage = analogRead(analogPhPin)/ESPADC*ESPVOLTAGE; // read the voltage
  69.   phValue = ph.readPH(phVoltage, temperature); // convert voltage to pH with temperature compensation
  70.  
  71.   // Send sensor readings to Blynk
  72.   // Please don't send more that 10 values per second.
  73.   Blynk.virtualWrite(V5, turbidity);
  74.   Blynk.virtualWrite(V6, phValue);
  75.   displaySensorDataToLCD(turbidity, phValue); // Send sensor data to display function
  76. }
  77.  
  78. void setup() {
  79.  
  80.   Serial.begin(9600); // Setup serial communication
  81.   Blynk.begin(auth, ssid, password); // Setup blynk server
  82.   EEPROM.begin(32); // needed to permit storage of calibration value in eeprom
  83.   ph.begin(); // begin ph object
  84.  
  85.   // Start I2C LCD module and show 'IOT Based Water Quality Monitor' for 1.5 seconds ar powering up
  86.   lcd.begin(16,2);
  87.   lcd.init();
  88.   lcd.backlight();
  89.   lcd.setCursor(0, 0);
  90.   lcd.print("IOT based Water");
  91.   lcd.setCursor(0, 1);
  92.   lcd.print("Quality Monitor");
  93.   delay(1500);
  94.   lcd.clear();
  95.   // End powerup display and clear the display
  96.  
  97.   // Setup a function to be called every second
  98.   timer.setInterval(1000L, readSendSensorData);
  99. }
  100.  
  101. void loop()
  102. {
  103.   Blynk.run();
  104.   timer.run();
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement