Advertisement
rsbelza

NODEMCU + ULTRASONIC

Mar 16th, 2023
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.21 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.   // <- CODE THAT IS BLACKENED IS USED FOR TROUBLESHOOTING
  8.  
  9.   long duration, waterlevel;
  10.  
  11.   // INSERT WIFI CREDENTIALS
  12.   const char* ssid = "converge-2.4g";
  13.   const char* password = "Sierra911";
  14.   // Your Domain name with URL path or IP address with path
  15.   const char* serverName = "https://us-central1-floodmonitor-474e0.cloudfunctions.net/devices";
  16.  
  17. void setup() {
  18.   Serial.begin(9600);
  19.   pinMode(TRIGGER_PIN, OUTPUT);
  20.   pinMode(ECHO_PIN, INPUT);
  21.  
  22.   // CONNECTING TO WIFI
  23.   WiFi.begin(ssid, password);
  24.  // Serial.println("Connecting");
  25.   while(WiFi.status() != WL_CONNECTED) {
  26.     delay(500);
  27.  //   Serial.print(".");
  28.   }
  29.  // Serial.println("");
  30.  // Serial.print("Connected to WiFi network with IP Address: ");
  31.  // Serial.println(WiFi.localIP());
  32.  
  33.  
  34. }
  35.  
  36. void loop() {
  37.   //INSERT DEVICEID AND LATITUDE AND LONGITUDE (NO LAT AND LONG SINCE NO GPS MODULE IS USED)
  38.   int deviceID = 1;
  39.   String lat = "14.5784832";
  40.   String longitude = "121.1367424";
  41.   String flooded = "";
  42.  
  43.   // GETTING LEVEL OF FLOOD
  44.   waterlevel = ultrasonic_distance();
  45.   // Serial.print("Flood Level: ");
  46.   //Serial.println(waterlevel);
  47.  
  48.   //  CHANGE VALUE TO DETERMINE HOW HIGH IF ITS FLOODED OR NOT
  49.   bool flood = (waterlevel >= 50);
  50.   if (flood == 1) {
  51.   // Serial.println("The Area is Flooded.");
  52.   flooded = "true";
  53.   } else {
  54.   // Serial.println("The Area is not Flooded.");
  55.     flooded = "false";
  56.   }
  57.  
  58.  
  59.   HTTPClient http;
  60.   WiFiClientSecure client;
  61.   client.setInsecure();
  62.      
  63.   // Your Domain name with URL path or IP address with path
  64.   http.begin(client,serverName);
  65.  
  66.      
  67.   // HTTP POST (SEND JSON TO DATABASE)
  68.   http.addHeader("Content-Type", "application/json");
  69.   String payload = "{\"deviceId\":\"" + String(deviceID) + "\",\"lat\":\"" + String(lat) + "\",\"long\":\"" + String(longitude) + "\",\"waterLevel\":\"" + String(waterlevel) + "\",\"flooded\":\"" + String(flooded) + "\"}"; ;
  70.   int httpResponseCode = http.POST(payload);
  71.      
  72.   // IF RESPONSE CODE IS LESS THAN 0 THEN DATA IS NOT BEING SENT  
  73.   //Serial.print("HTTP Response code: ");
  74.   //Serial.println(httpResponseCode);
  75.  
  76.   //Serial.print("Data Being Sent: ");
  77.   // CREATE A STRING TO SEND TO THE ARDUINO
  78.   String SMS = "Device ID: "+String(deviceID) +" Water Level : "+String(waterlevel)+ " Area: Dito Flooded: " + String(flooded);
  79.   // PUT DATA IN SERIAL MONITOR TO BE SENT TO ARDUINO TO BE SENT IN AN SMS
  80.   Serial.println(SMS);
  81.        
  82.   // Free resources
  83.   http.end();
  84.  
  85.   // Wait for 1 second
  86.   delay(100000);
  87. }
  88.  
  89. long ultrasonic_distance() {
  90.   // Send a 10 microsecond pulse to the trigger pin
  91.   // HC-SR04 VCC -> NodeMCU 5V
  92.   // HC-SR04 GND -> NodeMCU GND
  93.   // HC-SR04 TRIG -> NodeMCU D1
  94.   // HC-SR04 ECHO -> NodeMCU D2
  95.   digitalWrite(TRIGGER_PIN, LOW);
  96.   delayMicroseconds(2);
  97.   digitalWrite(TRIGGER_PIN, HIGH);
  98.   delayMicroseconds(10);
  99.   digitalWrite(TRIGGER_PIN, LOW);
  100.  
  101.   // Measure the duration of the echo pulse
  102.   duration = pulseIn(ECHO_PIN, HIGH);
  103.  
  104.   // Convert the duration to a distance
  105.   waterlevel = duration * 0.034 / 2;
  106.  
  107.   // Return the distance measurement
  108.   return waterlevel;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement