Advertisement
rsbelza

Edited Code

Mar 22nd, 2023
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.83 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.  
  54.   // GETTING LEVEL OF FLOOD
  55.   waterlevel = ultrasonic_distance();
  56.   Serial.print("Flood Level: ");
  57.   Serial.println(waterlevel);
  58.  
  59.   //  CHANGE VALUE TO DETERMINE HOW HIGH IF ITS FLOODED OR NOT
  60.   bool flood = (waterlevel >= 50);
  61.   if (flood == 1) {
  62.   Serial.println("The Area is Flooded.");
  63.   flooded = "true";
  64.   } else {
  65.   Serial.println("The Area is not Flooded.");
  66.     flooded = "false";
  67.   }
  68.  
  69.  
  70.   HTTPClient http;
  71.   WiFiClientSecure client;
  72.   client.setInsecure();
  73.      
  74.   // Your Domain name with URL path or IP address with path
  75.   http.begin(client,serverName);
  76.  
  77.      
  78.   // HTTP POST (SEND JSON TO DATABASE)
  79.   http.addHeader("Content-Type", "application/json");
  80.   String payload = "{\"deviceId\":\"" + String(deviceID) + "\",\"lat\":\"" + String(lat) + "\",\"long\":\"" + String(longitude) + "\",\"waterLevel\":\"" + String(waterlevel) + "\",\"flooded\":\"" + String(flooded) + "\"}"; ;
  81.   int httpResponseCode = http.POST(payload);
  82.      
  83.   // IF RESPONSE CODE IS LESS THAN 0 THEN DATA IS NOT BEING SENT  
  84.   Serial.print("HTTP Response code: ");
  85.   Serial.println(httpResponseCode);
  86.  
  87.   //Serial.print("Data Being Sent: ");
  88.   // CREATE A STRING TO SEND TO THE ARDUINO
  89.   String SMS = "Device ID: "+String(deviceID) +" Water Level : "+String(waterlevel)+ " Area: Dito Flooded: " + String(flooded);
  90.   // PUT DATA IN SERIAL MONITOR TO BE SENT TO ARDUINO TO BE SENT IN AN SMS
  91.   Serial.println(SMS);
  92.  
  93.   //STARTING TO SEND SMS
  94.   Serial.print("\r");
  95.   delay(10000);                  
  96.   Serial.print("AT+CMGF=1\r");    
  97.   delay(1000);
  98.   /*Replace XXXXXXXXXX to 10 digit mobile number &  ZZ to 2 digit country code*/
  99.   Serial.print("AT+CMGS=\"09171367340\"\r");    
  100.  
  101.   //The text of the message to be sent.
  102.   Serial.print(String(SMS));  
  103.   delay(1000);
  104.   Serial.write(0x1A);
  105.   delay(1000);
  106.        
  107.   // Free resources
  108.   http.end();
  109.  
  110.   // Wait for 1 second
  111.   delay(30000);
  112. }
  113.  
  114. long ultrasonic_distance() {
  115.   // Send a 10 microsecond pulse to the trigger pin
  116.   // HC-SR04 VCC -> NodeMCU 5V
  117.   // HC-SR04 GND -> NodeMCU GND
  118.   // HC-SR04 TRIG -> NodeMCU D1
  119.   // HC-SR04 ECHO -> NodeMCU D2
  120.   digitalWrite(TRIGGER_PIN, LOW);
  121.   delayMicroseconds(2);
  122.   digitalWrite(TRIGGER_PIN, HIGH);
  123.   delayMicroseconds(10);
  124.   digitalWrite(TRIGGER_PIN, LOW);
  125.  
  126.   // Measure the duration of the echo pulse
  127.   duration = pulseIn(ECHO_PIN, HIGH);
  128.  
  129.   // Convert the duration to a distance
  130.   waterlevel = duration * 0.034 / 2;
  131.  
  132.   // Return the distance measurement
  133.   return waterlevel;
  134. }
  135.  
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement