Advertisement
rsbelza

Untitled

Apr 9th, 2023
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.95 KB | Source Code | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266HTTPClient.h>
  3. #include <WiFiClient.h>
  4. #define TRIGGER_PIN D1
  5. #define ECHO_PIN D2
  6.  
  7.  
  8.   long duration, waterlevel;
  9.  
  10.   // INSERT WIFI CREDENTIALS
  11.   const char* ssid = "converge-2.4g";
  12.   const char* password = "Sierra911";
  13.   // Your Domain name with URL path or IP address with path
  14.   const char* serverName = "https://us-central1-floodmonitor-474e0.cloudfunctions.net/devices";
  15.  
  16. void setup() {
  17.   Serial.begin(9600);
  18.   pinMode(TRIGGER_PIN, OUTPUT);
  19.   pinMode(ECHO_PIN, INPUT);
  20.     // CONNECTING TO WIFI
  21.   int attempts = 0;
  22.   Serial.print("Connecting to Wi-Fi...");
  23.   while (WiFi.status() != WL_CONNECTED && attempts < 10) {
  24.     attempts++;
  25.     WiFi.begin(ssid, password);
  26.     delay(1000);
  27.     Serial.print(".");
  28. }  
  29. }
  30.  
  31. void loop() {
  32.   //INSERT DEVICEID AND LATITUDE AND LONGITUDE (NO LAT AND LONG SINCE NO GPS MODULE IS USED)
  33.     if (WiFi.status() == WL_CONNECTED) {
  34.     Serial.println("Still Connected");
  35.   } else {
  36.    
  37.     Serial.println("Reconnecting");
  38.       int attempts = 0;
  39.   Serial.print("Connecting to Wi-Fi...");
  40.   while (WiFi.status() != WL_CONNECTED && attempts < 10) {
  41.     attempts++;
  42.     WiFi.begin(ssid, password);
  43.     delay(1000);
  44.     Serial.print(".");
  45.     }
  46.  
  47.   }
  48.  
  49.   int deviceID = 1;
  50.   String lat = "14.5784832";
  51.   String longitude = "121.1367424";
  52.   String flooded = "";
  53.   float feet ;
  54.  
  55.  
  56.   // GETTING LEVEL OF FLOOD
  57.   waterlevel = ultrasonic_distance();
  58.   Serial.print("Flood Level: ");
  59.   Serial.println(waterlevel);
  60.  
  61.   //  CHANGE VALUE TO DETERMINE HOW HIGH IF ITS FLOODED OR NOT
  62.   bool flood = (waterlevel <= 90);
  63.   if (flood == 1) {
  64.   Serial.println("The Area is Flooded.");
  65.   flooded = "true";
  66.   } else {
  67.   Serial.println("The Area is not Flooded.");
  68.     flooded = "false";
  69.   }
  70.   feet = (122-waterlevel) * 0.0328;
  71.   String result=String(feet)+ " feet";
  72.   Serial.println(result);
  73.  
  74.   HTTPClient http;
  75.   WiFiClientSecure client;
  76.   client.setInsecure();
  77.      
  78.   // Your Domain name with URL path or IP address with path
  79.   http.begin(client,serverName);
  80.  
  81.      
  82.   // HTTP POST (SEND JSON TO DATABASE)
  83.   http.addHeader("Content-Type", "application/json");
  84.   String payload = "{\"deviceId\":\"" + String(deviceID) + "\",\"lat\":\"" + String(lat) + "\",\"long\":\"" + String(longitude) + "\",\"waterLevel\":\"" + String(result) + "\",\"flooded\":\"" + String(flooded) + "\"}"; ;
  85.   int httpResponseCode = http.POST(payload);
  86.      
  87.   // IF RESPONSE CODE IS LESS THAN 0 THEN DATA IS NOT BEING SENT  
  88.   Serial.print("HTTP Response code: ");
  89.   Serial.println(httpResponseCode);
  90.  
  91.   //Serial.print("Data Being Sent: ");
  92.   // CREATE A STRING TO SEND TO THE ARDUINO
  93.   String SMS = "Device ID: "+String(deviceID) +", Water Level : "+String(result)+ ", Area: DEVICE 1 LOC, Flooded: " + String(flooded);
  94.   // PUT DATA IN SERIAL MONITOR TO BE SENT TO ARDUINO TO BE SENT IN AN SMS
  95.   Serial.println(SMS);
  96.  
  97.   //STARTING TO SEND SMS
  98.   Serial.print("\r");
  99.   delay(10000);                  
  100.   Serial.print("AT+CMGF=1\r");    
  101.   delay(1000);
  102.   /*Replace XXXXXXXXXX to 10 digit mobile number &  ZZ to 2 digit country code*/
  103.   Serial.print("AT+CMGS=\"09171367340\"\r");    
  104.  
  105.   //The text of the message to be sent.
  106.   Serial.print(String(SMS));  
  107.   delay(1000);
  108.   Serial.write(0x1A);
  109.   delay(1000);
  110.        
  111.   // Free resources
  112.   http.end();
  113.  
  114.   // Wait for 1 second
  115.   delay(30000);
  116. }
  117.  
  118. long ultrasonic_distance() {
  119.   // Send a 10 microsecond pulse to the trigger pin
  120.   // HC-SR04 VCC -> NodeMCU 5V
  121.   // HC-SR04 GND -> NodeMCU GND
  122.   // HC-SR04 TRIG -> NodeMCU D1
  123.   // HC-SR04 ECHO -> NodeMCU D2
  124.   digitalWrite(TRIGGER_PIN, LOW);
  125.   delayMicroseconds(2);
  126.   digitalWrite(TRIGGER_PIN, HIGH);
  127.   delayMicroseconds(10);
  128.   digitalWrite(TRIGGER_PIN, LOW);
  129.  
  130.   // Measure the duration of the echo pulse
  131.   duration = pulseIn(ECHO_PIN, HIGH);
  132.  
  133.   // Convert the duration to a distance
  134.   waterlevel = duration * 0.034 / 2;
  135.  
  136.   // Return the distance measurement
  137.   return waterlevel;
  138. }
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement