Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <SoftwareSerial.h>
- #define trigPin 9
- #define echoPin 10
- SoftwareSerial SIM800L(3, 2);
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- bool changeFlag = 0;
- String message;
- long duration;
- float distanceD;
- float WaterPercentage, WaterPercentageOLD;
- unsigned long dataMillis = 0;
- unsigned long smsMillis = 0;
- void setup() {
- lcd.init();
- lcd.init();
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print("Hello, world!");
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- Serial.begin(9600);
- SIM800L.begin(9600);
- delay(5000);
- SendMessage();
- }
- void loop() {
- US();
- showData();
- }
- void US() {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distanceD = duration * 0.034 / 2;
- distanceD = constrain(distanceD, 4, 17);
- WaterPercentage = map(distanceD, 4, 17, 100, 1);
- WaterPercentage = constrain(WaterPercentage, 1, 100);
- // 0% 25% 50% 75% 100%
- //0--19
- if (WaterPercentage <= 19) {
- WaterPercentage = 0;
- }
- //20-39
- else if (WaterPercentage <= 39 && WaterPercentage >= 20) {
- WaterPercentage = 25;
- }
- //40-59
- else if (WaterPercentage <= 59 && WaterPercentage >= 40) {
- WaterPercentage = 50;
- }
- //60-85
- else if (WaterPercentage <= 85 && WaterPercentage >= 60) {
- WaterPercentage = 75;
- }
- //86-100
- else if (WaterPercentage >= 86) {
- WaterPercentage = 100;
- }
- }
- void showData() {
- if (millis() - dataMillis > 15000)// && WaterPercentage != WaterPercentageOLD)
- {
- changeFlag = 1;
- dataMillis = millis();
- WaterPercentageOLD = WaterPercentage;
- Serial.print("Distance: ");
- Serial.print(distanceD);
- Serial.print("\t Water Amount:");
- Serial.print(WaterPercentage);
- Serial.println("%");
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Water Level:");
- lcd.setCursor(0, 1);
- lcd.print(WaterPercentage);
- lcd.print("%");
- SendMessage();
- }
- if (WaterPercentage == WaterPercentageOLD && changeFlag == 1) {
- changeFlag = 0;
- //SendMessage();
- }
- }
- void SendMessage() {
- //if (millis() - smsMillis > 3000)
- //{
- // smsMillis = millis();
- //WaterPercentageOLD = WaterPercentage;
- Serial.println ("Sending Message");
- SIM800L.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
- delay(1000);
- Serial.println ("Set SMS Number");
- SIM800L.println("AT+CMGS=\"+966554522247\"\r"); //Mobile phone number to send message
- delay(1000);
- Serial.println("Set SMS Content");
- message = "Water Level:" + String(WaterPercentage) + "%";
- SIM800L.println(message);// Messsage content
- delay(100);
- Serial.println("Finish");
- SIM800L.println((char)26);// ASCII code of CTRL+Z
- delay(1000);
- Serial.println("Message has been sent");
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement