safwan092

Untitled

Jan 7th, 2023
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <SoftwareSerial.h>
  4.  
  5. #define trigPin 9
  6. #define echoPin 10
  7.  
  8.  
  9. SoftwareSerial SIM800L(3, 2);
  10. LiquidCrystal_I2C lcd(0x27, 16, 2);
  11.  
  12. bool changeFlag = 0;
  13. String message;
  14. long duration;
  15. float distanceD;
  16. float WaterPercentage, WaterPercentageOLD;
  17. unsigned long dataMillis = 0;
  18. unsigned long smsMillis = 0;
  19.  
  20. void setup() {
  21. lcd.init();
  22. lcd.init();
  23. lcd.backlight();
  24. lcd.setCursor(0, 0);
  25. lcd.print("Hello, world!");
  26. pinMode(trigPin, OUTPUT);
  27. pinMode(echoPin, INPUT);
  28. Serial.begin(9600);
  29. SIM800L.begin(9600);
  30. delay(5000);
  31. }
  32. void loop() {
  33. US();
  34. showData();
  35. }
  36.  
  37. void US() {
  38. digitalWrite(trigPin, LOW);
  39. delayMicroseconds(2);
  40. digitalWrite(trigPin, HIGH);
  41. delayMicroseconds(10);
  42. digitalWrite(trigPin, LOW);
  43. duration = pulseIn(echoPin, HIGH);
  44. distanceD = duration * 0.034 / 2;
  45. distanceD = constrain(distanceD, 4, 22);
  46. WaterPercentage = map(distanceD, 4, 22, 100, 1);
  47. WaterPercentage = constrain(WaterPercentage, 1, 100);
  48. // 0% 25% 50% 75% 100%
  49. //0--19
  50. if (WaterPercentage <= 19) {
  51. WaterPercentage = 0;
  52. }
  53.  
  54. //20-39
  55. else if (WaterPercentage <= 39 && WaterPercentage >= 20) {
  56. WaterPercentage = 25;
  57. }
  58.  
  59. //40-59
  60. else if (WaterPercentage <= 59 && WaterPercentage >= 40) {
  61. WaterPercentage = 50;
  62. }
  63.  
  64. //60-85
  65. else if (WaterPercentage <= 85 && WaterPercentage >= 60) {
  66. WaterPercentage = 75;
  67. }
  68.  
  69. //86-100
  70. else if (WaterPercentage >= 86) {
  71. WaterPercentage = 100;
  72. }
  73.  
  74. }
  75.  
  76. void showData() {
  77. if (millis() - dataMillis > 1000 && WaterPercentage != WaterPercentageOLD)
  78. {
  79. changeFlag = 1;
  80. dataMillis = millis();
  81. WaterPercentageOLD = WaterPercentage;
  82. Serial.print("Distance: ");
  83. Serial.print(distanceD);
  84. Serial.print("\t Water Amount:");
  85. Serial.print(WaterPercentage);
  86. Serial.println("%");
  87. lcd.clear();
  88. lcd.setCursor(0, 0);
  89. lcd.print("Water Level:");
  90. lcd.setCursor(0, 1);
  91. lcd.print(WaterPercentage);
  92. lcd.print("%");
  93. }
  94. if (WaterPercentage == WaterPercentageOLD && changeFlag == 1) {
  95. changeFlag = 0;
  96. SendMessage();
  97. }
  98. }
  99.  
  100.  
  101. void SendMessage() {
  102. if (millis() - smsMillis > 30000)
  103. {
  104. smsMillis = millis();
  105. WaterPercentageOLD = WaterPercentage;
  106. Serial.println ("Sending Message");
  107. SIM800L.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
  108. delay(1000);
  109. Serial.println ("Set SMS Number");
  110. SIM800L.println("AT+CMGS=\"+966554522247\"\r"); //Mobile phone number to send message
  111. delay(1000);
  112. Serial.println("Set SMS Content");
  113. message = "Water Level:" + String(WaterPercentage) + "%";
  114. SIM800L.println(message);// Messsage content
  115. delay(100);
  116. Serial.println("Finish");
  117. SIM800L.println((char)26);// ASCII code of CTRL+Z
  118. delay(1000);
  119. Serial.println("Message has been sent");
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment